Reputation: 1837
I've tried using
find . | grep -v '(.ext1|.ext2)$'
But it still returns those files ending with extensions .ext1
and .ext2
. Then I tried the following:
ls | grep -v ".ext1$" | grep -v ".ext2$"
and this works how I want it. Is there another way to do what I'm looking for ? All I want to do is list all files or directories that do NOT end in .ext1
or .ext2
.
Upvotes: 2
Views: 8239
Reputation: 753970
You are using an extended regular expression (ERE) which requires grep -E
or egrep
:
find . | grep -E -v '\.(ext1|ext2)$'
find . | egrep -v '\.(ext1|ext2)$'
I've also escaped the .
so it is not treated as a metacharacter.
As you wrote it (grep -v '(.ext1|.ext2)$'
) the parentheses and pipe are not metacharacters (but .
and $
are), so the command would eliminate names such as:
(Xext1|Yext2)
function(Aext1|Bext2)
Where the parentheses and pipe are literally a part of the file name.
Upvotes: 0
Reputation: 2408
You have not escaped the .
try this . It will work. Remember .
needs to be escaped.
ls | grep -v '\.ext1$' | grep -v '\.ext2$'
same for your find
find . | grep -v '(\.ext1|\.ext2)$'
Hope that helps :)
Upvotes: 3
Reputation: 212979
You can do it all with find
:
find . \! -name \*.ext1 \! -name \*.ext2
Upvotes: 1