Geeks With Blogs

News

Aboo S blog

I was deleting files by using rm on the linux server. There is a maximum number of files that can be passed as arguments to rm. So it does not allow you remove the files with 'rm' command.

Using rm to delete files is the easiest way adopted by us (linux users). rm is generally used with ease on a daily basis. So what you can do when the simple rm command does not work.

There were around 72K files were generated in a folder which I wanted to delete this morning. So I went to that directory and tried to remove all the files starting with "Count*".

root # rm Count*
/bin/rm: Argument list too long.

No way. The irritating error message appeared.

Not sure what the maximum number of arguments for rm command, may be its 1024 arguments only.

The workaround is simple: Use find to pipe all the matching files to rm, one at a time.
root # find . -name 'Count*' | xargs rm

And this can remove as many files as you want. This work really great!!

Posted on Friday, October 7, 2005 1:02 PM | Back to top


Comments on this post: Argument list too long - Linux Workaround

# not that easy
Requesting Gravatar...
The find | xargs solution doesn't work on files with spaces.

The only reliable way I could find is to use
find . -name 'Count*' -exec rm {} \;
but there's a caveat: it will look for Count* in subdirectories as well, which may be undesireable
Left by Tonik on Dec 26, 2005 11:37 AM

# Oh :)
Requesting Gravatar...
-maxdepth 0 does the trick:

find . -maxdepth 0 -name 'Count*' -exec rm {} \;
Left by Tonik on Dec 26, 2005 11:41 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Oh Its a great addition to the resolutions, thanks a lot!
Left by Aboo on Dec 26, 2005 12:29 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
The above still gave me a "arg list too long" error
but this worked (not considering subdirectories):
for x in *; do rm $x; done
Left by NK on Sep 15, 2006 4:13 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
That find command should be:

find . -maxdepth 1 -name 'Count*' -exec rm {} \;

A maxdepth of 0, according to the manual, means:

maxdepth 0 means only apply the tests and actions to the command line arguments.

which means the '.' argument, not 'Count*'. I.E., this command:

find dir -maxdepth 0 -name 'Count*' -exec rm {} \;

will never delete anything since dir does not match 'Count*'. Setting maxdepth to 1 will process all files in the dir directory also, which is more likely what is intended.
Left by absmiths on Jan 04, 2007 8:11 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
What about a tar command.

My problem is that I am given a list of file names in different directories that need to be placed in the same Volume on a Tape drive.

Any ideas?
Left by aweikle on Mar 07, 2008 12:31 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
What if we want to avoid the subdirectories?
Left by Autoline Reviews on Jun 14, 2008 1:30 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Pretty good information for linux enthusiasts. Specially the tips that are tried and tested.
Left by Health Fitness on Jun 14, 2008 1:41 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Thanks for the information
Left by Finance blog on Jun 14, 2008 1:42 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Thanks for the neat trick. Saved my @$$ today @ work :)
Left by Mayank on May 03, 2009 6:23 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
This just saved me a load of time too! rm worked, and it works well for cp/mv too:

find /dir/dir/ -name 'Count*' -exec cp '{}' /dir/dir/ \;
Left by Lok on Jul 27, 2009 2:09 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Nice bit with the xargs command. This saved me today
Left by Ray on Oct 26, 2009 12:51 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
ls | xargs rm
Left by Elijah on Nov 01, 2009 12:10 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
ls | xargs rm
this worked perfectly, thank you!
Left by frog on Dec 16, 2009 1:16 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Thanks a lot! You have saved tons of my time which I would have spent in deleting the 100,000+ log files from the entire system. Great tip.
Left by web hosting blog on Apr 27, 2010 2:30 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Thanks a lot for the tip and saving my time.
Left by Blog hosting on Jun 21, 2010 11:05 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
thanks, that was really helpful...
Left by thiyagi on Jan 05, 2011 4:41 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
из консоли ls -l /usr/local/vpopmail/domains/oldcomp.ru/abuse/Maildir/cur/ | awk '{print "rm /usr/local/vpopmail/domains/oldcomp.ru/abuse/Maildir/cur/"$9}' | /bin/sh

удаляет все !!!
Left by baraholka on May 10, 2011 5:44 AM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
this was very helpful to me. Thx a lot
Left by nagy on Aug 29, 2011 3:52 PM

# re: Argument list too long - Linux Workaround
Requesting Gravatar...
Very helpful to me. thx a lot
Left by nagy on Aug 29, 2011 3:53 PM

Your comment:
 (will show your gravatar)
 


Copyright © Aboo S | Powered by: GeeksWithBlogs.net | Join free