-1

I need to find all log files older than 5 days and that the filename MUST ends with .log AND any character (i.e. first.log20210701 but not first.log). I tried this command but it shows also ".log"

find . -type f -name '*.log*' -mtime +5 

Could you help me please?

1
  • Should file.login123.log or something.logger.log be found or not? Could you please clarify the exact requirements for the filenames to search for?
    – Kusalananda
    Jul 24, 2021 at 15:30

2 Answers 2

6

Something like this (.+ means "one or more characters"):

find . -regex ".*\.log.+" -mtime +5

Or, POSIXly (the ? requires any one character, thus ?* means "one or more characters"):

find . -name "*.log?*" -mtime +5
3
  • This would still potentially find files that have names ending in .log if the name contained .log elsewhere as well.
    – Kusalananda
    Jul 24, 2021 at 15:24
  • @Kusalananda, technically, such names end with ".log AND any character[s]", which is what they say they want there. But it's perhaps a bit unclear what they want in that corner case, so maybe you should ask that as a comment on the question?
    – ilkkachu
    Jul 24, 2021 at 15:26
  • @ilkkachu I've asked for clarification.
    – Kusalananda
    Jul 24, 2021 at 15:30
4
find . -type f -name '*.log*' ! -name '*.log' -mtime +5 

Simply disallow filenames ending in .log, this is what ! -name '*.log' does.

This would find first.log20210701, but not first.log or first.log20210701.log or first.login.log.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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