I have 25 related pairs of files in a folder called Data. These files are named tcr1_r1.txt and tcr1_r2.txt, tcr2_r1.txt and tcr2_r2.txt, and so on until I reach tcr25… (or how many files pairs I have). My problem is that I need to pair them up and run a command on each pair in a batch file. Example:

command  tcr1_r1.txt  tcr2_r2.txt
command  tcr2_r1.txt  tcr2_r2.txt

How can I do this? I'm thinking a loop, but I can't seem to be able to separate and alternate the files on each command. I tried a nested loop but that just runs each "r1" file with all the "r2" files.

for filename1 in /Data/*_r1.txt
  for filename2 in /Data/*_r2.txt
     echo "$filename1 and $filename2"

I have tried to use Jeff Schaller’s answer.  Here are the exact shell file lines I tried:


for first in /mnt/data/Sequencing_core/Data/Raw_data/062419_TCRB_Vanessa_Danielle/20190624_FS10000703_3_BPC29606-1232/Alignment_1/20190625_132145/Fastq/*R1_001.fastq.gz

  echo "$first"
  echo "${first/_R1_001.fastq.gz/_R2_001.fastq.gz}" 


I must be missing something.  I’m getting a "Bad substitution" error message.

  • Please run the loop with my modification (echo the R1 name and the R2 name separately).  Then, as Jeff asked, show the output/error it gives.  And add the output of bash --version. – Scott Jun 29 at 1:02
  • 2
    I think you made a mistake in writing command tcr1_r1.txt tcr2_r2.txt - please edit if so. – Wildcard Jun 29 at 1:46

Two ways, depending on whether you want to care about the total number of files.

In the first way, you know the number of files is 25 (specifically named with 1 through 25):

for index in {1..25}
  command tcr"${index}"_r1.txt tcr"${index}"_r2.txt

Above, the (bash) shell expands the {1..25} to the full set of numbers; we then substitute those numbers into the appropriate place in the paired filenames.

In the second way, you don't know or care how many files there are:

for first in tcr*_r1.txt
  command "$first" "${first/_r1.txt/_r2.txt}"

Above, we loop over all of the "r1" files and substitute the "_r1.txt" part for the paired "_r2.txt".

  • Very clever. I'll give it a try. – Orion11 Jun 28 at 15:27
  • I must be missing something. I'm getting a "Bad substitution" error message. – Orion11 Jun 28 at 15:35
  • On which method? Are you using the bash shell? – Jeff Schaller Jun 28 at 16:39
  • The second way. – Orion11 Jun 28 at 16:57
  • I can't reproduce that error; could you please edit your question to show the command you ran, the output/error it gave, and the output of echo "$SHELL"? – Jeff Schaller Jun 28 at 17:55

You can just use xargs:

echo /Data/*_r[12].txt | xargs -n 2 the-command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.