I'm sure this has been answered before, but my searches have not turned up the right solution.
I need to remove files in a directory, without descending into subdirectories, older than n days. Some of the filenames contain spaces or other special characters:
E10403 (2)
E20402 (2)
W50301 (2)
Shortcut to enew1.lnk
So far all variations of find I've tried result in The parameter list is too long (e.g. find * -prune -type f -mtime +30).
Suggestions or links to solutions would be much appreciated.
This eliminates the The parameter list is too long error.
find . -type f -prune
-type f ahead of -prune
However, it descends into the subdirectories, which violates one of the primary requirements. It needs to be confined to the current directory without descending into subdirectories.
Find every file that meets your mod time/age requirement, use "sed" to remove the leading "./" characters, remove any names that still have a '/' directory separator in them, and remove what's left:
find . -mtime N -type f -name '* *' | sed 's/\.\///g' | grep -v '/' | while read ff; do rm "$ff"; done
The find -xdev primary only helps if the subdirectories are all mount points for different filesystems. However, the following command should do what you want:
Using find * was suggested from another source. At first it seemed to do what was needed. In fact, executing it in a directory where none of the file names contain spaces or something that's appearing as a space does work as advertised.
---------- Post updated at 12:17 PM ---------- Previous update was at 12:09 PM ----------
{} ; means "run command once per file". {} + means "run command once for many files". So ';' will run rm file1 , rm file2 etc. while '+' will run rm file1 file2 file3 .. It keeps the maximum number of arguments in mind, but puts in as many as safely possible in one command for efficiency.