如何使用bash终端脚本将带有数字关键字的文件移动到相应的文件夹

如何使用bash终端脚本将带有数字关键字的文件移动到相应的文件夹

我有一个关于如何根据关键字将文件移动到特定文件夹的问题。比如说,我有一堆文件: R01.fastq.gz R02.fastq.gz R03.fastq.gz R04.fastq.gz ...我有一堆文件夹:data01 data02 data03 data04 ...我想将R01.fastq.gz放入data01,R02.fastq.gz放入data02,R03.fastq.gz放入data03,R04.fastq.gz放入data04......那么如何使用循环脚本来做到这一点呢?多谢!

答案1

# use brace expansion to create a list 00,01,02,[...] 98,99
for i in {0..9}{0..9} ; do
  # defining file and directory as vars as we will use them a couple of times each
  F=R$i.fastq.gz
  D=data$i/
  # if F does not exist, or is not a file, do nothing
  [ -f $F ] || continue
  # create any needed directories and move files if created OK (mkdir
  # will fail if D exists but is not a directory)
  mkdir -p $D && mv $F $D
done

相关内容