Skip to main content
added 1 character in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYYXXYYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell wildcard pattern instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Note that -mtime +5 is for files whose age as an integer number of days is strictly greater than 5, so 6 days and over. For files 5 days old or over, you'd need -mtime +4.

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell wildcard pattern instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Note that -mtime +5 is for files whose age as an integer number of days is strictly greater than 5, so 6 days and over. For files 5 days old or over, you'd need -mtime +4.

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell wildcard pattern instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Note that -mtime +5 is for files whose age as an integer number of days is strictly greater than 5, so 6 days and over. For files 5 days old or over, you'd need -mtime +4.

added 183 characters in body
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell globwildcard pattern instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Note that -mtime +5 is for files whose age as an integer number of days is strictly greater than 5, so 6 days and over. For files 5 days old or over, you'd need -mtime +4.

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell glob instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell wildcard pattern instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete

Note that -mtime +5 is for files whose age as an integer number of days is strictly greater than 5, so 6 days and over. For files 5 days old or over, you'd need -mtime +4.

Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Using GNU find:

find . -type f -mtime +5 \
    -regextype egrep -regex '.*[0-9]{4}-[0-9]{2}-[0-9]{2}[^/]*$' \
    -delete

The regular expression will match any string in the basename of a pathname that contains a date on the form YYYY-MM-DD. Note that we may also match XXYYY-MM-DDZZ where XX and ZZ are some other characters.

The [^/]*$ at the end makes sure that we're actually matching the expression against the basename of the current pathname, and means "no / for the rest of the string, please".

Using a shell glob instead (easier to maintain):

find . -type f -mtime +5 \
    -name '*[0-9][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*' \
    -delete