Question

How to sort out - bash: /bin/rm: Argument list too long

Posted February 8, 2020 907 views
Linux BasicsLinux Commands

When running rm * in a directory that contains a lot of individual files might get the following error:

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

There are a few ways to delete the files inside that specific directory:

  • You could delete the whole folder
  • You could the find command - this is my personal favorite
  • Or use a for loop - much slower!

Here’s how to do that!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer
  • Option 1:

Let’s say that you don’t need the folder, so what you could do is just use rm -r to delete the folder recursively, that way you would basically supply just 1 argument (the folder name), and you will be able to delete the folder and all files inside:

  • rm -r folder_name
  • Option 2:

Use the find command, the benefit is that it is much quicker and you could add specific arguments to narrow down the risk of deleting something that should not be deleted:

Again, first cd into the folder in question and then run:

  • find . -maxdepth 1 -type f -name "temp_file_name_*" -delete

A quick rundown of the arguments:

  • -maxdepth - here we specify the levels of directories below the starting-points that you would like to go. In our case 1 means that we would only delete the files from the current directory and not the subfolders
  • -type f - this is how we specify the type of the files that we would like to delete, this is really beneficial as if there are any symbolic links they would not be deleted
  • -name - we specify the file name with a wildcard at the end that we could use as a pattern
  • -delete - with the delete argument we basically tell the find command what to do with the files that match the patterns above.

In most cases, this should be fairly quick.

  • Option 3:

You could use a for loop to delete the files inside the folder with the rm command, note that this could take a very long time so I would not really recommend it:

You would need to first cd into the folder and then run:

  • for i in * ; do rm ${i} ; done

Note that this would delete all files inside the directory.

Here’s a quick video on how to do that as well:

Hope that this helps!
Regards,
Bobby

Submit an Answer