Remove many many many files from a folder

13,066

Solution 1

As I can see you don't need to remove your dir , only files inside. So you can recreate it

rm -r /path/to/dir && mkdir /path/to/dir

or even delete only files inside

find /path/to/dir -type f -delete

afair first one works faster.

UPD. Note that way with find might be not optimal from space consumption point of view, as directory size will be reduced only after fsck. Details.

Solution 2

Workaround #1:

find /path/to/dir -delete

Workaround #2:

rm a*;

rm b*;

rm c*;

etc
Share:
13,066

Related videos on Youtube

David Oneill
Author by

David Oneill

Updated on September 18, 2022

Comments

  • David Oneill
    David Oneill almost 2 years

    I have a folder with 137795 files in it. I need to delete all of them. When I run rm * I get -bash: /bin/rm: Argument list too long. How do I get past this error?

  • rush
    rush about 12 years
    you don't need to add -name "*" to find all files. It finds so by default.
  • jsbillings
    jsbillings about 12 years
    using -name "*" seems a bit redundant, could you explain why you chose that instead of just a find /path/to/dir -delete, other than just using the same glob as the OP?
  • jsbillings
    jsbillings about 12 years
    Also, depending on the implementation or age of your find, "*" might not include files that start with a "." (current versions of findutils does include that dotted files).
  • David Oneill
    David Oneill about 12 years
    Thanks - I knew there was going to be some elegant solution.
  • clerksx
    clerksx about 12 years
    Do not parse ls, or expect things to break. Badly. mywiki.wooledge.org/ParsingLs
  • Arcege
    Arcege about 12 years
    I had mentioned that above: "if there are no special characters".