115

I was trying to find all files dated and all files 3 days or more ago.

find /home/test -name 'test.log.\d{4}-d{2}-d{2}.zip' -mtime 3

It is not listing anything. What is wrong with it?

user5994461
  • 5,301
  • 1
  • 36
  • 57
Tree
  • 9,532
  • 24
  • 64
  • 83

5 Answers5

177
find /home/test -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip' -mtime +3
  1. -name uses globular expressions, aka wildcards. What you want is -regex
  2. To use intervals as you intend, you need to tell find to use Extended Regular Expressions via the -regextype posix-extended flag
  3. You need to escape out the periods because in regex a period has the special meaning of any single character. What you want is a literal period denoted by \.
  4. To match only those files that are greater than 3 days old, you need to prefix your number with a + as in -mtime +3.

Proof of Concept

$ find . -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip'
./test.log.1234-12-12.zip
Community
  • 1
  • 1
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 26
    If you happen to run on OS X, then use `-E`. `find -E . -regex 'theregex'` to take advantage of extended (modern) regular expressions. – i4niac Aug 27 '14 at 05:17
  • `[0-9]{2}` hasn't worked for me on macOs, i had to use `[0-9][0-9]` – To Kra Jan 12 '17 at 21:17
  • 1
    needed extended to use optional groups i.e. `find -E . -iregex '^.*\.js(\.map)?$'` – chris Mar 11 '17 at 19:37
  • @Chris see '-regextype posix-extended' – SiegeX Mar 11 '17 at 19:40
  • @SiegeX my previous comment was in case anyone might find it useful to know that in order to use optional groups you need to enable posix-extended and to give an example of what that looks like. Thank you for your answer. – chris Mar 13 '17 at 15:13
  • it is also important to note that the regex is applied to the entire path, not only to a file or folder name; see `man find` > `-regex` – ssc May 10 '20 at 16:47
  • I didn't need `^` or `$` to mark the start and end, but the pattern did need to match the whole filename including the path part supplied as the first argument. – IpsRich Jan 15 '21 at 15:32
22

Use -regex not -name, and be aware that the regex matches against what find would print, e.g. "/home/test/test.log" not "test.log"

Erik
  • 88,732
  • 13
  • 198
  • 189
20

Start with:

find . -name '*.log.*.zip' -a -mtime +1

You may not need a regex, try:

 find . -name '*.log.*-*-*.zip' -a -mtime +1

You will want the +1 in order to match 1, 2, 3 ...

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
13

Use -regex:

From the man page:

-regex pattern
       File name matches regular expression pattern.  This is a match on the whole path, not a search.  For example, to match a file named './fubar3',  you  can  use  the
       regular expression '.*bar.' or '.*b.*3', but not 'b.*r3'.

Also, I don't believe find supports regex extensions such as \d. You need to use [0-9].

find . -regex '.*test\.log\.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\.zip'
dogbane
  • 266,786
  • 75
  • 396
  • 414
7

Just little elaboration of regex for search a directory and file

Find a directroy with name like book

find . -name "*book*" -type d

Find a file with name like book word

find . -name "*book*" -type f
Hafiz Muhammad Shafiq
  • 8,168
  • 12
  • 63
  • 121
  • You do not use regex with `find . -name "*book*" -type f`. Using "f" for filename is still useful as an answer since only filenames shall be searched for, but you should make clear that these are globular expressions as [the other answer makes clear](https://stackoverflow.com/a/5249797/11154841), not regex. – questionto42 Sep 21 '22 at 07:56