Skip to content
Advertisement

Linux Find command- exclude find based on file name

I feel like this is a ridiculously easy question but I cannot find a simple regex answer for this. Basically, I am trying to use find to get a list of all files in my system with some exclusions. One of these exclusions is any file that ends in .Foo.cs, or any file named FooInfo.cs. I have successfully excluded a couple directories from my search, but cannot seem to exclude these two files. I’ve tried using -name, but would -name even work for this? Below is my expression. Thanks.

find . ! -name 'FooInfo.cs' ! -name '*.Foo.cs'  -type d ( -name Foo-o -name 2Foo -o -name 2_Foo ) -prune -o -type f ! -size 0 ( -name "*.java" -o -name "*.cs" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c" -o -name "*.h" -o -name "*.scala" -o -name "*.css" -o -name "*.html" -o -name "*.bat" -o -name "*.js" ) -exec realpath {} ;| xargs grep -L CUSTOMERINFO  | sed -e 's/$/r/g' >> ../output.txt

Advertisement

Answer

So I’m not sure why, but I ended up fixing this by changing the order of what I’m excluding. Instead of excluding at the very beginning, the following worked (moving the ! -name '.FOO.cs' and ! -name '.fooinfo.cs' to right after the declaration type -f).

I’m assuming this worked because they are files so they must be flagged with type -f. But please comment and correct below if you know why.

find . -type d ( -name Foo-o -name 2Foo -o -name 2_Foo ) -prune -o -type f ! -size 0 ! -name 'FooInfo.cs' ! -name '*.Foo.cs'   ( -name "*.java" -o -name "*.cs" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c" -o -name "*.h" -o -name "*.scala" -o -name "*.css" -o -name "*.html" -o -name "*.bat" -o -name "*.js" ) -exec realpath {} ;| xargs grep -L CUSTOMERINFO  | sed -e 's/$/r/g' >> ../output.txt
Advertisement