I have a folder containing *.fastq.gz
files (R1 and R2) for many samples. For example, in the folder "Raw_WGS", I have several files like
Sample_1_R1.fastq.gz, Sample_1_R2.fastq.gz, Sample_2_R1.fastq.gz, Sample_2_R2.fastq.gz, Sample_3_R1.fastq.gz, Sample_3_R2.fastq.gz
I have another folder e.g. "Analysis" where I have different sub-folders according to the name of my sample sequences. I have sub-folders named like
Sample_1, Sample_2, Sample_3
I have a Text file containing all and ONLY the names of my samples i.e. Sample_1, Sample_2, Sample_3
. Now, I want to move the *_R1.fastq.gz
and *_R2.fastq.gz
files for each sample to their respective sub-folder according to the name in the "Analysis" folder.
Can you please tell me how can I do that for all the samples at once? I can use the mv
command to move each file at a time. But I have 1000s of files. So, I want to move them all by running a single script. Please let me know if you have any suggestions.
-
Loop across the directory names, and move matching filesChris Davies– Chris Davies2021年02月28日 11:42:15 +00:00Commented Feb 28, 2021 at 11:42
-
This might help unix.stackexchange.com/questions/527073/…Andre Wildberg– Andre Wildberg2021年02月28日 11:49:44 +00:00Commented Feb 28, 2021 at 11:49
2 Answers 2
If I understand you correctly, you don't need a file with the names, since they are already in the file names. Try this:
for i in *_R{1,2}.fastq.gz; do
# use `cp` to be sure it works
cp "$i" /path/toAnalisys/"${i%_*}"
done
Assuming Samples.txt contains Sample_1 till 3 one per lne , we can setup an xargs command to call mv
tree -F
< Samples.txt \
xargs -I {} -t mv -t Analysis/{}/ \
Raw_WGS/{}_R1.fastq.gz \
Raw_WGS/{}_R2.fastq.gz ;
tree -F
Results:
Before move...
.
├── Analysis/
│ ├── Sample_1/
│ ├── Sample_2/
│ ├── Sample_3/
│ ├── Sample_4/
│ └── Sample_5/
└── Raw_WGS/
├── Sample_1_R1.fastq.gz
├── Sample_1_R2.fastq.gz
├── Sample_2_R1.fastq.gz
├── Sample_2_R2.fastq.gz
├── Sample_3_R1.fastq.gz
└── Sample_3_R2.fastq.gz
After move...
.
├── Analysis/
│ ├── Sample_1/
│ │ ├── Sample_1_R1.fastq.gz
│ │ └── Sample_1_R2.fastq.gz
│ ├── Sample_2/
│ │ ├── Sample_2_R1.fastq.gz
│ │ └── Sample_2_R2.fastq.gz
│ ├── Sample_3/
│ │ ├── Sample_3_R1.fastq.gz
│ │ └── Sample_3_R2.fastq.gz
│ ├── Sample_4/
│ └── Sample_5/
└── Raw_WGS/