Linux rm Argument list too long

A problem that occasionally occurs is that when attempting to remove files in Unix or Linux system is that when you attempt to remove files from a directory, you are not able to do so. This only happens when you have too many files in a particular directory.

What happens when you run rm * is that the OS gets the names of all the files in the directory that match that condition (which would be everything in this case) and changes the command to rm file1.txt file2.txt file3.txt file4.txt ... You get the idea. The problem is sometimes the argument list is too long and the command is not able to process all of the files that are in the directory. As a result, you'll get a "Argument list too long" error message and none of the files being deleted.

How do you get around this problem? Here's the solution:

for x in $(ls -1)
do
echo $x
rm $x
done

What does this do? Lets explain:

  • for x in $(ls -1) gets all of the files that are in the directory.
  • do denotes the beginning of the for loop
  • echo $x will print out the name of the file that is being removed so that you can monitor the progress.
  • rm $x removes the file. The name of the file that is being removed was printed out by the previous line
  • done denotes the end of the for loop

If you have to clean up directories that often contain a lot of files, then placing this command in a script would be recommended than running it from the command line each time you need it.

Other examples for using this command are as follows:

for x in $(ls -1 | grep 2016 | awk '{print $9}')
do
echo $x
rm $x
done

This will remove all the files that have "2016" in the file name, year, or file size.

for x in $(ls -1 *hello* | awk '{print $9}')
do
echo $x
rm $x
done

This will remove all the files that have "hello" in the file name.

Updated: 2020-07-15 | Posted: 2017-04-03