-1

I'm using a file to pass in paths to exclude from a find. I can generate the command and run it fine from the command-line but when I try to execute it from the script I get an error.

Here's my script:

#!/usr/bin/env bash

nots=$(while read line; do
  echo -n "-not -path \"*/${line}/*\" "
done < exclude_directories.txt)

echo $nots

echo "Run this command:"
echo "find ~/files -type f $nots > ~/files.txt"

find ~/files -type f $nots > ~/files.txt

When I run the script the find command return every file - even those specified in the nots.

Here's the output with set enabled:

$ ./dump_all_local_repo_files.sh

nots=$(while read line; do
  echo -n "-not -path \"*/${line}/*\" "
done < exclude_directories.txt)
++ read line
++ echo -n '-not -path "*/.git/*" '
++ read line
++ echo -n '-not -path "*/vendor/*" '
++ read line
++ echo -n '-not -path "*/external/*" '
++ read line
++ echo -n '-not -path "*/node_modules/*" '
++ read line
++ echo -n '-not -path "*/test/*" '
++ read line
+ nots='-not -path "*/.git/*" -not -path "*/vendor/*" -not -path "*/external/*" -not -path "*/node_modules/*" -not -path "*/test/*" '

echo $nots
+ echo -not -path '"*/.git/*"' -not -path '"*/vendor/*"' -not -path '"*/external/*"' -not -path '"*/node_modules/*"' -not -path '"*/test/*"'
-not -path "*/.git/*" -not -path "*/vendor/*" -not -path "*/external/*" -not -path "*/node_modules/*" -not -path "*/test/*"

echo "Run this command:"
+ echo 'Run this command:'
Run this command:
echo "find ~/files -type f $nots > ~/files.txt"
+ echo 'find ~/files -type f -not -path "*/.git/*" -not -path "*/vendor/*" -not -path "*/external/*" -not -path "*/node_modules/*" -not -path "*/test/*"  > ~/files.txt'
find ~/files -type f -not -path "*/.git/*" -not -path "*/vendor/*" -not -path "*/external/*" -not -path "*/node_modules/*" -not -path "*/test/*"  > ~/files.txt

find ~/files -type f $nots > ~/files.txt
+ find /Users/ken/files -type f -not -path '"*/.git/*"' -not -path '"*/vendor/*"' -not -path '"*/external/*"' -not -path '"*/node_modules/*"' -not -path '"*/test/*"'
user3063045
  • 2,019
  • 2
  • 22
  • 35
  • consider prefacing the code with `set -xv` (debug mode), run the code, update the question with the debug output so we can see what's in `$nots` and the actual `find` command that's being executed – markp-fuso Jan 09 '22 at 21:49
  • Please add command which runs fine from command-line to your question (no comment here). – Cyrus Jan 09 '22 at 21:49
  • sooo `-path '"*/.git/*"'` do you see the problem? No, quoting by putting `"` inside a string is invalid, `"` has no effect there. – KamilCuk Jan 10 '22 at 18:09
  • I do see that now. Echo doesn't add the single quotes.`-path "*/.git/*"` works fine. – user3063045 Jan 10 '22 at 18:21
  • Perhaps see also https://stackoverflow.com/a/70364976 for some ideas you could try. – tripleee Jan 10 '22 at 18:42

1 Answers1

1

Does your version of find really support a -not operator? Usually it is spelled !.

To find all non-directories you would use

find . \! -type d

You also should remove the quotes in

find ~/files -type f "$nots"

since with the quotes there's no word-splitting. That said,

 nots=$(while read line; do
   echo -n "-not -path */${line}/* "
   done < exclude_directories.txt)
 find ~/files -type f $nots > ~/files.txt

should work.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • As I mentioned in the question I can run the command fine in the shell but it fails in the script. – user3063045 Jan 10 '22 at 13:54
  • @user3063045 No you can't, you're running a different command. The following will not work and elicit the exact error you posted: `nots="-not -path foo"; find . "$nots"`. The first problem is the double quotes. – Jens Jan 10 '22 at 14:13
  • I've updated the question. Removing the quotes allows me to run the command but find returns everything instead of everything minus the nots. – user3063045 Jan 10 '22 at 18:01
  • @user3063045 You should also remove the backslash/double quotes in `\"*/${line}/*\"`. – Jens Jan 10 '22 at 18:25
  • Removing the backslash/double quotes in \"*/${line}/*\" worked! Please update the answer. – user3063045 Jan 10 '22 at 18:33
  • @user3063045 Wonderful! I've updated the answer. – Jens Jan 10 '22 at 20:41