0

This question already has an answer here:

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

marked as duplicate by Jeff Schaller, Romeo Ninov, RalfFriedl, Thomas, Thomas Dickey Sep 13 '18 at 20:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

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 {} \;
  • not as I ask , I mean to delete all files with name date syntax as xxxx-xx-xx , and that older then 10day – yael Sep 13 '18 at 11:56
  • the syntax find /var/log/roler_t -mtime +10 -exec rm {} \; , will remove also the file as PL-22T20:KJU:00 – yael Sep 13 '18 at 12:01
  • @yael as @B.McCready answered, then just filter by filename, too adding -name "*XXXX-XX-XX*" to the find` command. – Mr Shunz Sep 13 '18 at 12:11
0

This is similar to your earlier question, you can achieve this by the following

$ find /var/log/roler_t -mtime +10 -name "XXXX-XX-XX*"

where XXXX-XX-XX is the datestamp you are looking for. If you know that the year month and day correspond to the modification time you are looking for you coul change the argument to the -name option to be something like this:

$find /var/log/roler_t -mtime +10 -name "`date -d'now - 10 days' +'%y-%m-%d'*"

to automatically generate the filename prefix, though you may need to play with the date command number of days you subtract to be sue the mtime and the datestamp match what you need.

If you just want to match the names of files that begin with a datestamp like string, e.g. '2018-09-13' then use the following:

/var/log/roler_t -mtime +10 -name "[12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]*"
  • but 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 – yael Sep 13 '18 at 12:16
  • @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. – B.McCready Sep 13 '18 at 12:20
  • yes thats I mean – yael Sep 13 '18 at 12:30
  • @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/… – michael Sep 13 '18 at 12:31
  • so can you please update the answer , instead the remark so I will choose the right answer – yael Sep 13 '18 at 12:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.