views:

31

answers:

1

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 filename" ; 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
A: 

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.)

Dennis Williamson
Very nice. Works great. I was thinking too hard.
Dispelwolf