[英]How to copy files to folders with same name by comparing file names with folder names?
我试图比较同一文件夹中的文件和文件夹名称将文件复制到具有相同名称的文件夹。
现在这是我的代码:
测试文件/文件夹名称:sth(文件夹)sth2.fastq sth.fastq
for fname in *.fastq; do
for f in */; do
if "${fname%.*}"=="$f"; then
-exec cp $fname /${f} \;
fi
done
done
当我运行代码时出现此错误:
./script.sh:line 3:sth2 == sth /:没有这样的文件或目录./script.sh:line 3:sth == sth /:没有这样的文件或目录
谢谢你的帮助。
让我们这样说:
for fname in *.fastq; do
#Getting rid of the extension
locfname=${fname%.*}
#Getting rid of numbers if any
locdirname=${locfname//[0-9]*}
#Creating the directory if it doesn't exist
if [[ ! -d $locdirname ]]; then
mkdir $locdirname
fi
#Moving file in the proper directory (can use cp instead)
mv $fname $locdirname
done
希望它可能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.