簡體   English   中英

bash 腳本中的 find 命令導致“沒有這樣的文件或目錄”錯誤僅適用於目錄?

[英]find command in bash script resulting in “No such file or directory” error only for directories?

更新 2014-03-21

所以我意識到我的效率並沒有達到我所能達到的水平,因為我需要“清理”的所有磁盤都在/media並命名為“ disk1disk2disk3等”。 這是最終的腳本:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM