Search code examples
bashunixfastq

executing a command on multiple paired files


Say I have a command, command.py, and it pairs together files, File_01_R1.fastq to File_01_R2.fastq. The command executed on a single pair looks like this:

command.py -f File_01_R1.fastq -r File_01_R2.fastq

I have many files however, each with a R1 and R2 version. How can I tell this command to go through every file I have, so it also executes

command.py -f File_02_R1.fastq -r File_02_R2.fastq
command.py -f File_03_R1.fastq -r File_03_R2.fastq

and so on.


Solution

  • You may use a simple parameter expansion:

    for f in *_R1.fastq; do
        echo command.py -f "$f" -r "${f%_R1.fastq}_R2.fastq"
    done
    

    This will just print out what's to be executed. Remove the echo if you're happy with the result.