<p>Something like this? This uses <code>printf</code> to create a zero-padded 3-digit number corresponding to 17 numbers from 1 to 17:</p>
<pre><code>for fileBase in $(printf 'file_%03d\n' $(seq 1 17));
   do echo ${fileBase}_R1.fq.gz ${fileBase}_R2.fq.gz;
   pear -f ${fileBase}_R1.fastq.gz -r ${fileBase}_R2.fastq.gz -o ${fileBase}.fastq
   ...
done
</code></pre>

CC BY-SA 4.0
2023-06-30T00:11:19.947Z
2023-06-30T00:11:19.947Z
gringer
<p>Here's what I use - along with an example you can modify as needed:</p>
<pre><code>FASTQ_DIR=&quot;/directory/containing/FASTQs/&quot;

for R1_FASTQ in &quot;<span class="math-container">$FASTQ_DIR"*R1*.fastq.gz
do
                         BASE=$</span>(basename &quot;<span class="math-container">$R1_FASTQ" _R1.fastq.gz)
                         R2_FASTQ="$</span>{R1_FASTQ/R1/R2}&quot;
                         R3_FASTQ=&quot;<span class="math-container">${R1_FASTQ/R1/R3}"
                         echo "BASE: $</span>BASE&quot;
                         echo &quot;R1: <span class="math-container">$R1_FASTQ"
                         echo "R2: $</span>R2_FASTQ&quot;
                         echo &quot;R3: <span class="math-container">$R3_FASTQ"
                         # process R1/R2 here using "$</span>R1_FASTQ&quot; &quot;<span class="math-container">$R2_FASTQ" for input, and "$</span>{BASE}_XXXXXXX.fastq&quot; for output
done
</code></pre>
<p>Basically, the strategy is to iterate over all the R1 FASTQs in a directory, calculate a <code>BASE</code> name that can be used to rename output files with arbitrary extensions (like <code>.sam</code>, <code>.bam</code>, etc...), and use BASH shell parameter expansion to calculate and set variables to the R2 (and R3 if necessary) reads present in an Illumina run that uses standard Illumina naming conventions.</p>
<p>This may need modification depending on the naming conventions you are using.</p>

CC BY-SA 4.0
2023-06-29T18:35:19.640Z
2023-06-29T18:40:25.670Z
2023-06-29T18:40:25.670Z
Scot
<p>I'm expecting to get 17 different paired-end fastq files (34 in total), so I want to make a bash script to just run my code through all the fastq files in a directory at once. How can I change the name of the input and output files each time the script runs through each file? So when it moves to the file_002, all names have file_002 at the beginning instead of file_001, and so on. And also, when merging the R1 and R2 reads how can I make that it only merges the correspondant files with a loop? for examples merging only file_001_R1 with file_001_R2, file_002_R1 with file_002_R2, file_003_R1 with file_003_R2, and so on.</p>
<p>This is my code:</p>
<pre><code>for file in directory_name
do
pear -f file_001_R1.fastq.gz -r file_001_R2.fastq.gz -o file_001.fastq
cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_001_barcode.fastq file_001.fastq
cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_001.fastq
seqkit concat file_001_barcode.fastq file_001_UMI.fastq &gt; file_001_concatenation.fastq
seqkit rmdup -s file_001_concatenation.fastq -o file_001_unique_pairs.fastq
seqkit subseq -r file_001_unique_pairs.fastq &gt; file_001_unique_barcodes.fasta
bowtie -q --suppress 1,2,4,6,7,8 -x ref_index file_001_unique_barcodes.fasta &gt; file_001_barcodes_allignment.bowtie
sort file_001_barcodes_allignment.bowtie | uniq -c &gt; file_001_barcode_counts.txt
awk 'BEGIN{print &quot;Barcode,TF_variant,Code&quot;}{print $3&quot;,&quot;$2&quot;,&quot;$1}' file_001_barcode_counts.txt &gt; file_001_barcode_counts.csv
done
</code></pre>
<p>Thank you.</p>

CC BY-SA 4.0
2023-06-29T16:25:58.907Z
I assume the code is `directory_name/*`, `$file` is now by each file within `directory_name`. At the moment `$file` is simply `directory_name` as its written. There'll be bash experts here that will describe the loop for the paired end files.
M__
CC BY-SA 4.0
2023-06-30T16:43:06.850Z
Hi, please consider upvoting and accepting one of the answers. These are good answers from two different perspectives: one pulling the directory file contents; the second using a numerical loop. Both would work.
M__
<bash>
How can I give different names to files in a directory with a for loop in a bash script?
# How can I give different names to files in a directory with a for loop in a bash script?

(Asked by: username_0 on 2023-06-29)

I'm expecting to get 17 different paired-end fastq files (34 in total), so I want to make a bash script to just run my code through all the fastq files in a directory at once. How can I change the name of the input and output files each time the script runs through each file? So when it moves to the file_002, all names have file_002 at the beginning instead of file_001, and so on. And also, when merging the R1 and R2 reads how can I make that it only merges the correspondant files with a loop? for examples merging only file_001_R1 with file_001_R2, file_002_R1 with file_002_R2, file_003_R1 with file_003_R2, and so on.
This is my code:
for file in directory_name
do
pear -f file_001_R1.fastq.gz -r file_001_R2.fastq.gz -o file_001.fastq
cutadapt -g TGATAACAATTGGAGCAGCCTC...GGATCGACCAAGAACCAGCA -o file_001_barcode.fastq file_001.fastq
cutadapt -g GTGTACAAATAATTGTCAAC...CTGTCTCTTATACACATCTC -o file_001_UMI.fastq file_001.fastq
seqkit concat file_001_barcode.fastq file_001_UMI.fastq > file_001_concatenation.fastq
seqkit rmdup -s file_001_concatenation.fastq -o file_001_unique_pairs.fastq
seqkit subseq -r file_001_unique_pairs.fastq > file_001_unique_barcodes.fasta
bowtie -q --suppress 1,2,4,6,7,8 -x ref_index file_001_unique_barcodes.fasta > file_001_barcodes_allignment.bowtie
sort file_001_barcodes_allignment.bowtie | uniq -c > file_001_barcode_counts.txt
awk 'BEGIN{print "Barcode,TF_variant,Code"}{print $3","$2","$1}' file_001_barcode_counts.txt > file_001_barcode_counts.csv
done

Thank you.

**Tags:** bash
> **Comments:**
> **username_1**: I assume the code is `directory_name/*`, `$file` is now by each file within `directory_name`. At the moment `$file` is simply `directory_name` as its written. There'll be bash experts here that will describe the loop for the paired end files.  
> **username_1**: Hi, please consider upvoting and accepting one of the answers. These are good answers from two different perspectives: one pulling the directory file contents; the second using a numerical loop. Both would work.  

---

## 2 answers

### Answer by username_3 on 2023-06-29. Score: 3 | Accepted answer

Here's what I use - along with an example you can modify as needed:
FASTQ_DIR="/directory/containing/FASTQs/"

for R1_FASTQ in "$FASTQ_DIR"*R1*.fastq.gz
do
                         BASE=$(basename "$R1_FASTQ" _R1.fastq.gz)
                         R2_FASTQ="${R1_FASTQ/R1/R2}"
                         R3_FASTQ="${R1_FASTQ/R1/R3}"
                         echo "BASE: $BASE"
                         echo "R1: $R1_FASTQ"
                         echo "R2: $R2_FASTQ"
                         echo "R3: $R3_FASTQ"
                         # process R1/R2 here using "$R1_FASTQ" "$R2_FASTQ" for input, and "${BASE}_XXXXXXX.fastq" for output
done

Basically, the strategy is to iterate over all the R1 FASTQs in a directory, calculate a BASE name that can be used to rename output files with arbitrary extensions (like .sam, .bam, etc...), and use BASH shell parameter expansion to calculate and set variables to the R2 (and R3 if necessary) reads present in an Illumina run that uses standard Illumina naming conventions.
This may need modification depending on the naming conventions you are using.


---

### Answer by username_2 on 2023-06-30. Score: 2

Something like this? This uses printf to create a zero-padded 3-digit number corresponding to 17 numbers from 1 to 17:
for fileBase in $(printf 'file_%03d\n' $(seq 1 17));
   do echo ${fileBase}_R1.fq.gz ${fileBase}_R2.fq.gz;
   pear -f ${fileBase}_R1.fastq.gz -r ${fileBase}_R2.fastq.gz -o ${fileBase}.fastq
   ...
done


---

