How to Fix /bin/rm: cannot execute [Argument list too long]

The rm command is used in Linux to delete files and directories. It can be used to delete a few files by specifying each file name, or it can be used with the -r argument (recursive) to delete a directory and the entire directory tree structure which lies beneath it.

The syntax to use to rm command is:

$ rm <file1> <file2> ... <fileN> 
$ rm -r <directory1> <directory2> ... <directoryN> <target_directory>

Delete Files and Directories in Linux

These syntaxes can also be combined to delete files and directories in one go:

$ rm -r file1 file2 file3 directory1 directory2
Delete Files and Directories
Delete Files and Directories

You can also specify file and directory names with wildcards, if the names are similar, i.e., begin, end, or contain the same pattern in their name. This is especially useful when there a large number of files of the same file extension are to be removed.

For example, to delete all files whose filename begins with the string ‘file‘, we can run:

$ rm file*

Similarly, to delete all files with extension '.txt', we can run:

$ rm *.txt

Basically what the wildcards do is: place all the files or directories which satisfy the wildcard pattern as arguments to rm. Thus theoretically there is no limit on the number of arguments to rm command.

Deleting a Large Number of Files: “Argument list too long” Error

Even though theoretically ‘rm’ there is no limit on the number of arguments, there is a predefined limit on the maximum number of arguments that a single command can work with.

This is a limit defined in the Linux system and is based on the stack size of the system. Hence, if a wildcard pattern places tens of thousands of files or directories as arguments, the program throws an error: “Argument list too long“.

Let’s take an example of a folder that contains around 2.5 lacs (250K) files.

$ ls -l | wc -l
$ ls | head
List Files Count in Linux
List Files Count in Linux

Let’s try to remove them with a wildcard.

$ rm file*
Deleting Tons of Files in Linux
Deleting Tons of Files in Linux

Thus it gives the aforementioned error. Let’s now see how we can solve this error.

Solution: Use Find Command with ‘-exec’ Argument to Delete Thousands of Files

The find command basically searches for files in directories based on different parameters, like filename pattern, file extension, etc.

Then we make use of the '-exec' parameter of ‘find‘ which will allow you to run any command over the output of find.

$ find . -maxdepth 1 -name "<filename_pattern>" -exec rm -r {} \;

Here, the first argument, '.' (current directory) is the directory where to find the files. If files from some other directory are to be removed, you must specify the path to that directory here.

The argument '-maxdepth 1' considers only the files and directories in the current directory. Without specifying this depth, the find command will look through all subdirectories unnecessarily.

The argument "-name" is used to find files and directories with a string pattern. The ‘filename_pattern’ specifies the string pattern. Then we specify the ‘rm -r‘ command after an argument '-exec'.

The open brackets '{ }' are a placeholder for the argument which is to be used from the output of find.

$ find . -maxdepth 1 -name "file*" -exec rm -r {} \;
Find and Deleting Thousands of Files in Linux
Find and Deleting Thousands of Files in Linux

As we can see, all the files have been deleted. The same command can be used to delete directories in bulk, as we are already passing the '-r' argument.

Conclusion

In this article, we learned about the command rm command and how the issue ‘Argument list too long’ can be dealt with by using ‘rm’ combined with the ‘find‘ command.

If you have any feedback or questions, let us know in the comments below!

Got something to say? Join the discussion.