简体   繁体   English

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

[英]bash script for copying files between directories

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. 谢谢,麻烦您了。

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 while instead of for and quoting variables that contain filenames protects against filenames that contain spaces from being interpreted as more than one filename. 使用while不是for和引用包含文件名的变量可以防止包含空格的文件名被解释为多个文件名。 Removing the rm keeps it from repeatedly producing errors and failing to copy any but the first file. 删除rm可防止重复产生错误,并且无法复制除第一个文件之外的任何错误。 The file glob for -name needs to be quoted. 需要引用文件glob for -name Habitually using lowercase variable names reduces the chances of name collisions with shell variables. 习惯上使用小写变量名称可以减少名称与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. 如您所见,不需要循环。

The correct syntax is: 正确的语法是:

DOWN="/home/myusuf3/Downloads/"
QUEUE="/home/myusuf3/.hellanzb/nzb/daemon.queue/"
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? 如果cp失败怎么办? You'll lose your file. 你会丢失你的档案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM