![](/img/trans.png)
[英]Bash script for searching of files (file types) which are defined in the external file recursively in directory using find command
[英]find command in bash script resulting in “No such file or directory” error only for directories?
更新 2014-03-21
所以我意識到我的效率並沒有達到我所能達到的水平,因為我需要“清理”的所有磁盤都在/media
並命名為“ disk1
、 disk2
、 disk3
等”。 這是最終的腳本:
DIRTY_DIR="/media/disk*"
find $DIRTY_DIR -depth -type d -name .AppleDouble -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDB -exec rm -rf {} \;
find $DIRTY_DIR -depth -type d -name .AppleDesktop -exec rm -rf {} \;
find $DIRTY_DIR -type f -name ".*DS_Store" -exec rm -f {} \;
find $DIRTY_DIR -type f -name ".Thumbs.db" -exec rm -f {} \; # I know, I know, this is a Windows file.
接下來可能會更進一步地清理代碼,並添加諸如日志記錄和報告結果之類的功能(通過電子郵件或其他方式); 不包括系統和目錄; 並允許人們自定義文件/目錄列表。
感謝所有的幫助!
更新
在我吸收大家提供的有用建議之前,我進行了一些測試,測試結果非常有趣(見下文)。
作為測試,我運行了以下命令:
root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec echo rm -rf {} \;
結果(這是我的預期):
rm -rf /media/disk3/Videos/Chorus/.AppleDouble
但是,當我運行實際命令(沒有echo
)時:
root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;
我收到了相同的“錯誤”輸出:
find: `/media/disk3/Videos/Chorus/.AppleDouble': No such file or directory
我將“錯誤”放在引號中,因為顯然該文件夾已被刪除,通過立即運行驗證:
root@doi:~# find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;
root@doi:~#
似乎find
命令存儲了原始結果,通過刪除目錄對其進行了操作,但隨后又嘗試將其刪除? 還是rm
的-f
選項(應該用於忽略不存在的文件和參數)被忽略了? 我注意到,當我單獨使用rm
命令而不使用find
命令運行測試時,一切都按預期工作。 因此,直接運行rm -rf ... \\nonexistent_directory
nonexistent_directory ,即使“non_existent_directory”不存在,也不會返回任何錯誤,並且直接運行rm -r \\nonexistent_directory
nonexistent_directory 提供了預期的:
rm: cannot remove 'non_existent_directory': No such file or directory
我應該使用-delete
選項而不是-exec rm ...
選項嗎? 我本來想使腳本廣泛適用盡可能為那些沒有系統-delete
供選擇find
。
最后,我不認為/media/disk1
, /media/disk2
,...是否在/media/storage
下的 AUFS 文件系統中合並並不重要,因為find
命令本身在單個磁盤上運行? 感謝到目前為止所有的幫助,伙計們。 完成后我會發布腳本。
原帖
我正在編寫一個 bash 腳本來刪除我的 Lubuntu 文件共享上的一些 OS X 殘余。 但是,在執行此操作時:
...
BASE_DIR="/media/disk" # I have 4 disks: disk1, disk2, ...
COUNTER=1
while [ $COUNTER -lt 5 ]; do # Iterate through disk1, disk2, ...
DIRTY_DIR=${BASE_DIR}$COUNTER # Look under the current disk counter /media/disk1, /media/disk2, ...
find $DIRTY_DIR -name \.AppleDouble -exec rm -rf {} \; # Delete all .AppleDouble directories
find $DIRTY_DIR -name ".*DS_Store" -exec rm -rf {} \; # Delete all .DS_Store and ._.DS_Store files
COUNTER=$(($COUNTER+1))
done
...
我看到以下輸出:
find: /media/disk1/Pictures/.AppleDouble
: 沒有/media/disk1/Pictures/.AppleDouble
文件或目錄
在我添加-exec rm ...
部分之前,腳本找到了/media/disk1/Pictures/.AppleDouble
目錄。 該腳本可以正常刪除 DS_Store 文件,但是對於目錄的 find 命令我缺少什么?
我害怕用-exec
部分搞砸太多,因為我不想錯誤地刪除目錄。
tl;dr - Pass -prune
如果您使用find
刪除目錄。
對於任何偶然發現這個問題的人。 運行一個這樣的例子
find /media/disk3 -type d -name .AppleDouble -exec rm -rf {} \;
導致錯誤如
rm: cannot remove 'non_existent_directory': No such file or directory
使用 find 查找和刪除目錄時,經常會遇到此錯誤,因為find
存儲目錄以處理子目錄,然后使用exec
刪除它,然后嘗試遍歷不再存在的子目錄。
您可以通過-maxdepth 0
或-prune
來防止此問題。 像這樣:
find /media/disk3 -type d -name .AppleDouble -prune -exec rm -rf {} \;
現在它刪除目錄沒有任何錯誤。 歡呼! :)
您不需要在 shell glob 中轉義 DOT,因為這不是正則表達式。 所以使用.AppleDouble
而不是\\.AppleDouble
:
find $DIRTY_DIR -name .AppleDouble -exec rm -rf '{}' \;
PS:我在你的腳本中沒有看到任何地方增加了$COUNTER
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.