Unable To Remove Files From Directory with Error “Argument list too long”

The Problem

While trying to list or interact with large number of files from command line it results with following error:

$ ls -l *.xml | wc -l
bash: /bin/ls: Argument list too long
0
$ rm *.xml
bash: /bin/rm: Argument list too long

or when trying to delete large number of files using find command it fails with below error message:

$ find /u01/oracle/apps/fs1/EBSapps/appl/xdo/12.0.0/temp/*.xml -mtime +120 -exec rm -v {} \;
bash: /usr/bin/find: Argument list too long

The Solution

There is a large but finite limit to command line imposed by the kernel and the errors shown above indicates that limit has reached.

To workaround this, xargs can be used which accepts a list of file names from its standard input and runs command on them. You can use ‘find’ in combination with ‘xargs’ as below:

# find . -type f -name '*.xml' | xargs rm

NOTE: The above command will delete all the files with a .xml extension in the current directory without any confirmation. I would recommend you to first take backup of necessary files before executing. It is always good to verify on a test machine and check before proceeding on the production machine.

Related Post