2018年11月5日 星期一
2018年11月2日 星期五
[Linux] find, delete 非常危險一定要注意
https://unix.stackexchange.com/questions/150628/why-does-find-delete-delete-all-files-in-a-directory-recursively
In
find
argument order matters, a lot.
Arguments can be options, tests and actions. You should typically use options first, then tests, then actions.
Sometimes
find
even warns you about a possible bad ordering (for example when you use -maxdepth
after other arguments), but others it seems it doesn't.
What
find . -delete -name '*ar'
does is:- Finds files and directories upon current directory.
- Deletes them ALL as they are found!
- Then sees if they are named like '*ar' (this part has no effect now).
What you probably want to do is:
find -name '*ar' -delete
This will, for every file, see if it matches
'*ar'
, and only if it satisfies the condition it will delete the file.
Sorry if you found out too late.
訂閱:
文章 (Atom)