![](/img/trans.png)
[英]Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them
[英]Move command error target is not directory UNIX
我是 unix 的新手,我正在嘗試編寫一個 bash,將所有以.c
結尾的文件移動到另一個文件夾。 當我執行命令時: find ~/testfiles -name '*.c' -exec mv -i ~/destination {} +
我得到一個錯誤: mv: target '/home/user/testfiles/dir3/sourcefile1.c' is not a directory.
testfiles
文件夾包括文件和文件夾。 如果有幫助,我正在使用 ubuntu 20.04..感謝您的時間。
在find -exec
, {}
被find -exec
的文件/目錄替換。 在您的示例中使用的mv
命令試圖將第一個參數移動到第二個參數,因此您的參數順序錯誤。
這是正確的:
[[ -d ~/destination ]] && find ~/testfiles -name '*.c' -exec mv -i '{}' ~/destination \;
稍微更有效的方法是使用mv
s -t
(target) 選項,這意味着您可以在find
使用+
(一次向mv
提供盡可能多的參數):
[[ -d ~/destination ]] && find ~/testfiles -name '*.c' -exec mv -i -t ~/destination '{}' +
我在find
命令之前添加了[[ -d ~/destination ]] &&
,因為一個常見的錯誤是將大量文件移動到同一個目的地,如果它不存在(或者是一個文件而不是目錄)它'將創建一個名為destination
的文件,並用所有文件覆蓋它,使您丟失所有文件(最后一個移動的除外)。
從+
到\\;
的轉換說明\\;
in -exec
直接取自我的man find
頁面:
-exec command ;
find 的所有以下參數都被視為命令的參數,直到參數由
;
遇到了。 字符串{}
被當前正在處理的文件名替換,它出現在命令的參數中的任何地方,...指定的命令對每個匹配的文件運行一次。 ...
-exec command {} +
-exec
動作的這個變體在選定的文件上運行指定的命令,但命令行是通過在末尾附加每個選定的文件名來構建的; 命令行的構建方式與xargs
構建其命令行的方式非常相似。 命令中只允許一個{}
實例,...
請注意,語法有所不同,第二個版本的{} +
必須位於末尾(附加文件的位置)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.