Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何通过比较文件名和文件夹名将文件复制到同名文件夹?_Linux_Bash_Shell - Fatal编程技术网

Linux 如何通过比较文件名和文件夹名将文件复制到同名文件夹?

Linux 如何通过比较文件名和文件夹名将文件复制到同名文件夹?,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 运行代码时,出现以下

我试图比较同一文件夹中的文件名和文件夹名,以便将文件复制到具有相同名称的文件夹中

现在,这是我的代码:

测试文件/文件夹名称: 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:第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