/bin/rm: Argument list too long

Posted on 2010-01-12
Medium Priority
Last Modified: 2013-12-20
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?
Question by:Sainai
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 14

Accepted Solution

Deepak Kosaraju earned 280 total points
ID: 26301048
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.

LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 26301059
For filenames with spaces, new lines and other whitespace characters use following command.
find . -name * -print0 | xargs -o rm

Expert Comment

ID: 26301212
or do it like that
ls -l |awk '{print "rm -fr ",$9) > remove_file
then you have a file to exec.
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.


Assisted Solution

hemmi earned 220 total points
ID: 26301271
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

Expert Comment

ID: 32783252
I have a similar problem but when I use your solution listed here I get XARGS command not recognised...?

Expert Comment

ID: 32785395
Well I noticed a typo: in my line there is
... xarg ...
it must read

Can you start
xargs --help

by itself?


Expert Comment

ID: 32785682
Thx - that's it - I used XARG but of course XARGS does work - thank you! ;)

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question