For loop with two input files


I wish to use a for loop for a command that requires two input files. There is a single character change between the two files. Here are two example files:

Fay2_TCCGGAGA-CCTATCCT_L001_R1_001.fastq Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq

Here is my attempt of the command:

for f in /directory/*R1*.fastq pref=${basename "$f" _*R1*.fastq} command input1 $f input2 ${pref}_*R2*.fastq

The issue most likely lies in manipulating the basename so the loop uses the permutation of file1 to find file2. How do I make the basename shorter than the entire file name. I am getting the error

Fay2_TCCGGAGA-CCTATCCT_L001_R2_001.fastq_*R2.fastq does not exist

Thanks to comments below the looping is working properly but my files are being overwritten whenever a the command loops over a new file. The following is my entire command which requires naming both output files.

for f1 in /directory/*R1*.fastq f2="${f1/_R1_/_R2_}" pref=${basename "$f" _*R1*.fastq} command output1 ${pref}_trim_R1.fastq output2 ${pref}_trim_R2.fastq input1 $f input2 $f2


You can loop through the R1 files and use brace expansion to derive R2 file names:

for r1_file in /directory/*_R1_*.fastq; do r2_file="${r1_file/_R1_/_R2_}" command input1 "$r1_file" input2 "$r2_file" done


  • starting slurm array job with a specified number of nodes
  • Splitting large files efficiently (currently using awk)
  • How can I use a list of lists, or a list of sets, for the TfidfVectorizer?
  • Setting SGE for running an executable with different input files on different nodes
  • Non-recursive algorithm for full permutation with repetitive elements?
  • Browser-sniffing alternative for bugs/misbehaviors (NOT lack of features)
  • Parsing Strings in SuperCSV
  • Alert Dialog was disappearing when user clicks out side
  • I have accidentally uninstalled jack server while building Android AOSP
  • Jquery - Differentiate between 'click' and 'focus' on same input when using both
  • Selecting an integer from each of the given n sets of integers such that the sum of their pairwise d
  • Can I make JSF2 skip rendering my custom tag without modifying the tag itself?
  • send intent from main activity to two fragment
  • Boot EBS volume from Bamboo instance
  • react-helmet outputting empty strings on server-side
  • Go lang multiplexing all goroutines are asleep - deadlock
  • Zipping the contents of a folders in PHP
  • F# NativePtr.stackalloc Unexpected Stack Overflow
  • Ansible sudo hangs after 5 tasks
  • IIS and ISAPI-WSGI = very slow
  • Why is my SqlPackage DeployReport returning an empty report?
  • PHP PDF generation problem
  • Batch file working differently if ran in administrator mode
  • Add class element based on the name of the page
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • removing last 3 characters on a file (file extension)
  • Inversing an interpolation of rotation
  • Passing 1 of 3 values to a Sub
  • Javascript simulate pressing enter in input box
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Obtain ObjectIdHex value from mgo query
  • How to check if every primary key value is being referenced as foreign key in another table
  • ILMerge & Keep Assembly Name
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Why winpcap requires both .lib and .dll to run?
  • Acquiring multiple attributes from .xml file in c#
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?