Sunday, March 6, 2011

What is the safest way to empty a directory in *nix?

I'm scared that one day, I'm going to put a space or miss out something in the command I currently use:

rm -rf ./*

Is there a safer way of emptying the current directory's contents?

From stackoverflow
  • You could always turn on -i which would prompt you on every file, but that would be really time consuming for large directories.

    I always do a pwd first.

    I'll even go as far as to create an alias so that it forces the prompt for my users. Red Hat does that by default, I think.

  • You could drop the `f' switch and it should prompt you for each file to make sure you really want to remove it.

  • Go one level up and type in the directory name

    rm -rf <dir>/*
    
    Filip Ekberg : If you already are in / and do cd .. rm -rf /* You will get owned. ;)
    Pyrolistical : Are we typing or are we sending commands from a script?
    OscarRyz : use ALWAYS the directory name you want to delete! Never work as root.
  • The safest way is to sit on your hands before pressing Enter.

    That aside, you could create an alias like this one (for Bash)

    alias rm="pwd;read;rm"
    

    That will show you your directory, wait for an enter press and then remove what you specified with the proper flags. You can cancel by pressing ^C instead of Enter.

    titaniumdecoy : That's nasty; it encourages you to assume that you'll be given a second chance with the rm command. Wait until you move to another machine or are logged in as another user, and *bam*...
    titaniumdecoy : It would be better to do something along the lines of alias alias delete="..."
    Vinko Vrsalovic : Yes, using a different name is probably a better idea. But still, nothing will replace proper care (you can get so accustomed to press enter twice that it'll do you no good when you most needed it)
    Christian.K : +1 for the "sit on your hands" thing.
  • I use one of:

    rm -fr .
    
    cd ..; rm -fr name-of-subdirectory
    

    I'm seldom sufficiently attached to a directory that I want to get rid of the contents but must keep the directory itself.

    Christian.K : I'd even go as far and using "cd .. && rm -fr name-of-subdir" if for some insane reason the "cd .." should fail, you might delete the wrong directory (if it exists at both levels) - quite paranoid, isn't it ;-)
    Jonathan Leffler : @Christian K: I would normally use two separate interactive commands rather than actually separating them with a semi-colon. Your revision is safer. There are times when 'cd ..' can fail; one is when someone has already removed the directory; another is when you don't have permission (any more).
    Jonathan Leffler : @Christian K: If your shell supports it, then 'cd .. && rm -fr $OLDPWD' is pretty safe. Using 'rm -fr ~-' would have dire consequences if you accidentally put a space between the tilde and the dash, so it is less safe.
  • If you want to be really safe, you could create a simple alias or shell script like:

    mv $1 ~/.recycle/

    This would just move your stuff to a .recycle folder (hello, Windows!).

    Then set up a cron job to do rm -rf on stuff in that folder that is older than a week.

  • When using rm -rf I almost always use the fully qualified path.

  • Here is a safer way: use ls first to list the files that will be affected, then use command-line history or history substitution to change the ls to rm and execute the command again after you are convinced the correct files will be operated on.

  • I think this is a reasonable way:

    find . -maxdepth 1 \! -name . -print0 | xargs -0 rm -rf
    

    and it will also take care of hidden files and directories. The slash isn't required after the dot and this then will also eliminate the possible accident of typing . /.

    Now if you are worried what it will delete, just change it into

    find . -maxdepth 1 \! -name . -print  | less
    

    And look at the list. Now you can put it into a function:

    function enum_files { find . -maxdepth 1 \! -name . "$@"; }
    

    And now your remove is safe:

    enum_files | less                     # view the files
    enum_files -print0 | xargs -0 rm -rf  # remove the files
    

    If you are not in the habit of having embedded new-lines in filenames, you can omit the -print0 and -0 parameters. But i would use them, just in case :)

  • If what you want to do is to blow away an entire directory there is always some level of danger associated with that operation. If you really want to be sure that you are doing the right thing you could always do a move operation to some place like /tmp, wait for some amount of time to make sure that everything is okay with the "deletion" in place. Then go into the /tmp directory and ONLY use relative paths for a forced and recursive remove operation. Additional, in the move do a rename to "delete-directoryname" to make it easier not to make a mistake.

    For example I want to delete /opt/folder so I do:

    mv /opt/folder /tmp/delete-folder

    .... wait to be sure everything is okay - maybe a minute, maybe a week ....

    cd /tmp

    pwd

    rm -rf delete-folder/

    The most important tip for doing an rm -rf is to always use relative paths. This keeps you from ever having typed a / before having completed your typing.

  • There's a reason I have [tcsh]:

    alias clean  '\rm -i -- "#"* *~'
    alias rmo    'rm -- *.o'
    

    They were created the first time I accidentally put a space between the * and the .o. Suffice to say, what happened wasn't what I expected to happen...

    But things could have been worse. Back in the early '90s, a friend of mine had a ~/etc directory. He wanted to delete it. Unfortunately he typed rm -rf /etc. Unfortunately, he was logged in as root. He had a bad day!


    To be evil: touch -- '-rf *'

    To be safe, use '--' and -i. Or get it right once and create an alias!

0 comments:

Post a Comment