BASH:将配对结束文件复制到子目录的脚本

时间:2018-03-02 19:53:30

标签: bash

问题:我有一个配对文件的文件夹,我需要将这些文件组织到子目录中。

我的档案示例:

SampleName1_S1_L001_R1_001.fastq.gz
SampleName1_S1_L001_R2_001.fastq.gz
SampleName2_S2_L001_R1_001.fastq.gz
SampleName2_S2_L001_R2_001.fastq.gz
...

我希望脚本创建子目录并移动文件,如:

SampleName1_S1_Analysis/
SampleName1_S1_L001_R1_001.fastq.gz SampleName1_S1_L001_R2_001.fastq.gz

SampleName2_S2_Analysis/
SampleName2_S2_L001_R1_001.fastq.gz SampleName2_S2_L001_R2_001.fastq.gz

...

我一直在寻找类似的线程,但似乎没有什么能满足我的问题。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

我认为rename是这里使用的工具:

rename --dry-run -p 's|_L.*|_Analysis/$_|' *gz

示例输出

'SampleName1_S1_L001_R1_001.fastq.gz' would be renamed to 'SampleName1_S1_Analysis/SampleName1_S1_L001_R1_001.fastq.gz'
'SampleName1_S1_L001_R2_001.fastq.gz' would be renamed to 'SampleName1_S1_Analysis/SampleName1_S1_L001_R2_001.fastq.gz'
'SampleName2_S2_L001_R1_001.fastq.gz' would be renamed to 'SampleName2_S2_Analysis/SampleName2_S2_L001_R1_001.fastq.gz'
'SampleName2_S2_L001_R2_001.fastq.gz' would be renamed to 'SampleName2_S2_Analysis/SampleName2_S2_L001_R2_001.fastq.gz'

如果输出看起来不错,请在没有--dry-run的情况下再次运行。

-p可以根据需要随时生成输出目录。

当前文件的名称将作为$_传递给中间的小Perl脚本。然后,脚本会修改$_rename会将其用作文件的新名称。我只是在_L之后删除所有内容,然后将_Analysis/附加到目录名的根目录。

答案 1 :(得分:-1)

您不需要这样的脚本,只需按此顺序执行此命令:

已编辑(我想这可能会有所帮助):

#!bin/bash
for file in *.fastq.gz; do
 if [ -e "$file" ]; then
  name=$(echo "$file" | cut -d'_' -f1,2)
  mkdir "${name}_Analysis" && mv "${name}"*.fastq.gz "${name}_Analysis"
 fi;
done;