Linux 如何通过比较文件名和文件夹名将文件复制到同名文件夹?
我试图比较同一文件夹中的文件名和文件夹名,以便将文件复制到具有相同名称的文件夹中 现在,这是我的代码: 测试文件/文件夹名称: sth(文件夹)sth2.fastq sth.fastqLinux 如何通过比较文件名和文件夹名将文件复制到同名文件夹?,linux,bash,shell,Linux,Bash,Shell,我试图比较同一文件夹中的文件名和文件夹名,以便将文件复制到具有相同名称的文件夹中 现在,这是我的代码: 测试文件/文件夹名称: 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 运行代码时,出现以下
for fname in *.fastq; do
for f in */; do
if "${fname%.*}"=="$f"; then
-exec cp $fname /${f} \;
fi
done
done
运行代码时,出现以下错误:
./script.sh:第3行:sth2==sth/:没有这样的文件或目录
/script.sh:第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
希望能有所帮助这正是我所需要的。非常感谢你的回答。我将您的代码用作:用于.fastq中的fname;do locfname=${fname%.}如果[[!-d$locfname]];然后mkdir$locfname fi mv$fname$locfname done