I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}"
.
我试图在一个目录中获取一个包含所有python和html文件的列表,其中包含命令查找文档名称“*.{py,html}”。
Then along came the man page:
接着,男人的页面出现了:
Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2.
模式中的括号(“{}”)不被认为是特殊的(即find)。-name 'foo{1,2}'匹配一个名为foo{1,2}的文件,而不是文件foo1和foo2。
As this is part of a pipe-chain, I'd like to be able to specify which extensions it matches at runtime (no hardcoding). If find just can't do it, a perl one-liner (or similar) would be fine.
由于这是管道链的一部分,我希望能够指定它在运行时匹配的扩展(没有硬编码)。如果find不能做到这一点,那么perl的一行程序(或类似的)就可以了。
Edit: The answer I eventually came up with include all sorts of crap, and is a bit long as well, so I posted it as an answer to the original itch I was trying to scratch. Feel free to hack that up if you have better solutions.
编辑:我最终想出的答案包括各种各样的废话,而且也有点长,所以我把它作为我想要抓的原始发痒的答案。如果您有更好的解决方案,请随意修改。
10 个解决方案
#1
347
Use -o
, which means "or":
使用-o,表示“或”:
find Documents \( -name "*.py" -o -name "*.html" \)
Edit: Sorry, just re-read the question... you'd need to build that command line programmatically, which isn't that easy.
编辑:抱歉,重新读一下问题……您需要以编程方式构建该命令行,这并不容易。
Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:
您是否使用bash(或在Windows上使用Cygwin)?如果你是,你应该可以这样做:
ls **/*.py **/*.html
which might be easier to build programmatically.
这可能更容易以编程方式构建。
Edit: Applied @artbristol comment to the answer.
编辑:应用@artbristol评论答案。
#2
40
Some editions of find, mostly on linux systems, possibly on others aswell support -regex and -regextype options, which finds files with names matching the regex.
一些版本的find,主要在linux系统上,可能还在其他的aswell支持-regex和-regextype选项中,这些选项找到与regex匹配的文件名。
for example
例如
find . -regextype posix-egrep -regex ".*\.(py|html)$"
should do the trick in the above example. However this is not a standard POSIX find function and is implementation dependent.
应该在上面的例子中做这个技巧。然而,这并不是一个标准的POSIX查找函数,而是依赖于实现。
#3
25
You could programmatically add more -name
clauses, separated by -or
:
您可以以编程方式添加更多的-名称子句,由-或:
find Documents \( -name "*.py" -or -name "*.html" \)
Or, go for a simple loop instead:
或者,选择一个简单的循环:
for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
#4
9
I had a similar need. This worked for me:
我也有类似的需求。这工作对我来说:
find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
#5
4
This will find all .c or .cpp files on linux
这将在linux上找到所有的.c或.cpp文件。
$ find . -name "*.c" -o -name "*.cpp"
You don't need the escaped parenthesis unless you are doing some additional mods. Here from the man page they are saying if the pattern matches, print it. Perhaps they are trying to control printing. In this case the -print acts as a conditional and becomes an "AND'd" conditional. It will prevent any .c files from being printed.
除非你在做额外的动作,否则你不需要圆括号。在这里,他们说如果模式匹配,打印出来。也许他们正试图控制印刷。在这种情况下,-print作为一个条件,成为一个“和”条件。它将防止任何.c文件被打印。
$ find . -name "*.c" -o -name "*.cpp" -print
But if you do like the original answer you can control the printing. This will find all .c files as well.
但是如果你喜欢原始的答案,你可以控制打印。这将找到所有的.c文件。
$ find . \( -name "*.c" -o -name "*.cpp" \) -print
#6
3
My default has been:
我默认了:
find -type f | egrep -i "*.java|*.css|*.cs|*.sql"
查找-类型f |白鹭-我“*.java|*.css|*.cs|*.sql”
Like the less process intencive find
execution by Brendan Long and Stephan202 et al.:
就像Brendan Long和Stephan202等人发现的更少的过程一样。
find Documents \( -name "*.py" -or -name "*.html" \)
查找文档\(-name“*”。py”——- name”*。html " \)
#7
2
#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done
simple but works :)
简单,但工作原理:)
#8
1
I needed to remove all files in child dirs except for some files. The following worked for me (three patterns specified):
除了一些文件外,我需要删除孩子的所有文件。以下为我工作(指定三种模式):
find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
#9
1
find MyDir -iname "*.[j][p][g]"
+
find MyDir -iname "*.[b][m][p]"
=
find MyDir -iname "*.[jb][pm][gp]"
#10
0
This works on AIX korn shell.
这在AIX korn shell上工作。
find *.cbl *.dms -prune -type f -mtime -1
This is looking for *.cbl
or *.dms
which are 1 day old, in current directory only, skipping the sub-directories.
这是在寻找*。cbl或*。在当前目录中只有1天的dms跳过子目录。
#1
347
Use -o
, which means "or":
使用-o,表示“或”:
find Documents \( -name "*.py" -o -name "*.html" \)
Edit: Sorry, just re-read the question... you'd need to build that command line programmatically, which isn't that easy.
编辑:抱歉,重新读一下问题……您需要以编程方式构建该命令行,这并不容易。
Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:
您是否使用bash(或在Windows上使用Cygwin)?如果你是,你应该可以这样做:
ls **/*.py **/*.html
which might be easier to build programmatically.
这可能更容易以编程方式构建。
Edit: Applied @artbristol comment to the answer.
编辑:应用@artbristol评论答案。
#2
40
Some editions of find, mostly on linux systems, possibly on others aswell support -regex and -regextype options, which finds files with names matching the regex.
一些版本的find,主要在linux系统上,可能还在其他的aswell支持-regex和-regextype选项中,这些选项找到与regex匹配的文件名。
for example
例如
find . -regextype posix-egrep -regex ".*\.(py|html)$"
should do the trick in the above example. However this is not a standard POSIX find function and is implementation dependent.
应该在上面的例子中做这个技巧。然而,这并不是一个标准的POSIX查找函数,而是依赖于实现。
#3
25
You could programmatically add more -name
clauses, separated by -or
:
您可以以编程方式添加更多的-名称子句,由-或:
find Documents \( -name "*.py" -or -name "*.html" \)
Or, go for a simple loop instead:
或者,选择一个简单的循环:
for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
#4
9
I had a similar need. This worked for me:
我也有类似的需求。这工作对我来说:
find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
#5
4
This will find all .c or .cpp files on linux
这将在linux上找到所有的.c或.cpp文件。
$ find . -name "*.c" -o -name "*.cpp"
You don't need the escaped parenthesis unless you are doing some additional mods. Here from the man page they are saying if the pattern matches, print it. Perhaps they are trying to control printing. In this case the -print acts as a conditional and becomes an "AND'd" conditional. It will prevent any .c files from being printed.
除非你在做额外的动作,否则你不需要圆括号。在这里,他们说如果模式匹配,打印出来。也许他们正试图控制印刷。在这种情况下,-print作为一个条件,成为一个“和”条件。它将防止任何.c文件被打印。
$ find . -name "*.c" -o -name "*.cpp" -print
But if you do like the original answer you can control the printing. This will find all .c files as well.
但是如果你喜欢原始的答案,你可以控制打印。这将找到所有的.c文件。
$ find . \( -name "*.c" -o -name "*.cpp" \) -print
#6
3
My default has been:
我默认了:
find -type f | egrep -i "*.java|*.css|*.cs|*.sql"
查找-类型f |白鹭-我“*.java|*.css|*.cs|*.sql”
Like the less process intencive find
execution by Brendan Long and Stephan202 et al.:
就像Brendan Long和Stephan202等人发现的更少的过程一样。
find Documents \( -name "*.py" -or -name "*.html" \)
查找文档\(-name“*”。py”——- name”*。html " \)
#7
2
#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done
simple but works :)
简单,但工作原理:)
#8
1
I needed to remove all files in child dirs except for some files. The following worked for me (three patterns specified):
除了一些文件外,我需要删除孩子的所有文件。以下为我工作(指定三种模式):
find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
#9
1
find MyDir -iname "*.[j][p][g]"
+
find MyDir -iname "*.[b][m][p]"
=
find MyDir -iname "*.[jb][pm][gp]"
#10
0
This works on AIX korn shell.
这在AIX korn shell上工作。
find *.cbl *.dms -prune -type f -mtime -1
This is looking for *.cbl
or *.dms
which are 1 day old, in current directory only, skipping the sub-directories.
这是在寻找*。cbl或*。在当前目录中只有1天的dms跳过子目录。