开发者

bash: Filtering out directories and extensions from find?

开发者 https://www.devze.com 2023-01-10 06:14 出处:网络
I\'m trying to find files modified recently with this find . -mtime 0 Which gives me en/content/file.xml

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 en and es directories but would like to grab anything else. In addition, I'd like to filter out .swp files from the results of those.

So I want to get back:

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

In addition to every other file not within es/en and not end开发者_StackOverflow社区ing in .swp


properly, just in find:

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


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

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


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

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


Try this:

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

Adding the cap character 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.

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

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


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 . -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.*' \)
0

精彩评论

暂无评论...
验证码 换一张
取 消