How to search filenames by regex with "find"
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?
find /home/test -regextype posix-extended -regex '^.*test\.log\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.zip' -mtime +3
-
-name
uses globular expressions, aka wildcards. What you want is-regex
- To use intervals as you intend, you
need to tell
find
to use Extended Regular Expressions via the-regextype posix-extended
flag - 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
\.
- 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
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"