How to simply remove everything from a directory on Linux

Posted by Tometzky on Server Fault See other posts from Server Fault or by Tometzky
Published on 2009-08-19T11:27:46Z Indexed on 2010/03/16 16:21 UTC
Read the original article Hit count: 415

How to simply remove everything from a current or specified directory on Linux?

Several approaches:

  1. rm -fr *
    rm -fr dirname/*
    Does not work — it will leave hidden files — the one's that start with a dot, and files starting with a dash in current dir, and will not work with too many files

  2. rm -fr -- *
    rm -fr -- dirname/*
    Does not work — it will leave hidden files and will not work with too many files

  3. rm -fr -- * .*
    rm -fr -- dirname/* dirname/.*
    Don't try this — it will also remove a parent directory, because ".." also starts with a "."

  4. rm -fr * .??*
    rm -fr dirname/* dirname/.??*
    Does not work — it will leave files like ".a", ".b" etc., and will not work with too many files

  5. find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -fr
    find dirname -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -fr
    As far as I know correct but not simple.

  6. find -delete
    find dirname -delete
    AFAIK correct for current directory, but used with specified directory will delete that directory also.

  7. find -mindepth 1 -delete
    find dirname -mindeph 1 -delete
    AFAIK correct, but is it the simplest way?

© Server Fault or respective owner

Related posts about linux

Related posts about command-line