[ACCEPTED]-Is there any trivial way to 'delete by date' using ´rm'- in bash?-rm
I would combine find and rm (without pipe)
find . ...bunch of find criterias to select certain files (e.g. by date) .... -exec rm \{\} \;
EDIT: with 4 parameters for your example it would be
find . -maxdepth 1 -type f -ctime -12 -exec rm \{\} \;
CAVEAT: This 3 works just today :-). (To make it work everytime, replace 2 the -ctime with absoulte time, see timeXY 1 in the manpage )
Some versions of find support the -delete 3 option, making it even more efficient...
find . -maxdepth 1 -type f -ctime -12 -delete;
Check 2 your find man page (this has worked on most 1 recent releases of Ubuntu for me)
I would use:
find . -maxdepth 1 -type f -newerct 'jan 1' -print0 \
| xargs -0 rm
(or -newermt
if you want to filter on 3 modification time)
Note that the 't' form 2 of -newerXY will allegedtly allow any date 1 format compatible with cvs (see doco).
find(1)
is a much more efficient to do what you 4 want than parsing ls(1)
output.
EDIT: something 3 to watch for is filenames with spaces in 2 them so you want to have a find
which supports 1 -print0
(to be used with xargs -0
) for better performance.
find . -mtime +12 -print0 | xargs -0 rm -f
Instead of parsing ls(1) which can too easily 4 break you should rely on stat(1):
stat -c '%z/%n' files_or_glob | grep '^date' | cut -d/ -f2- | xargs -d '\n' rm -i
e.g.
$ stat -c '%z/%n' *| grep '^2008-12-16' | cut -d/ -f2- | xargs -d '\n' rm -i
Note: this 3 will not handle filenames with embedded 2 newlines correctly. However they are rarely 1 found in the wil.d
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.