bash 用于在目录之间复制文件的bash脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3331348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:22:58  来源:igfitidea点击:

bash script for copying files between directories

bash

提问by myusuf3

I am writing the following script to copy *.nzb files to a folder to queue them for Download.

我正在编写以下脚本将 *.nzb 文件复制到一个文件夹中,以便将它们排队等待下载。

I wrote the following script

我写了以下脚本

#!/bin/bash

#This script copies NZB files from Downloads folder to HellaNZB queue folder.

${DOWN}="/home/user/Downloads/"
${QUEUE}="/home/user/.hellanzb/nzb/daemon.queue/"


for a in $(find ${DOWN}  -name  *.nzb)
do
cp ${a} ${QUEUE}
rm *.nzb
done

it gives me the following error saying:

它给了我以下错误说:

HellaNZB.sh: line 5: =/home/user/Downloads/: No such file or directory
HellaNZB.sh: line 6: =/home/user/.hellanzb/nzb/daemon.queue/: No such file or directory

Thing is that those directories exsist, I do have right to access them.

事实是这些目录存在,我确实有权访问它们。

Any help would be nice.

你能帮忙的话,我会很高兴。

Please and thank you.

谢谢,麻烦您了。

回答by Paused until further notice.

Variable names on the left side of an assignment should be bare.

赋值左侧的变量名应该是空的。

foo="something"
echo "$foo"

Here are some more improvements to your script:

以下是对您的脚本的更多改进:

#!/bin/bash

#This script copies NZB files from Downloads folder to HellaNZB queue folder.

down="/home/myusuf3/Downloads/"
queue="/home/myusuf3/.hellanzb/nzb/daemon.queue/"

find "${down}" -name "*.nzb" | while read -r file
do
    mv "${file}" "${queue}"
done

Using whileinstead of forand quoting variables that contain filenames protects against filenames that contain spaces from being interpreted as more than one filename. Removing the rmkeeps it from repeatedly producing errors and failing to copy any but the first file. The file glob for -nameneeds to be quoted. Habitually using lowercase variable names reduces the chances of name collisions with shell variables.

使用while代替for和引用包含文件名的变量可以防止包含空格的文件名被解释为多个文件名。删除rm它可以防止重复产生错误并且无法复制除第一个文件之外的任何文件。-name需要引用文件 glob 。习惯性地使用小写变量名可以减少与 shell 变量发生名称冲突的机会。

If all your files are in one directory (and not in multiple subdirectories) your whole script could be reduced to the following, by the way:

顺便说一下,如果您的所有文件都在一个目录中(而不是在多个子目录中),您的整个脚本可以简化为以下内容:

mv /home/myusuf3/Downloads/*.nzb /home/myusuf3/.hellanzb/nzb/daemon.queue/

If you do have files in multiple subdirectories:

如果您确实在多个子目录中有文件:

find /home/myusuf3/Downloads/ -name "*.nzb" -exec mv {} /home/myusuf3/.hellanzb/nzb/daemon.queue/ +

As you can see, there's no need for a loop.

如您所见,不需要循环。

回答by David Harris

The correct syntax is:

正确的语法是:

DOWN="/home/myusuf3/Downloads/"
QUEUE="/home/myusuf3/.hellanzb/nzb/daemon.queue/"

回答by reinierpost

for a in $(find ${DOWN}  -name  *.nzb)
   # escape the * or it will be expanded in the current directory
   # let's just hope no file has blanks in its name
do
  cp ${a} ${QUEUE}  # ok, although I'd normally add a -p
  rm *.nzb          # again, this is expanded in the current directory
                    # when you fix that, it will remove ${a}s before they are copied

done

Why don't you just use rm $(a}?

为什么不直接使用 rm $(a}?

Why use a combination of cp and rm anyway, instead of mv?

无论如何,为什么要使用 cp 和 rm 的组合,而不是 mv?

Do you realize all files will end up in the same directory, and files with the same name from different directories will overwrite each other?

您是否意识到所有文件最终都会在同一目录中,并且来自不同目录的同名文件会相互覆盖?

What if the cp fails? You'll lose your file.

cp失败了怎么办?你会丢失你的文件。