开发者

Bash - replacing targeted files with a specific file, whitespace in directory names

开发者 https://www.devze.com 2022-12-29 12:54 出处:网络
I have a large directory tree of files, and am using the following script to list and replace a searched-for name with a specific file. Problem is, I don\'t know how to write the createList() for-loop

I have a large directory tree of files, and am using the following script to list and replace a searched-for name with a specific file. Problem is, I don't know how to write the createList() for-loop correctly to account for whitespace in a directory name. If all directories don't have spaces, it works fine.

The output is a list of files, and then a list of "cp" commands, but reports directories with spaces in them as individual dirs.

aindex=1
files=( null )

[ $# -eq 0 ] && { echo "Usage: $0 fil开发者_如何学JAVAename" ; exit 500; }

createList(){
    f=$(find . -iname "search.file" -print)
    for i in $f
    do
        files[$aindex]=$(echo "${i}")
            aindex=$( expr $aindex + 1 )
    done
    }

writeList() {
    for (( i=1; i<$aindex; i++ ))
    do
        echo "#$i : ${files[$i]}"
    done
    for (( i=1; i<$aindex; i++ ))
    do
        echo "/usr/bin/cp /cygdrive/c/testscript/TheCorrectFile.file ${files[$filenumber]}"
    done
}

createList
writeList


Replace your entire script with these four lines:

find . -iname "search.file" | while read file
do 
    /usr/bin/cp /cygdrive/c/testscript/TheCorrectFile.file "$file"
done

(But isn't cp in /bin? Oh, on Cygwin it's in both. It's more portable, though, to use /bin.)

0

精彩评论

暂无评论...
验证码 换一张
取 消