How can I delete all files from a directory when it reports "Argument list too long"
Solution 1:
find . -maxdepth 1 -type f -exec rm -f {} \;
it simply takes too long (one exec of rm per file).
this one is much more efficient:
find . -maxdepth 1 -type f -print0 | xargs -r0 rm -f
as it takes as much filenames as argument to rm as much it's possible, then runs rm with the next load of filenames... it may happen that rm is only called 2 or 3 times.
Solution 2:
In the event you cannot remove the directory, you can always use find.
find . -maxdepth 1 -type f -exec rm -f {} \;
That will delete all files in the current directory, and only the current directory (not subdirectories).
Solution 3:
Both these will get round the problem. There is an analysis of the respective performance of each technique over here.
find . -name WHATEVER -exec rm -rf {} \;
or
ls WHATEVER | xargs rm -rf
The problem stems from bash expanding "*" with everysingle item in the directory. Both these solutions work through each file in turn instead.
Solution 4:
I was able to do this by backing up one level:
cd ..
And running:
rm directory name -rf
And then re-creating the directory.
Solution 5:
All these find invocations are very nice but I seldom remember exactly the nomenclature needed when I'm in a hurry: instead I use ls. As someone mentions, ls . would work but I prefer ls -1 as in:
ls -1 | xargs -n 100 rm -rf
The -n xxx figure is pretty safe to play around with as exceeding the maximum will either be auto-corrected ( if size-max is exceeded; see -s ) or if the args-max for an app is exceeded it will usually be rather obvious.
It should be noted grep is handy to insert in the middle of this chain when you only want to delete a subset of files in a large directory, and don't for whatever reason want to use find.
This answer assumes you are using Gnu core utilities for your ls, xargs & etc.