how to delete files with specific date pattern
6,616
You can filter your files by date with the find
command.
For example:
find /var/log/roler_t -mtime +10
returns all files with modification date > 10 days.
Similarly you can use flags like -atime
(access time), -ctime
(status change time), but I think -mtime
is what you're looking for.
If you want to remove them with a single command (but I suggest you always check before if the results of find
are what you're expecting):
find /var/log/roler_t -mtime +10 -exec rm {} \;
Related videos on Youtube
Author by
yael
Updated on September 18, 2022Comments
-
yael almost 2 years
Under the folder
/var/log/roler_t
, we have the following files:-rw-r--r-- 1 roler smike 37652 Jun 25 01:44 2018-06-23T02:KJU:00 -rw-r--r-- 1 roler smike 41742 Jun 25 01:45 2018-06-21T20:KJU:00 -rw-r--r-- 1 roler smike 36762 Jun 25 01:46 2018-06-22T08:KJU:00 -rw-r--r-- 1 roler smike 51764 Jun 25 01:47 PL-06-23T12:KJU:00 -rw-r--r-- 1 roler smike 42751 Jun 25 01:48 2018-06-21T16:KJU:00 -rw-r--r-- 1 roler smike 34627 Jun 25 01:49 2018-06-22T23:KJU:00 -rw-r--r-- 1 roler smike 59828 Jun 25 01:51 2018-06-23T05:KJU:00 -rw-r--r-- 1 roler smike 33561 Jun 25 01:52 2018-06-22T20:KJU:00 -rw-r--r-- 1 roler smike 59828 Jun 25 01:51 JFG:KJU:00 -rw-r--r-- 1 roler smike 33561 Jun 25 01:52 PL-22T20:KJU:00 -rw-r--r-- 1 roler smike 36643 Jun 25 01:53 2018-06-22T01:KJU:00
How can we remove only the files that are older than ten days and have a specific date pattern, for example
2018-06-23
.Expected results are as follows:
the following files will stay under
/var/log/roler_t
-rw-r--r-- 1 roler smike 51764 Jun 25 01:47 PL-06-23T12:KJU:00 -rw-r--r-- 1 roler smike 59828 Jun 25 01:51 JFG:KJU:00 -rw-r--r-- 1 roler smike 33561 Jun 25 01:52 PL-22T20:KJU:00
-
yael almost 6 yearsnot as I ask , I mean to delete all files with name date syntax as xxxx-xx-xx , and that older then 10day
-
Daniele Santi almost 6 years@yael as @B.McCready answered, then just filter by filename, too adding
-name "*XXXX-XX-XX*" to the
find` command. -
yael almost 6 yearsbut XXXX-XX-XX can be 1998-02-34 or 2013-01-56 , etc , so the option "-name "XXXX-XX-XX*" must to fit to any date
-
B.McCready almost 6 years@yael, are you saying there might be files of varying age and datestamps, but you only want to delete the ones whose names begin with a datestamp in the form of YYYY-MM-DD? then just change the -name option argument to "[12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*" or similar.
-
yael almost 6 yearsyes thats I mean
-
michael almost 6 years@yael I think I get what you're saying; you want the filename to match a pattern? You can do that via ‘-name '????-??-??‘ or ‘-name "[0-9][0-9]0-9][0-9]-[0-9] (etc)‘ or also see the 'regex‘ option stackoverflow.com/questions/19111067/…
-
yael almost 6 yearsso can you please update the answer , instead the remark so I will choose the right answer
-
Jeff Schaller almost 6 years"hard-coding" the "now - 10 days" would tell
find
to look for files that are named with 10-days-ago, and would not "find" those named 9, 8, 7, etc days ago.