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

2019-08-31 17:52发布

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?

标签: path find
1条回答
Juvenile、少年°
2楼-- · 2019-08-31 18:46

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`
查看更多
登录 后发表回答