bash:从查找中过滤掉目录和扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3400031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:25:54  来源:igfitidea点击:

bash: Filtering out directories and extensions from find?

bashfind

提问by meder omuraliev

I'm trying to find files modified recently with this

我正在尝试查找最近用此修改的文件

find . -mtime 0

Which gives me

这给了我

en/content/file.xml
es/file.php
en/file.php.swp
css/main.css
js/main.js

But I'd like to filter out the enand esdirectories but would like to grab anything else. In addition, I'd like to filter out .swpfiles from the results of those.

但我想过滤掉enes目录,但想抓住其他任何东西。另外,我想.swp从这些结果中过滤掉文件。

So I want to get back:

所以我想回来:

css/main.css
js/main.js
xml/foo.xml

In addition to every other file not within es/enand not ending in .swp

除了所有其他文件不在es/en其中且不以.swp

回答by mvds

properly, just in find:

正确地,只是在找到:

find -mtime 0 -not \( -name '*.swp' -o -path './es*' -o -path './en*' \)

回答by Chen Levy

The -prunecommand prevents find form descending down the directories you wish to avoid:

-prune命令可防止 find 表单从您希望避免的目录下降:

find . \( -name en -o -name es \) -prune , -mtime 0 ! -name "*.swp"

回答by speshak

find . -mtime 0 | grep -v '^en' | grep -v '^es' | grep -v .swp

The -v flag for grep makes it return all lines that don'tmatch the pattern.

grep 的 -v 标志使其返回所有与模式匹配的行。

回答by mmonem

Try this:

尝试这个:

find . -mtime 0 | grep -v '^en' | grep -v '^es'

Adding the capcharacter at the beginning of the pattern given to grep ensures that it is a must to find the pattern at the start of the line.

在提供给 grep 的模式的开头添加大写字符确保必须在行的开头找到模式。

Update:Following Chen Levy'scomment(s), use the following instead of the above

更新:按照Chen Levy 的评论,使用以下代替上面的

find . -mtime 0 | grep -v '^\./en' | grep -v '^\./es'

find is great but the implementation in various UNIX versions differs, so I prefer solutions that are easier to memorize and using commands with more standard options

find 很棒,但在各种 UNIX 版本中的实现不同,所以我更喜欢更容易记忆和使用带有更多标准选项的命令的解决方案

回答by karl

The -regex option of find(1) (which can be combined with the -E option to enable extended regular expressions) matches the whole file path as well.

find(1) 的 -regex 选项(可以与 -E 选项结合使用以启用扩展正则表达式)也匹配整个文件路径。

find . -mtime 0 -not \( -name '*.swp' -o -regex '\./es.*' -o -regex '\./en.*' \)
find "$(pwd -P)" -mtime 0 -not \( -name '*.swp' -o -regex '.*/es.*' -o -regex '.*/en.*' \)