I have some images named with generated uuid1 string. For example 81397018-b84a-11e0-9d2a-001b77dc0bed.jpg. I want to find out all these images using "find" command:
find . -regex "[a-f0-9\-]\{36\}\.jpg".
But it doesn't work. Something wrong with the regex? Could someone help me with this?
Judging from other answers, it seems this might be find's fault.
However you can do it this way instead:
find . * | grep -P "[a-f0-9\-]{36}\.jpg"
You might have to tweak the grep a bit and use different options depending on what you want but it works.
Try to use single quotes (') to avoid shell escaping of your string. Remember that the expression needs to match the whole path, i.e. needs to look like:
find . -regex '\./[a-f0-9-]*.jpg'
Apart from that, it seems that my find (GNU 4.4.2) only knows basic regular expressions, especially not the {36} syntax. I think you'll have to make do without it.
Simple way - you can specify .* in the beginning because find matches the whole path.
$ find . -regextype egrep -regex '.*[a-f0-9\-]{36}\.jpg$'
find version
$ find --version
find (GNU findutils) 4.6.0
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION
FTS(FTS_CWDFD) CBO(level=2)
on Mac OS X (BSD find): Same as accepted answer, the .*/
prefix is needed to match a complete path:
$ find -E . -regex ".*/[a-f0-9\-]{36}.jpg"
man find
says -E
uses extended regex support
The -regex
find expression matches the whole name, including the relative path from the current directory. For find .
this always starts with ./
, then any directories.
Also, these are emacs
regular expressions, which have other escaping rules than the usual egrep regular expressions.
If these are all directly in the current directory, then
find . -regex '\./[a-f0-9\-]\{36\}\.jpg'
should work. (I'm not really sure - I can't get the counted repetition to work here.) You can switch to egrep expressions by -regextype posix-egrep
:
find . -regextype posix-egrep -regex '\./[a-f0-9\-]{36}\.jpg'
(Note that everything said here is for GNU find, I don't know anything about the BSD one which is also the default on Mac.)
You should use absolute directory path when applying find instruction with regular expression. In your example, the
find . -regex "[a-f0-9\-]\{36\}\.jpg"
should be changed into
find . -regex "./[a-f0-9\-]\{36\}\.jpg"
In most Linux systems, some disciplines in regular expression cannot be recognized by that system, so you have to explicitly point out -regexty like
find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"
Source: Stackoverflow.com