Reputation: 11
I'm trying to create a job to delete files on a linux box older than X days. Pretty straightforward with:
find /path/to/files -mtime +X -exec rm {}\;
Problem is all my files have special characters b/c they are pictures from a webcam - most contain parenthesis so the above command fails with "no such file or directory".
Upvotes: 0
Views: 561
Reputation: 2883
Does
find /path/to/files -mtime +X -print | tr '()' '?' | xargs rm -f
work?
Upvotes: 0
Reputation: 26066
Have you tried this:
find /path/to/files -mtime +X -exec rm '{}' \;
Or perhaps:
rm $(find /path/to/files -mtime +X);
Or even this method using xargs
instead of -exec
:
find /path/to/files -mtime +X | xargs rm -f;
Another twist on xargs
is to use -print0
which will help the script differentiate between spaces in filenames & spaces between the returned list by using the ASCII null
character as a file separator:
find /path/to/files -mtime +X -print0 | xargs -0 rm -f;
Or as man find
explains under -print0
:
This primary always evaluates to true. It prints the pathname of
the current file to standard output, followed by an ASCII NUL
character (character code 0).
I would also recommend adding the -maxdepth
and -type
flags to better control what the script does. So I would use this for a dry-run test:
find /path/to/files -maxdepth 1 -type f -mtime +1 -exec echo '{}' \;
The -maxdepth
flag controls how many directories down the find
will execute and -type
will limit the search to files (aka: f
) so the script is focused on files only. This will simply echo
the results. Then when you are comfortable with it, change the echo
to rm
.
Upvotes: 1