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

18,687

Solution 1

Some versions of find support the -delete option, making it even more efficient...

find . -maxdepth 1 -type f -ctime -12 -delete;

Check your find man page (this has worked on most recent releases of Ubuntu for me)

Solution 2

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 parameters for your example it would be

find . -maxdepth 1 -type f -ctime -12 -exec rm \{\} \;

CAVEAT: This works just today :-). (To make it work everytime, replace the -ctime with absoulte time, see timeXY in the manpage )

Solution 3

I would use:

find . -maxdepth 1 -type f -newerct 'jan 1' -print0 \
    | xargs -0 rm

(or -newermt if you want to filter on modification time)

Note that the 't' form of -newerXY will allegedtly allow any date format compatible with cvs (see doco).

Solution 4

Instead of parsing ls(1) which can too easily 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 will not handle filenames with embedded newlines correctly. However they are rarely found in the wil.d

Solution 5

find(1) is a much more efficient to do what you want than parsing ls(1) output.

EDIT: something to watch for is filenames with spaces in them so you want to have a find which supports -print0 (to be used with xargs -0) for better performance.

find . -mtime +12 -print0 | xargs -0 rm -f
Share:
18,687
Fergie
Author by

Fergie

Veteran hacker

Updated on July 28, 2022

Comments

  • Fergie
    Fergie almost 2 years

    I noticed today (after ~8 years of happily hacking away at bash) that there is no trivial way to 'delete by date' using ´rm'. The solution is therefore to pipe stuff around a combination of commands like rm, ls, find, awk and sed.

    Say for example I wanted to delete every file in the working directory from 2009, what would be the typical approach?

    I came up with the following, which is butt-ugly and should only be run if 'rm' is set to skip over directories (otherwise you will delete the parent directory):

    ls -la | awk '{if (substr($6,0,5)==2009) print $8}' | xargs rm
    

    Points for both the most elegant and the most outrageously over-engineered solutions.

  • OIS
    OIS over 15 years
  • Rob Kennedy
    Rob Kennedy over 15 years
    Is there any danger of "rm" interpreting a file name as a command switch in this scenario, and thus deleting too little or too much?
  • Joachim Sauer
    Joachim Sauer over 15 years
    @Rob: Partially: "-exec" calls rm with only one argument each time, so there's only the danger to delete too little. You could replace "rm \{\} \;" with "rm -- \{\} \;" to avoid that.
  • Fergie
    Fergie over 15 years
    nice- I like that there are no pipes or -execs
  • porges
    porges over 15 years
    \{\} is superfluous; just use {}
  • Alastair
    Alastair almost 15 years
    Nice, except that it doesn't use rm as required in the question. I'm not trolling: using rm is a valid requirement if, for example, you want to use -i for interactive delete...
  • MikeW
    MikeW almost 7 years
    Where "ctime" indicates that the file's 'Changed timestamp' (in days/24hr periods) was updated: ON/EXACTLY: no sign on number BEFORE/EARLIER: negative sign AFTER/LATER: positive + sign. Hence above example deletes files which were changed more than 12 days ago from now.
  • MikeW
    MikeW almost 7 years
    Note: Busybox 'find' does not support ctime and several other related options.