Linux Workaround for ‘Argument list too long’ error message while removing all files from a directory

While someone deleting the files by using rm on the linux server, he might face this problem. 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 linux users. Well, I see that if this does not work, there is a solution provided for this problem. As mentioned, there were around 72K files were generated in a folder which needed to be deleted. In the example, there are all the files starting with “Count”, so can be chosen using “Count*”.

Here is the output on command prompt.

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

The workaround as given is: 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. Perfect!! Not yet…

The “find | xargs” solution doesn’t work on files with spaces.Now what to do? 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. So in that case, -maxdepth 0 does the trick.

root # find . -maxdepth 0 -name ‘Count*’ -exec rm {} \;

I hope you got your trick now!!

Maverick is a guest poster on TechWhack and blogs at TechieCorner.

Popularity: 100%



Related Posts

Programs Run Slower After You Install Windows XP SP-1

Disable Recent Documents History

Speed up Nero in XP

Enable or disable boot defrag

Faster, Safer Windows XP

Your Ad Here

3 Comments to “Linux Workaround for ‘Argument list too long’ error message while removing all files from a directory”

  1. Marcus | April 27th, 2006 at 8:27 pm

    Good tips Maverick,

    Another one I picked up, which is very useful when removing stuff is the -ok flag (as opposed to -exec):

    find . -maxdepth 0 -name ‘Count*’ -ok rm {} \;

    Cheers,

    Marcus

  2. Sean | June 3rd, 2006 at 5:52 pm

    Good tip! The -exec option forks a new ‘rm’ for each file, though; a more efficient solution is to use -print0:

    find . -name ‘Count*’ -print0 | xargs -0 rm

    The -0 tells the commands to terminate arguments with a null, which solves the quotes and spaces problem (check the man page for xargs for more info).

    — SER

  3. Jean Edwidge | November 26th, 2007 at 5:48 pm

    it was verry helpfull for me

Leave a Comment