bash: /bin/rm: Argument list too long - Solution

Symptoms

This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:

df -ih
/var/lib/php/session/
-bash: /bin/rm: Argument list too long

 

echo *.pdf | xargs -n 100 rm

linux command to find your limit for maximum arguments:

# getconf ARG_MAX
2097152

Example:

# rm *
-bash: /bin/rm: Argument list too long

Solution

There are number of solutions for this problem. Firstly, if there are no files within a directory we would like to keep, the best and fastest solution is to simply remove entire directory and recreate it again. Before you proceed make sure that the directory does not contain files you would like to keep and that you take a note regarding a permissions and ownership of this directory.

$ cd ../
$ ls -d mydirectory
$ rm -fr mydirectory
$ mkdir mydirectory

The other solution is to engage a for loop and remove all files one be one:

$ for i in *; do rm "$i"; done

or much faster by use of printf and xargs:

$ printf '%s__g5_token5e83abc28c43b' * | xargs -0 rm

or

$ time echo -n * | tr ' ' '__g5_token5e83abc28c43c' | xargs -0 rm

However, the above solution has its own limitation as it also may remove files we would like to keep if no proper regular expression in in place. As for an example here we will remove only *.txt files:

$ for i in *.txt; do rm "$i"; done

The last and proposed solution is to use regex to split all file into smaller batches. Find a similar pattern in all files and split them into a smaller groups. For example first remove,copy or move all files which start with a and have extension txt, after that all files which start with b and so on.:

$ rm a*.txt
$ rm b*.txt

You can also do the above for all letters using a for loop:

$ for i in $( echo {a..z} ); do rm $i*.txt ; done

 

 

 

  • 0 gebruikers vonden dit artikel nuttig
Was dit antwoord nuttig?

Gerelateerde artikelen

451 Temporary local problem + Squirrel mail

Message not sent. Server replied: Requested action aborted: error in processing 451 Temporary...

Locations of Common Log Files on cPanel Servers

One of the nice things about cPanel based servers is the way that they keep the location of key...

How to flush DNS - windows

I. Flushing DNS Cache in Windows 8: 1. Press the Windows Logo+R keys together and the Run box...

tuning-primer.sh – an alternative for mysqltuner

Insatllation cd /usr/local/bin wget https://launchpadlibrarian.net/78745738/tuning-primer.sh...

How to install mysqltuner

  How to install mysqltuner  wget http://mysqltuner.com/mysqltuner.plchmod +x...