[英]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 thatxargs
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.