/bin/rm: Argument list too long

Hi I am trying to delete tens of thousands of files from a directory but
it keeps telling me that the argument list is too long when I use
rm -rf *.
How can I delete them all?
SainaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak KosarajuDevOps EngineerCommented:
Apparently there is a maximum number of files that can be passed as arguments to rm

Work around for this issue is using find and xargs.

find . -name 'spam-*' | xargs rm

In the above instance the command will forcefully delete all files in the current directory that begin with spam-. You can replace the spam-* with anything you like. You can also replace it with just a * if you want to remove all files in the folder.

find . -name '*' | xargs rm

Xargs is Linux command that makes passing a number of arguments to a command easier.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deepak KosarajuDevOps EngineerCommented:
For filenames with spaces, new lines and other whitespace characters use following command.
find . -name * -print0 | xargs -o rm
0
AsrCommented:
hi
or do it like that
ls -l |awk '{print "rm -fr ",$9) > remove_file
then you have a file to exec.
remove_file
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

hemmiCommented:
cd to the directory where you tried the rm -rf * before and do

find . -mindepth 1 -iname "*" -print0|xarg --no-run-if-empty --null rm -rf

the mindepth avoids that you delete "." directory. For the "*" put in whatver pattern you like to remove.
It work for all filenames, i.e. with special characters like spaces in them

The above awk solution will
a) take hours (it creates a new process for every single rm)
b) it fails on filenames with e.g. spaces
0
Chris KenwardDirectorCommented:
@hemmi:
I have a similar problem but when I use your solution listed here I get XARGS command not recognised...?
0
hemmiCommented:
Well I noticed a typo: in my line there is
... xarg ...
it must read
xargs

Can you start
xargs --help

by itself?

0
Chris KenwardDirectorCommented:
Thx - that's it - I used XARG but of course XARGS does work - thank you! ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.