bash 在 shell 中查找除 *.xml 文件之外的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12602936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find all files except e.g. *.xml files in shell
提问by paweloque
Using bash, how to find files in a directory structure except for *.xml files? I'm just trying to use
使用bash,如何在目录结构中查找*.xml 文件以外的文件?我只是想使用
find . -regex ....
regexe:
正则:
'.*^((?!xml).)*$'
but without expected results...
但没有预期的结果......
or is there another way to achieve this, i.e. without a regexp matching?
还是有另一种方法来实现这一点,即没有正则表达式匹配?
回答by Pepelac
find . ! -name "*.xml" -type f
find . ! -name "*.xml" -type f
回答by verdesmarald
find . -not -name '*.xml'
Should do the trick.
应该做的伎俩。
回答by Andy Lester
Sloppier than the findsolutions above, and it does more work than it needs to, but you could do
比find上面的解决方案更草率,它做的工作比它需要的要多,但你可以做
find . | grep -v '\.xml$'
Also, is this a tree of source code? Maybe you have all your source code and some XML in a tree, but you want to only get the source code? If you were using ack, you could do:
另外,这是一棵源代码树吗?也许您在树中拥有所有源代码和一些 XML,但您只想获取源代码?如果你使用ack,你可以这样做:
ack -f --noxml
回答by glenn Hymanman
with bash:
使用 bash:
shopt -s extglob globstar nullglob
for f in **/*!(.xml); do
[[ -d $f ]] && continue
# do stuff with $f
done
回答by Marius
You can also do it with or-ring as follows:
您也可以使用 or-ring 执行以下操作:
find . -type f -name "*.xml" -o -type f -print
find . -type f -name "*.xml" -o -type f -print
回答by Anselm
Try something like this for a regex solution:
为正则表达式解决方案尝试这样的事情:
find . -regextype posix-extended -not -regex '^.*\.xml$'

