spaces in filenames, for do

Hi All,

I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory.

What I have:

#!/bin/bash
sed 's/.*/"&"/' filename.txt > /tmp/files
PAT=`more /tmp/files`
for fname in $PAT
do
find / -name "$fname" -type f -print >> /tmp/found
done
CP=`more /tmp/found`
for fname in $CP
do
cp -v "$fname" dest_dir
done
rm -f /tmp/found /tmp/files

Sed parses the file and adds quotes around the filenames (/tmp/files looks fine), but if I echo "$fname" instead of the find command I can see that it breaks up the filenames into separate lines. So if I have "filename1" and "filename 2" it returns:

"filename1"
"filename
2"

How can I get it to parse correctly? Any help is greatly appreciated!

-Navi

while read N
do
     cp "$N" "$dest_dir"
done <filenames.txt

Thanks for the reply, yes, that works when returning the list, but I need to find the files listed in the filesystem, then copy them from thier found locations to the destdir.

Using your suggestion I tried:

sed 's/.*/"&"/' filenames.txt > /tmp/files
while read N
do
find / -name "$N" -type f -print >> /tmp/found
done < /tmp/files

But the /tmp/found file is empty :frowning:

I get the proper list if I replace the find command with echo "$N"...:confused:

That is what

cp "$N" "$dest_dir"

was doing.

If you need to prefix a directory path to the source then do so.

cp "$src_dir/$N" "$dest_dir"

That's the problem, I don't know the $src_dir, I need to search the filesystem to find the full paths. The files are spread all over the place, hence the find command.