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
Related videos on Youtube
Author by
David Oneill
Updated on September 18, 2022Comments
-
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 about 12 yearsyou don't need to add
-name "*"
to find all files. It finds so by default. -
jsbillings about 12 yearsusing
-name "*"
seems a bit redundant, could you explain why you chose that instead of just afind /path/to/dir -delete
, other than just using the same glob as the OP? -
jsbillings about 12 yearsAlso, 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 about 12 yearsThanks - I knew there was going to be some elegant solution.
-
clerksx about 12 yearsDo not parse
ls
, or expect things to break. Badly. mywiki.wooledge.org/ParsingLs -
Arcege about 12 yearsI had mentioned that above: "if there are no special characters".