In Unix, how do you remove everything in the curre

2019-03-07 17:18发布

I know this will delete everything in a subdirectory and below it:

rm -rf <subdir-name>

But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?

10条回答
贼婆χ
2楼-- · 2019-03-07 17:51
rm  -rf * 

Don't do it! It's dangerous! MAKE SURE YOU'RE IN THE RIGHT DIRECTORY!

查看更多
贪生不怕死
3楼-- · 2019-03-07 17:51

This simplest safe & general solution is probably:

find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf
查看更多
萌系小妹纸
4楼-- · 2019-03-07 17:51

I believe this answer is better:

https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

basically you go up one level, and then say delete everything inside X directory. This way you are still specifying what folder should have its content deleted, which is safer than just saying 'delete everything here", while preserving the original folder, (which sometimes you want to because you aren't allowed or just don't want to modify the folder's existing permissions)

查看更多
Viruses.
5楼-- · 2019-03-07 17:59

Practice safe computing. Simply go up one level in the hierarchy and don't use a wildcard expression:

cd ..; rm -rf -- <dir-to-remove>

The two dashes -- tell rm that <dir-to-remove> is not a command-line option, even when it begins with a dash.

查看更多
登录 后发表回答