How to get “! -path” argument from file for find c

2019-08-31 17:55发布

问题:

I would need a script that can find files with the given extension on the given path, but excluding directories, their name taken from a file.

We have a script.sh and a white.list file. white.list contains:

/path/something

/path/someotherthing

Script has:

whitelistfile=/scriptdir/white.list
whitelist=`cat $whitelistfile`

if test -n "$whitelist"
then
    # collect whitelist paths
    for listitem in $whitelist;
    do
        excludepath+="! -path \"${listitem}*\" "
    done

files=`find "$path" $excludepath -name *."$3" -type f`

fi

echo $files

The problem is that find does not accept $excludepath argument. Says there is no such directory.

Can someone help me with this, please?

回答1:

You'd use an array, as I explained in your other question. Bash FAQ has a thing about reading lines from a file.

so:

whitelistfile=/scriptdir/white.list

while IFS= read -r; do
  excludepath+=(! -path "$REPLY")
done <$whitelistfile
[[ $REPLY ]] && excludepath+=(! -path "$REPLY")

files=`find "$path" "${excludepath[@]}" -name "*.$3" -type f`


标签: path find