Remove a directory with all of its contents using find under unix [closed]

偶尔善良 提交于 2019-12-11 18:21:11

问题


I am writing a ShellScript that deletes every found.??? folders with its contents. I wrote this:

find $DRIVE -name 'found.???' -type d -exec rm -r {} \;

It does what I want, but gives this error:

find: „/media/.../found.000”: No such file or directory
find: „/media/.../found.001”: No such file or directory

What can I do with this? Thanks!


回答1:


This isn't really an issue. find is going to pre-process some results and when it tries to delete some directories which have already been deleted, you will get the stderr.

You can either ignore the error messages, use -depth to make it traverse in DFS, or force it to -prune the directories. i.e.

find "$DRIVE" -mindepth 1 -depth -name 'found.???' -type d -exec rm -r {} \;

or

find "$DRIVE" -mindepth 1 -name 'found.???' -type d -prune -exec rm -r {} \;

Note, mindepth 1 is important so you don't accidentally delete "$DRIVE"




回答2:


I'll guess that you're trying to populate a FAT-based fs. Try presenting the real case of those filenames instead:

find "$DRIVE" -name 'FOUND.???' -type d -exec rm -r {} \;

And quote your variable too.



来源:https://stackoverflow.com/questions/24723499/remove-a-directory-with-all-of-its-contents-using-find-under-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!