Home » linux-commands » How to fix the /bin/rm: Argument list too long error

How to fix the /bin/rm: Argument list too long error

Some time we getting the issue as ‘/bin/rm: Argument list too long’ while While removing/deleting the temporary files on server this acrticle will provide steps to to fix the issue.
While removing/deleting the temporary files on server if you are getting below error.

root@linuxpcfix [/tmp]#rm sess_*
/bin/rm: Argument list too long.

Solution
This error generally occurs when you have too many files in a directory and want to delete them with a simple rm -rf *? Please used the below command to clear the many files from server.

root@linuxpcfix [/tmp]#find . -name ‘sess_*’ | xargs rm

or

root@linuxpcfix [/tmp]#find . -type f -exec rm -fv {} ;

The above command will forcefully delete all files in the current directory that begin with spam-.

This method works fine but it has a drawback, file deletion is too slow For a million of files using this method will take long time and server hard drive also stressing.
Finding and deleting big number of files with find’s -delete argument:
there is a better solution to delete the files, by using find’s command with -delete option it’s at least 1,000 times faster than rm

root@linuxpcfix [/tmp]#find . -type f -print -delete

Use the printing out option with find command to delete files as below.
If you want to output on your system’s terminal in “real time” that what is going on then use following

root@linuxpcfix [/tmp]#find . -type f -print -delete

To prevent your server hard drive from being stressed then use the following command.

root@linuxpcfix [/tmp]#ionice -c 3 find . -type f -print -delete

2. you can also use a simple bash loop with rm command to delete Too many of files

for i in *; do
rm -f $i;
done

ow many files will be deleted, then time how long it takes to delete them:

root@linuxpcfix [/tmp]#ls -1 | wc -l && time find . -type f -delete
test1 0m3.660s
test2 0m0.036s
test3 0m0.552s

If you want to print output that what what files are deleting then use the following command:

root@linuxpcfix [/tmp]#for i in $(echo *); do echo “Deleting : $i”; rm -f $i;

3. Deleting multiple files with perl
Deleting multiple files with perl is not a bad idea at all.
Here is a perl one liner, to delete all files contained within a directory:

root@linuxpcfix [/tmp]#perl -e ‘for(<*>){((stat)[9]<(unlink))}'

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud