如何根据前缀将文件移动到不同的文件夹?
How to move files to different folders based on their prefix?
- 我的主文件夹中有数千个文件,如下所示:
> ls
6_2_S28_R1_001.fastq.gz
19_1_S160_R1_001.fastq.gz
25_3_S114_R1_001.fastq.gz
这里作为例子,我分解文件名6_2_S28_R1_001.fastq.gz;
6_ = 第 6 天
2_ = 复制 2
S28_R1_001.fastq.gz = 扩展
在我有文件夹的同一目录中
rep1
rep2
rep3
我的目标
我正在尝试根据与复制相关的前缀将每个文件移动到不同的文件夹
6_2_S28_R1_001.fastq.gz, #this file needs to be moved to rep2 <br>
19_1_S160_R1_001.fastq.gz, #this file needs to be moved to rep1 <br>
25_3_S114_R1_001.fastq.gz, #thus file needs to go to rep3 folder <br>
我试过一个不成功的for循环。
感谢任何帮助或指导
#! /bin/bash
for f in *_*_*_*_*.gz; do
i="${f#*_}"; i="${i%%_*}"
d="rep$i"
echo "Move '$f' to '$d'"
mkdir -p "$d" \
&& mv "$f" "$d"
done
- 我的主文件夹中有数千个文件,如下所示:
> ls
6_2_S28_R1_001.fastq.gz
19_1_S160_R1_001.fastq.gz
25_3_S114_R1_001.fastq.gz
这里作为例子,我分解文件名6_2_S28_R1_001.fastq.gz;
6_ = 第 6 天
2_ = 复制 2
S28_R1_001.fastq.gz = 扩展在我有文件夹的同一目录中
rep1
rep2
rep3
我的目标
我正在尝试根据与复制相关的前缀将每个文件移动到不同的文件夹
6_2_S28_R1_001.fastq.gz, #this file needs to be moved to rep2 <br>
19_1_S160_R1_001.fastq.gz, #this file needs to be moved to rep1 <br>
25_3_S114_R1_001.fastq.gz, #thus file needs to go to rep3 folder <br>
我试过一个不成功的for循环。 感谢任何帮助或指导
#! /bin/bash
for f in *_*_*_*_*.gz; do
i="${f#*_}"; i="${i%%_*}"
d="rep$i"
echo "Move '$f' to '$d'"
mkdir -p "$d" \
&& mv "$f" "$d"
done