bash: /usr/bin/rm: Argument list too long – Solution

Over time, the storage used on Linux systems you manage will grow. As a result, you will, at some point, try to delete, move, search, or otherwise manipulate thousands of files using commands such as rm, cp, ls, mv, and so on, which are all subject to this limitation. As such, you will eventually come across the “Argument list too long” error detailed below.


The Error

bash: /usr/bin/rm: Argument list too long

bash: /usr/bin/rm: Argument list too long
All regular system commands, such as rm, ls, mv, cp, and so on are subject to this limitation.


What does “argument list too long” indicate?

“Argument list too long” indicates when a user feeds too many arguments into a single command which hits the ARG_MAX limit. The ARG_MAX defines the maximum length of arguments to the exec function.

An argument, also called a command-line argument, can be defined as the input given to a command, to help control that command line process. Arguments are entered into the terminal or console after typing the command. Multiple arguments can be used together; they will be processed in the order they typed, left to right.

This limit for the length of a command is imposed by the operating system. You can check the limit for maximum arguments on your Linux system using this command:

getconf ARG_MAX

Which will return something like this:

hydn@centos:~$ getconf ARG_MAX

The “argument list too long” error means that you’ve exceeded the maximum command-line length allowed for arguments in a command.


The Solution

There are a number of solutions to this problem (bash: /usr/bin/rm: Argument list too long).

Remove the folder itself then recreate it.

If you are trying to delete ALL files and folders in a directory, then instead of using a wild card “*” for removal (i.e. rm *), you can try the following:

rm -r /path/to/directory/

If you still need that directory, then recreate with the mkdir command.

Mass delete files using the find command:

You can use the find command to find every file and then delete them:

find . -type f -delete

Or to delete only specific file types (i.e., .txt files) or otherwise, use something like:

find . -name '.txt' -type f -delete

Tags: , ,