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

我的梦境 提交于 2019-12-25 03:38:06

问题


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`


来源:https://stackoverflow.com/questions/34763736/how-to-get-path-argument-from-file-for-find-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!