Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • JQL
    Member
    • Apr 2011
    • 83

    shell script for copying files with consecutive numbers

    Hi all,

    I have 170 fastq files from miSeq. They are named as 1_Sxx_L001…, 2_Sxx_L001…, 3_Sxx_L001… consecutively.

    If I only need files from 1 to 120. How to quickly copy those files? I was thinking about using a for loop, but keep getting errors.

    I can try this, but inefficient and error prone.
    cp [1-9]_*.gz;
    cp [1-9][0-9]_*.gz;
    cp [1-9][0-1][0-9]_*.gz;
    cp 120.*gz

    thanks in advance.

    example file names:

    100_S122_L001_R1_001.fastq.gz 126_S145_L001_R2_001.fastq.gz 152_S168_L001_R1_001.fastq.gz 23_S71_L001_R2_001.fastq.gz 4_S36_L001_R1_001.fastq.gz 75_S34_L001_R2_001.fastq.gz
    100_S122_L001_R2_001.fastq.gz 127_S155_L001_R1_001.fastq.gz 152_S168_L001_R2_001.fastq.gz 24_S82_L001_R1_001.fastq.gz 4_S36_L001_R2_001.fastq.gz 76_S45_L001_R1_001.fastq.gz
    Last edited by JQL; 10-17-2017, 08:02 AM.
  • GenoMax
    Senior Member
    • Feb 2008
    • 7142

    #2
    Where are you copying them from or to?

    You can generate the numbers you need using a bash for loop like this easily
    Code:
    for i in `seq 1 120`; do echo $i; done

    Comment

    • JQL
      Member
      • Apr 2011
      • 83

      #3
      Those files belong to 3 projects. Lets say I want to copy the 120 files to the directory called project1.

      for i in `seq 1 120`
      do
      cp "$1_*.fastq.gz" ~/project1/ ## it doesn't work.
      done

      thanks

      Originally posted by GenoMax View Post
      Where are you copying them from or to?

      You can generate the numbers you need using a bash for loop like this easily
      Code:
      for i in `seq 1 120`; do echo $i; done

      Comment

      • GenoMax
        Senior Member
        • Feb 2008
        • 7142

        #4
        That should be a $i (not 1).
        Code:
        for i in `seq 1 120`
        do
        cp $i\_*.fastq.gz ~/project1/
        done
        Last edited by GenoMax; 10-17-2017, 08:38 AM. Reason: Escaped _

        Comment

        • JQL
          Member
          • Apr 2011
          • 83

          #5
          how come I copied all 170 files (since they are PE, so 340) over? Should be 240. Was it to due the "_"?

          $ for i in `seq 1 120`; do cp $i_*fastq.gz ~/project1/; done

          $ ls |wc -l
          340

          Comment

          • GenoMax
            Senior Member
            • Feb 2008
            • 7142

            #6
            The _ should have been escaped. Corrected code above. Replace "echo" instead of "cp" to make sure all looks good first.
            Last edited by GenoMax; 10-17-2017, 08:44 AM.

            Comment

            • JQL
              Member
              • Apr 2011
              • 83

              #7
              yes, it was the "_".

              thanks!

              $ ls | wc -l
              240

              Comment

              • JQL
                Member
                • Apr 2011
                • 83

                #8
                what does underscore mean without escaped?

                Originally posted by GenoMax View Post
                The _ should have been escaped. Corrected code above. Replace "echo" instead of "cp" to make sure all looks good first.

                Comment

                • GenoMax
                  Senior Member
                  • Feb 2008
                  • 7142

                  #9
                  Run the following to see the difference
                  Code:
                  for i in `seq 1 120`
                  do
                  echo cp $i_*.fastq.gz ~/project1/
                  done

                  Comment

                  • JQL
                    Member
                    • Apr 2011
                    • 83

                    #10
                    I ran it before. It copied all the *.gz files over (more than 2x120).
                    Interestingly, when I removed the copies in project1,
                    it copied again, removed, recopied.

                    any explanation?

                    Originally posted by GenoMax View Post
                    Run the following to see the difference
                    Code:
                    for i in `seq 1 120`
                    do
                    echo cp $i_*.fastq.gz ~/project1/
                    done

                    Comment

                    Latest Articles

                    Collapse

                    • SEQadmin2
                      From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
                      by SEQadmin2


                      Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


                      The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
                      ...
                      06-02-2026, 10:05 AM
                    • SEQadmin2
                      Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
                      by SEQadmin2


                      With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


                      Introduction

                      Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
                      05-22-2026, 06:42 AM
                    • SEQadmin2
                      Environmental Genomics in the Age of NGS: From Microbes to Conservation Strategies
                      by SEQadmin2

                      Studying ecosystems means dealing with complex, multi-species communities that are hard to observe at scale. This complexity, however, hides many important questions to be answered, from how biogeochemical cycles work and how climate change can affect species distribution to how conservation strategies can work best.


                      Genomics, particularly since the expansion of NGS, has transformed ecosystem ecology. By sequencing environmental DNA, we can now assess biodiversity without direct...
                      05-06-2026, 09:04 AM

                    ad_right_rmr

                    Collapse

                    News

                    Collapse

                    Topics Statistics Last Post
                    Started by SEQadmin2, Today, 08:59 AM
                    0 responses
                    10 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-02-2026, 12:03 PM
                    0 responses
                    21 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 06-02-2026, 11:40 AM
                    0 responses
                    17 views
                    0 reactions
                    Last Post SEQadmin2  
                    Started by SEQadmin2, 05-28-2026, 11:40 AM
                    0 responses
                    31 views
                    0 reactions
                    Last Post SEQadmin2  
                    Working...