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, ls, mv, cp, 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

Here’s the error:

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
2097152

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

 

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

There are several 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 it 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

 

Conclusion

Knowing how to address the “Argument list too long” error is a valuable skill. These solutions ensure that you can efficiently manage and manipulate your files, regardless of their quantity, without running into this limitation. So, the next time you encounter this error, you’ll be well-prepared to tackle it and continue your Linux system administration tasks seamlessly.

 

Published: October 1st, 2020 | Last updated: October 1st, 2023

 

Tags: , ,

Discussion

  1. Another solution: Get the list of targets and redirect the output to a text file.

    $ ls *.tmp > script.sh

    Edit the file to do a chunk of commands per iteration:
    rm -f file1 file2 file3 … file50
    rm -f file51 file52 file53… file100
    rm -f file101 …

    Then chmod +x and run the script. This effectively removes the limitation altogether.

  2. thanks for sharing this. :handshake:



Top ↑