[ACCEPTED]-Is there any trivial way to 'delete by date' using ´rm'- in bash?-rm

Accepted answer
Score: 11

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 )

Score: 10

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)

Score: 3

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).

Score: 1

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
Score: 1

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