Question

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.

Was it helpful?

Solution

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 :)

OTHER TIPS

You can do it all with find:

find . \! -name \*.ext1 \! -name \*.ext2

This should work :

ls | grep "[^(\.ext1|\.ext2)]$"

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top