简体   繁体   English

移动命令错误目标不是目录 UNIX

[英]Move command error target is not directory UNIX

I am new to unix and I am trying to write a bash that moves all files that ends to .c to another folder.我是 unix 的新手,我正在尝试编写一个 bash,将所有以.c结尾的文件移动到另一个文件夹。 When I'm executing command: find ~/testfiles -name '*.c' -exec mv -i ~/destination {} + I got an error: mv: target '/home/user/testfiles/dir3/sourcefile1.c' is not a directory.当我执行命令时: find ~/testfiles -name '*.c' -exec mv -i ~/destination {} +我得到一个错误: mv: target '/home/user/testfiles/dir3/sourcefile1.c' is not a directory.

The testfiles folder includes files and folders. testfiles文件夹包括文件和文件夹。 I am using ubuntu 20.04 if that helps..Thanks for your time.如果有帮助,我正在使用 ubuntu 20.04..感谢您的时间。

In find -exec the {} is replaced by the file/directory found.find -exec{}find -exec的文件/目录替换。 The mv command used as in your example is trying to move the first argument to the second argument so your arguments are in the wrong order.在您的示例中使用的mv命令试图将第一个参数移动到第二个参数,因此您的参数顺序错误。

This would be correct:这是正确的:

[[ -d ~/destination ]] && find ~/testfiles -name '*.c' -exec mv -i '{}' ~/destination \;

A slightly more effetive way would be to use mv s -t (target) option which means that you can use the + in find (to supply as many arguments to mv in one go as possible):稍微更有效的方法是使用mv s -t (target) 选项,这意味着您可以在find使用+ (一次向mv提供尽可能多的参数):

[[ -d ~/destination ]] && find ~/testfiles -name '*.c' -exec mv -i -t ~/destination '{}' +

I added [[ -d ~/destination ]] && before the find command since one common mistake is to move a lot of files to the same destination and if it doesn't exist (or is a file and not a directory) it'll create a file named destination and overwrite that with all the files making you loose all your files (except the last one moved).我在find命令之前添加了[[ -d ~/destination ]] && ,因为一个常见的错误是将大量文件移动到同一个目的地,如果它不存在(或者是一个文件而不是目录)它'将创建一个名为destination的文件,并用所有文件覆盖它,使您丢失所有文件(最后一个移动的除外)。

Explanation for the shift from + to \\;+\\;的转换说明\\; in -exec taken directly from my man find page: in -exec直接取自我的man find页面:

-exec command ;

All following arguments to find are taken to be arguments to the command until an argument consisting of ; find 的所有以下参数都被视为命令的参数,直到参数由; is encountered.遇到了。 The string {} is replaced by the current file name being processed everywhere it occurs in the arguments to the command, ... The specified command is run once for each matched file.字符串{}被当前正在处理的文件名替换,它出现在命令的参数中的任何地方,...指定的命令对每个匹配的文件运行一次。 ... ...

-exec command {} +

This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end ; -exec动作的这个变体在选定的文件上运行指定的命令,但命令行是通过在末尾附加每个选定的文件名来构建 The command line is built in much the same way that xargs builds its command lines.命令行的构建方式与xargs构建其命令行的方式非常相似。 Only one instance of {} is allowed within the command, ...命令中只允许一个{}实例,...

Note that the syntax differs and that {} + must be at the end (where it's appending files) for the second version.请注意,语法有所不同,第二个版本的{} +必须位于末尾(附加文件的位置)。

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

相关问题 创建 Unix shell 脚本以将非空文件从源目录移动到目标目录并为其添加时间戳 - Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them 目标目录名称中带有“ - =”的cd命令出错 - Error with cd Command with “-=” in the Target Directory's Name 如何使用单个命令在UNIX文件系统中提取JAR并使用JAR命令指定其目标目录? - How do you extract a JAR in a UNIX filesystem with a single command and specify its target directory using the JAR command? Linux命令移动目录 - Linux command to move a directory Unix查找命令目录提示 - Unix find command directory hints 关于UNIX Move命令覆盖保护 - Regarding UNIX Move Command Override Protection Unix脚本/从变量中替换文件中的字符串并移至另一个目录 - Unix script / replace string in file from variable and move into another directory 需要使用 unix 脚本将文件名移动到带有文件路径的目录中 - Need to move file names in a directory with path to a file using unix script ELF二进制错误Unix(“./X没有这样的文件或目录”) - ELF Binary Error Unix (“./X No such file or directory”) Linux:如果目录存在于目标位置,如何移动和重命名目录? - Linux : how to move and rename the directory if it exist in the target location?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM