0

I have a script that recursively searches all directories for specific files or specific file endings. These certain files I want to save the path in a description file. Looks for example like this:

./org/apache/commons/.../file1.pom
./org/apache/commons/.../file1.jar
./org/apache/commons/.../file1.zip

and so on.

In a blacklist , I describe which file endings I want to ignore.

! -path "./.cache/*" ! -path "./org/*" ! -name "*.sha1" ! -name"*.lastUpdated"

and so on.

Now i want to read this blacklist file while the search to ignore the described files:

find . -type f $(cat blacklist) > artifact.descriptor

Unfortunately, the blacklist will not be included while the search. When:

echo "find . -type f $(cat blacklist) > artifact.descriptor"

Result is as expected:

find . -type f ! -path "./.cache/*" ! -path "./org/*" ! -name "*.sha1" ! -name"*.lastUpdated" > artifact.descriptor

But it does not work or exclude the described files. I tried with following command and it works, but i want to know why not with with find alone.

find . -type f | grep -vf  $blacklist  > artifact.descriptor

Hopefully someone can explain it to me :) Thanks a lot.

1 Answers1

1

As tripleee suggests, it is generally considered bad practice to store a command in a variable because it does not catch all the cornercases. However you can use eval as a workaround

/tmp/test$ ls
blacklist  test.a  test.b  test.c
/tmp/test$ cat blacklist 
-not -name *.c -not -name *.b
/tmp/test$ eval "find . -type f "`cat blacklist`
./test.a
./blacklist

In your case I think it fails because the quotes in your blacklist file are considered as a literal and not as enclosing the patterns and I think it works if you remove them, but still it's probably not safe for other reasons.

! -path ./.cache/* ! -path ./org/* ! -name *.sha1 ! -name *.lastUpdated
Community
  • 1
  • 1
Emilien
  • 2,385
  • 16
  • 24