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

Posted by Dispelwolf on Stack Overflow See other posts from Stack Overflow or by Dispelwolf
Published on 2010-05-09T17:06:02Z Indexed on 2010/05/09 19:08 UTC
Read the original article Hit count: 156

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about bash

Related posts about replace