2011-07-27 107 views
178

我有一些用生成的uuid1字符串命名的圖像。例如81397018-b84a-11e0-9d2a-001b77dc0bed.jpg。我想用「查找」命令找出所有這些圖像:如何使用find命令使用正則表達式?

find . -regex "[a-f0-9\-]\{36\}\.jpg". 

但它不起作用。正則表達式有什麼問題?有人可以幫助我嗎?

+4

可能會改變正則表達式類型。默認值是* Emacs Regular Expressions *,無論如何。 – pavium

+0

http://stackoverflow.com/questions/5635651/linux-find-regex –

+11

這不是offtopic。 – rutherford

回答

217
find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg" 

請注意,您需要在一開始就指定.*/因爲find整個路徑相匹配。

例子:

[email protected]:~/so$ find . -name "*.jpg" 
./foo-111.jpg 
./test/81397018-b84a-11e0-9d2a-001b77dc0bed.jpg 
./81397018-b84a-11e0-9d2a-001b77dc0bed.jpg 
[email protected]:~/so$ 
[email protected]:~/so$ find . -regextype sed -regex ".*/[a-f0-9\-]\{36\}\.jpg" 
./test/81397018-b84a-11e0-9d2a-001b77dc0bed.jpg 
./81397018-b84a-11e0-9d2a-001b77dc0bed.jpg 

我的版本的find:

$ find --version 
find (GNU findutils) 4.4.2 
Copyright (C) 2007 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. 
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b 
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0) 
[email protected]:~/so$ 
[email protected]:~/so$ find . -regextype foo -regex ".*/[a-f0-9\-]\{36\}\.jpg" 
find: Unknown regular expression type `foo'; valid types are `findutils-default', `awk', `egrep', `ed', `emacs', `gnu-awk', `grep', `posix-awk', `posix-basic', `posix-egrep', `posix-extended', `posix-minimal-basic', `sed'. 
+0

我不知道爲什麼,但只有這個工程。謝謝! – thoslin

+0

我不知道這是否只是我的版本,但我得到'發現:未知的正則表達式類型sed;有效的類型是findutils-default,awk,egrep,emacs,gnu-awk,grep posix-awk,posix-basic,posix-egrep,posix-extended' – yarian

+0

我的版本是find(GNU findutils)4.4.2 – thoslin

6

嘗試使用單引號(')來避免shell逃脫你的字符串。請記住,表達式需要匹配整個路徑,即需要看起來像:

find . -regex '\./[a-f0-9-]*.jpg' 

除此之外,似乎我發現(GNU 4.4.2)只知道基本的正則表達式,尤其不能{36 } 句法。我認爲你必須做到沒有它。

56

-regex查找表達式匹配全名,包括當前目錄的相對路徑。對於find .這總是以./開頭,然後是任何目錄。

此外,這些是emacs正則表達式,其中有其他逃避規​​則比正常的egrep正則表達式。

如果這些都直接在當前目錄下,然後

find . -regex '\./[a-f0-9\-]\{36\}\.jpg' 

應該工作。 (我真的不知道 - 我無法計數的重複在這裏工作。)您可以通過切換到-regextype posix-egrep表達式egrep命令:

find . -regextype posix-egrep -regex '\./[a-f0-9\-]{36}\.jpg' 

(注意,這裏的一切說是GNU找到,我不「知道關於BSD一個這也是Mac上默認的東西)

+1

我在我的正則表達式中有多個匹配字符串的括號,所以'posix-egrep'類型爲我工作。 – palswim

+2

需要注意的是,'-regextype'是GNU'find'的一個選項,而不是BSD(至少不是Mac的BSD-like)'find'。如果此選項不可用,請確保安裝GNU find。如果在Brew包裝上使用'findutils',則可以使用Mac。然後通過'gfind'查找。 – DanCat

23

從其他的答案來看,似乎這可能會被發現的錯。

但是你可以做到這一點,而不是這樣:

find . * | grep -P "[a-f0-9\-]{36}\.jpg"

您可能需要調整grep的一點,並使用不同的選項取決於你想要什麼,但它的工作原理。

+0

爲我工作得很好,並提供了與正則表達式有很大程度的自由度。 – glaucon

+1

這個缺點是你不能利用'find'的'-prune'功能來完全跳過某些目錄。大多數情況下這並不重要,但值得一提的是。 –

6

在使用正則表達式應用查找指令時,您應該使用絕對目錄路徑。 在您的例子中,

find . -regex "[a-f0-9\-]\{36\}\.jpg" 

應改爲

find . -regex "./[a-f0-9\-]\{36\}\.jpg" 

在大多數Linux系統上,正則表達式中某些學科不能由該系統所識別,所以你必須明確地指出-regexty像

find . -regextype posix-extended -regex "[a-f0-9\-]\{36\}\.jpg"