/bin/rm: argument list too long

The “rm” command line tool is beneficial for deleting files, directories, and subdirectories from the system. Removing extra files and directories is necessary to free up storage space. Sometimes the directory contains a large number of files that can not be removed using the “rm” command, and it returns an error “/bin/rm: argument list too long”.

This post illustrates the possible reasons and solutions to fix the error. The outcomes of this post are as follows:

Reason: Exceed the Argument Limit

The error occurs when the user passes too many arguments while executing a single command. The “argument list too long” indicates that the user has exceeded the argument list maximum limit while using the “rm”, “cp”, “ls”, and “mv” commands.

Hence it is recommended to get the maximum argument list before the deletion of a large no of files from the directory using the following command:

$ getconf ARG_MAX

The above command represents the “ARG_MAX” limit “2097125”.

Now move on to the first solution to resolve the error “/bin/rm: argument list too long”.

Solution 1: Remove and Recreate the Target Directory

The first possible solution to this error is not to use the wildcard “*” from the “rm” command that exceeds the “ARG_MAX” limit. Delete the targeted folder/directory completely.

It can be done with the help of the “rm” command followed by its “-r(recursive)” flag and the targeted directory “absolute path” in the following way:

$ rm -r /path/to/director

For practical implementation an “Extra” directory is removed completely by using the above syntax of the “rm” command:

$ rm -r /home/itslinuxfoss/Extra

The above command has deleted the targeted “Extra” directory completely, including all the files, directories, and subdirectories available in it.

The above command also allows the user to delete all the files having types “.txt”, “.pdf”, “.doc”, and many others. To do so, specify the file type with the “rm” command:

$ rm *.pdf

Hence all the “.pdf” file format files have been deleted from the “Extra” directory.

Once the deletion of all files is done, and the user wants to recreate it again, then use the “mkdir” command for the recreation of the target directory:

$ mkdir [directory-name]

Suppose the above-deleted directory “Extra” is being recreated by utilizing the “mkdir” command:

$ mkdir Extra

This solution will definitely work and if it does not fix the error then try the next solution.

Solution 2: Delete the Files Using the “find” Command

It is recommended first to find the files from the target directory and then delete them as per requirements. This method is so easy and accurate as it locates each file from the directory and deletes it.

For this purpose, first, change the present working directory to the target directory through the “cd” command:

$ cd [directory-name]

In this case, the targeted directory is the “Sample” directory having “2000” “files that can be assessed through the “cd” command in this way:

$ cd Sample

After that use the “find” command with the file type argument “-type f” and the “-delete” in this way:

$ find . -type f -delete

All the files from the “Sample” directory have been removed, and there are no more files in it that are verified by the “ls -l” command.

Conclusion

The error “ /bin/rm: argument list too long” occurs due to too many arguments passing in a single command. Basically, the specified arguments in a command exceed the “ARG_MAX” limit defined by the operating system. To resolve this error, delete the directory itself using the “rm” command. It can also be fixed by utilizing the “find” command. This post has illustrated all the possible reasons and solutions to the error “/bin/rm: argument list too long”.