Answers.Body: <p>You can certainly use a for loop for this if desired, but an easier method is to use the <code>cat</code> command - something like this:</p>
  <pre class="lang-bash prettyprint-override"><code>cat R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L00[123]_R1_trimmed.fastq &gt; R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_R1_trimmed.fastq
  cat R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L00[123]_R2_trimmed.fastq &gt; R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_R2_trimmed.fastq
  cat R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L00[123]_R1_trimmed.fastq &gt; R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_R1_trimmed.fastq
  cat R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L00[123]_R2_trimmed.fastq &gt; R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_R2_trimmed.fastq
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-22T15:41:12.093Z
Answers.Text: Holy s**t, have I been missing out all along? TIL, thank you @Scot!
Answers.UserDisplayName: Ram RS
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-22T17:48:59.770Z
Answers.Text: I can relate, @RamRS!  I love to learn a new trick... and bash is full of them. :)
Answers.UserDisplayName: Scot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-25T15:15:16.383Z
Answers.Text: This is news to me too so I had to look this one up.  Evidently it's part of bash's "pathname expansion" feature set, just like `*` (and so, distinct from "brace expansion" in that it depends on what files are present; so like, `cat [123].txt` expands to `cat 1.txt 2.txt 3.txt` if those are present, but is just `cat [123].txt` if not, versus `{1..3}.txt` which always expands).  Very handy, thanks Scot.
Answers.UserDisplayName: Jesse
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-21T18:43:16.560Z
Answers.LastActivityDate: 2023-08-22T15:41:51.643Z
Answers.LastEditDate: 2023-08-22T15:41:51.643Z
Answers.UserDisplayName: Scot
Body: <p><em>This question was also asked on <a href="https://www.biostars.org/p/9572854/" rel="nofollow noreferrer">Biostars</a></em></p>
  <p>I have two samples where the sequencing was done on 3-lanes paired end reads. The two samples are A44943_1 and A44944_1.</p>
  <p>I would like to generate a for in loop to merge the R1 files of sample A44943_1 from the 3-lanes,  and then do the same for R2 files of this sample, then I want to merge R1 files of sample A44944_1 from the 3-lanes and then do the same for R2 files of this sample. So I thought about for in loop. My fastq files looks like:</p>
  <pre><code>R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L001_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L001_R2_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L002_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L002_R2_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H7YKTDRXY_CGCAACTA-GAATCCGA_L003_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H7YKTDRXY_CGCAACTA-GAATCCGA_L003_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L001_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L001_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L002_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L002_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H7YKTDRXY_CACAGACT-TGGTACAG_L003_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H7YKTDRXY_CACAGACT-TGGTACAG_L003_R2_trimmed.fastq
  </code></pre>
  <p>Could you advise me on  a for in loop to do this task?</p>
  <p>Thanks</p>
  
Tags: <linux>
Title: How to loop over certain files within a directory that belongs to certain samples
ThreadText: # How to loop over certain files within a directory that belongs to certain samples
  
  (Asked by: username_0 on 2023-08-21)
  
  *This question was also asked on [Biostars](https://www.biostars.org/p/9572854/)*
  
  I have two samples where the sequencing was done on 3-lanes paired end reads. The two samples are A44943\_1 and A44944\_1.
  
  I would like to generate a for in loop to merge the R1 files of sample A44943\_1 from the 3-lanes, and then do the same for R2 files of this sample, then I want to merge R1 files of sample A44944\_1 from the 3-lanes and then do the same for R2 files of this sample. So I thought about for in loop. My fastq files looks like:
  
  ```
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L001_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L001_R2_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L002_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L002_R2_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H7YKTDRXY_CGCAACTA-GAATCCGA_L003_R1_trimmed.fastq
  R0480-S0001_182602S1P1_A44943_1_H7YKTDRXY_CGCAACTA-GAATCCGA_L003_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L001_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L001_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L002_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L002_R2_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H7YKTDRXY_CACAGACT-TGGTACAG_L003_R1_trimmed.fastq
  R0480-S0002_182602S1P2_A44944_1_H7YKTDRXY_CACAGACT-TGGTACAG_L003_R2_trimmed.fastq
  
  ```
  
  Could you advise me on a for in loop to do this task?
  
  Thanks
  
  **Tags:** linux
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-08-21. Score: 5
  
  You can certainly use a for loop for this if desired, but an easier method is to use the `cat` command - something like this:
  
  ```bash
  cat R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L00[123]_R1_trimmed.fastq > R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_R1_trimmed.fastq
  cat R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_L00[123]_R2_trimmed.fastq > R0480-S0001_182602S1P1_A44943_1_H77YFDRXY_CGCAACTA-GAATCCGA_R2_trimmed.fastq
  cat R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L00[123]_R1_trimmed.fastq > R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_R1_trimmed.fastq
  cat R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_L00[123]_R2_trimmed.fastq > R0480-S0002_182602S1P2_A44944_1_H77YFDRXY_CACAGACT-TGGTACAG_R2_trimmed.fastq
  
  ```
  
  > **Comments:**
  > **username_2**: Holy s**t, have I been missing out all along? TIL, thank you @username_1!  
  > **username_1**: I can relate, @username_2!  I love to learn a new trick... and bash is full of them. :)  
  > **username_3**: This is news to me too so I had to look this one up.  Evidently it's part of bash's "pathname expansion" feature set, just like `*` (and so, distinct from "brace expansion" in that it depends on what files are present; so like, `cat [123].txt` expands to `cat 1.txt 2.txt 3.txt` if those are present, but is just `cat [123].txt` if not, versus `{1..3}.txt` which always expands).  Very handy, thanks username_1.  
  
  ---
  
  