rm on steroids

June 28th, 2006

If you run Linux and have been working with the filesystem(s) for any length of time, you have used and abused rm. However, when the number of files concerned is very large or there are unescaped characters in your filenames, rm can misbehave. The following code will take care of all your worries. The exec command always makes me stand back and wonder at the power of the Linux shell.
find . -name '*.*' -exec rm {} \;

Also, Veen overhears an rm conversation

[EDIT] Be very careful using find to remove files.




  1. Pablo Lopez Cienfuegos (1 comments.) says:

    Ok, that erases all your files with a dot in the name… What about “rm -rf .”?

  2. Anton Olsen (5 comments.) says:

    Be very careful using find to remove files.

    The find command by default is recursive so any and all files in all subdirs below the current directory will go away too.

    Using ‘*.*’ for the pattern will miss all files without dots in them. rm is not able to remove the directories so you will get an error for each of them found (include ‘.’, the current dir).

    If you really want to remove all files from the current directory through all it’s subdirectories and leave the dirs alone then the better find parameters would be:
    find . -type f -exec rm {} \;

    You could also use “find . -exec rm {} \;” and ignore the errors rm throws out for the dirs.

  3. Mark (118 comments.) says:

    Very good advice indeed. My post was much more general but the link explains things further.
    The best advice is to use the find without the rm to see what gets included before using the rm in the exec.

  4. Ray (5 comments.) says:

    I posted about this a few days ago:

    find . -name ‘*\.*’ | xargs rm

  5. zeroHalo (1 comments.) says:


    find . -name ‘*.*’ -exec rm -rf {} \;

    if you want to erase everything. Of course, I have yet to find a purpose for doing such except to be mean to *n*x newbies.

    NOTE: This will erase everything – do NOT try.

  6. Mike Bailey (1 comments.) says:

    If you’re converting from mod_php to phpsuexec on a cpanel system, the following commands help:

    find /home/user/public_html/ -user nobody -exec chown user:user {} \;
    find /home/user/public_html/ -perm 777 -exec chmod 755 {} \;

    Run that for each user on the system, and you won’t have any phpsuexec internal server errors.

  7. jez (56 comments.) says:

    find . -name ‘*.*’ -exec rm -rf {} \;

    is really, really mean :x


  1. rm on steroids (a safer way)

    I came across an excellent tip about mixing rm and find commands in Linux shell.
    The only thing I’d add to it is a bit of safety. Before executing the mix of find and rm, run the find command with the same options, but without rm. It will pri…

Obviously Powered by WordPress. © 2003-2013

page counter