In Unix, how do you remove everything in the current directory and below it?

前端 未结 10 1961
庸人自扰
庸人自扰 2021-01-29 21:57

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

rm -rf 

But how do you delete everything in the current d

相关标签:
10条回答
  • 2021-01-29 22:13
    rm  -rf * 
    

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

    0 讨论(0)
  • 2021-01-29 22:13

    make sure you are in the correct directory

    rm -rf *
    
    0 讨论(0)
  • 2021-01-29 22:20

    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.

    0 讨论(0)
  • 2021-01-29 22:22

    Will delete all files/directories below the current one.

    find -mindepth 1 -delete
    

    If you want to do the same with another directory whose name you have, you can just name that

    find <name-of-directory> -mindepth 1 -delete
    

    If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1. Do it without the -delete to get a list of the things that will be removed.

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-01-29 22:28

    What I always do is type

    rm -rf *
    

    and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.

    The benefits are:

    • I can review the list of files to delete before hitting ENTER.
    • The command history will not contain "rm -rf *" with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
    • It has also become handy once or twice to answer "wait a second... which files did I just delete?". The file names are visible in the terminal scrollback buffer or the command history.

    In fact, I like this so much that I've made it the default behavior for TAB with this line in .bashrc:

    bind TAB:insert-completions
    
    0 讨论(0)
提交回复
热议问题