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

前端 未结 10 1962
庸人自扰
庸人自扰 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:28

    Use

    rm -rf *
    

    Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and ... Use the wildcard globbing instead. But this can be risky.

    A safer version IMO is to use:

    rm -ri * 
    

    (this prompts you for confirmation before deleting every file/directory.)

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

    It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot (.) means the current directory. be carefull not to do rm -rf .. since the double dot (..) means the previous directory.

    This being said, if you are like me and have multiple terminal windows open at the same time, you'd better be safe and use rm -ir . Lets look at the command arguments to understand why.

    First, if you look at the rm command man page (man rm under most Unix) you notice that –r means "remove the contents of directories recursively". So, doing rm -r . alone would delete everything in the current directory and everything bellow it.

    In rm –rf . the added -f means "ignore nonexistent files, never prompt". That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that's really what you want.

    On the otherhand, in rm -ri . the -i that replaces the -f means "prompt before any removal". This means you'll get a chance to say "oups! that's not what I want" before rm goes happily delete all your files.

    In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That's why I now employ rm -ri now.

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

    This simplest safe & general solution is probably:

    find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf
    
    0 讨论(0)
  • 2021-01-29 22:39

    How about:

    rm -rf "$(pwd -P)"/* 
    
    0 讨论(0)
提交回复
热议问题