Aboo S

blog

  Home  |   Contact  |   Syndication    |   Login
  28 Posts | 0 Stories | 145 Comments | 59 Trackbacks

News


Article Categories

Archives

Finanace

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 07, 2005 1:02 PM

Feedback

# not that easy 12/26/2005 11:37 AM Tonik
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


# Oh :) 12/26/2005 11:41 AM Tonik
-maxdepth 0 does the trick:

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

# re: Argument list too long - Linux Workaround 12/26/2005 12:29 PM Aboo
Oh Its a great addition to the resolutions, thanks a lot!

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

# re: Argument list too long - Linux Workaround 1/4/2007 8:11 PM absmiths
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.

# re: Argument list too long - Linux Workaround 3/7/2008 12:31 PM aweikle
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?

# re: Argument list too long - Linux Workaround 6/14/2008 1:30 PM Autoline Reviews
What if we want to avoid the subdirectories?

# re: Argument list too long - Linux Workaround 6/14/2008 1:41 PM Health Fitness
Pretty good information for linux enthusiasts. Specially the tips that are tried and tested.

# re: Argument list too long - Linux Workaround 6/14/2008 1:42 PM Finance blog
Thanks for the information

# re: Argument list too long - Linux Workaround 5/3/2009 6:23 AM Mayank
Thanks for the neat trick. Saved my @$$ today @ work :)

# re: Argument list too long - Linux Workaround 7/27/2009 2:09 AM Lok
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/ \;

# re: Argument list too long - Linux Workaround 10/26/2009 12:51 PM Ray
Nice bit with the xargs command. This saved me today

# re: Argument list too long - Linux Workaround 11/1/2009 12:10 AM Elijah
ls | xargs rm

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: