Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1593
  • Last Modified:

/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?
0
Sainai
Asked:
Sainai
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
Deepak KosarajuSr. Monitoring Architect - Nagios/Op5 SME - ConsultantCommented:
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
 
Deepak KosarajuSr. Monitoring Architect - Nagios/Op5 SME - ConsultantCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
kenwardcCommented:
@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
 
kenwardcCommented:
Thx - that's it - I used XARG but of course XARGS does work - thank you! ;)
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now