Coding⏱️ 4 min read📅 2026-06-11

How to Fix: "Argument list too long" error for `rm -rf *` on a directory with 4000 files

Error fixing rm -rf * on OS X and Ubuntu

Quick Answer: Use the -n option with rm to prevent deletion, or use find to delete files in batches.

The "Argument list too long" error occurs when attempting to delete files using the `rm -rf *` command, which can be particularly frustrating when dealing with large numbers of files. This issue primarily affects users running OS X and, to a lesser extent, Linux distributions such as Ubuntu.

Dealing with this error can be time-consuming and may require manual intervention to resolve. Fortunately, there are system-level workarounds available in both OS X and Linux environments that can help alleviate this issue.

💡 Why You Are Getting This Error

  • The primary reason for the "Argument list too long" error is due to the maximum allowed length of a command argument in Unix-based systems. In OS X, this limit is set at around 4096 characters, which may not be sufficient to accommodate the paths of all files within a given directory when using the `rm -rf *` command.
  • Another possible reason for this error is related to the way Linux handles file system permissions and ownership. If the user executing the `rm -rf *` command does not have the necessary permissions or ownership, it may result in an "Argument list too long" error.

🚀 How to Resolve This Issue

Using the `find` Command with `-print0` Option

  1. Step 1: Open a terminal and use the following command to find all files within the directory: `find /path/to/directory -type f -print0`. This will list all file paths without spaces, making it easier to handle large numbers of files.
  2. Step 2: Next, pipe the output to `xargs` with the `-0` option to split the input into separate arguments: `find /path/to/directory -type f -print0 | xargs -0 rm`. This will ensure that each file path is handled individually without exceeding the maximum allowed length.
  3. Step 3: Finally, use the `rm -rf` command with the `-i` flag (interactive mode) to confirm deletion of each file: `find /path/to/directory -type f -print0 | xargs -0 rm -i`. This will prompt the user to confirm deletion before proceeding.

Using the `rsync` Command with `-avz` Options

  1. Step 1: Open a terminal and use the following command to copy all files within the directory using `rsync`: `rsync -avz /path/to/directory/ user@remotehost:/path/to/remotefolder/`. This will transfer all files from the local directory to a remote location, avoiding the need for manual deletion.
  2. Step 2: Note: Replace `/user@remoteshost:/path/to/remotefolder/` with the actual destination path and IP address of the remote server.

💡 Conclusion

To resolve the "Argument list too long" error when using `rm -rf *` on a directory with 4000 files, consider utilizing system-level workarounds such as using the `find` command with the `-print0` option or the `rsync` command with the `-avz` options. These methods can help alleviate the issue and make manual deletion more manageable.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions