2016-01-31 30 views
0

這應該如何解決?我下面a tutorial但我收到此錯誤:linux find:通過xargs移動文件

$ find ~/Desktop -name 「*.jpg」 -o -name 「*.gif」 -o -name 「*.png」 -print0 | xargs -0 mv –target-directory ~/Pictures 
mv: cannot stat `–target-directory': No such file or directory 

*我感興趣的有關如何使用xargs的執行這個命令!

+0

如果你想所有的圖像文件從桌面移動到一個特定的目錄只使用'-exec MV \ {\} 〜/圖片\;'在find的末尾(沒有管道和xargs和print0) – pouyan

+0

你正在使用[tag:linux]標籤,但你真的使用Linux(和GNU'xargs'和'mv')嗎? BSD版本(也在[Mac OS X]上(https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mv.1.html))有不同的選項。 – e0k

回答

2

使用find和exec

$ find ~/Desktop -name "*.jpg" -exec mv '{}' /tmp/target/ \; -or -name "*.gif" -exec mv '{}' /tmp/target/ \; -or -name "*.png" -exec mv '{}' /tmp/target/ \; 

使用xargs的

$ find ~/Desktop -name "*.jpg" -or -name "*.gif" -or -name "*.png" | xargs -I SRCFILE mv SRCFILE /tmp/target/ 
+0

@ mona-jalal這是你正在尋找的答案嗎? – noorul

1

我相信-target-directory應該是--target-directory,或只是-t

+0

不知道缺少什麼:'find〜/ Desktop -name「* .jpg」-o -name「* .gif」-o -name「* .png」-print0 | xargs -0 mv -t〜/圖片 mv:缺少文件操作數 嘗試使用'mv --help'以獲取更多信息.' –

+0

對於它的價值,我認爲'-name'的引號引起'find'不要返回任何東西。嘗試刪除它們 – lanceg

+0

引號會阻止shell中的通配符擴展。你想給星號「find」。 – e0k

2

你並不需要使用xargsfind可以在比賽中執行命令:

find ~/Desktop -name 「*.jpg」 -o -name 「*.gif」 -o -name 「*.png」 -exec mv \{\} ~/Pictures \; 

可以-exec後逃脫的分號\;之前發出命令。 \{\}被替換爲匹配的文件名。

man find

-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

注意,分號和{}必須進行轉義。

+0

好吧,我主要關心如何在這裏使用xargs! –

相關問題