CodexBloom - Programming Q&A Platform

how to to Use 'find' Command with 'exec' on MacOS Monterey - 'Argument list too long' scenarios

👀 Views: 216 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
bash macos find

I'm trying to delete a large number of files in a directory using the `find` command combined with `exec`, but I'm running into the behavior message: `find: Argument list too long`. I'm using MacOS Monterey (12.0) and the following command: ```bash find /path/to/directory -name '*.tmp' -exec rm {} + ``` I expected this to delete all files with the `.tmp` extension, but instead, it fails with the behavior mentioned above. I've also tried using the `-delete` option instead: ```bash find /path/to/directory -name '*.tmp' -delete ``` This seems to work for individual files, but on directories with thousands of files, it still gives me trouble with performance. I suspect it has to do with the way the `find` command handles a large number of file arguments. I also tried increasing the maximum arguments limit in bash with `ulimit -s unlimited`, but that didn't help either. What is the best way to handle this situation when I need to delete a large number of files without running into argument list limitations? Is there a different approach or pattern that I could use that would perform better in this case? Any best practices or alternatives would be greatly appreciated!