Bash - replacing targeted files with a specific file, whitespace in directory names
- by Dispelwolf
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