Moving files with space, in for loop

Hi All
I need to put a bunch of specific files in a directory (with loads of other files), into a tar archive. The best way I thought of doing this was putting the filenames into a file, reading them line by line in a for loop, and then adding them to a tar acrhive.

However the filenames have spaces:

/the/path/to/the/file/the file name with spaces

So when I do a for x in `cat /listoffiles` etc, it doesn't work because the shell sees the spaces as separators, and hence the bits of the name as separate files.

Anyone got a resolution to this? If I list the filename at the command line, enclosed by quotes, it's OK - but when I put $x in quotes in my for loop, it doesn't work.

Thanks for any help!

put $x in quotes -

 "$x"

jim's solution will not work. The problem is "for x in `cat listoffiles`" will spilt the contents of listoffiles on whitespace. So with the example given, x will be "the" then it will be "file". And so on. Placing $x in quotes isn't enough because it's too late...the filename was split before x got a value.

My solution would be:

#! /usr/bin/ksh
exec < listoffiles
while read x ; do
       somecommand "$x"
done

Nice one Perderabo, it works perfectly.

That is why I have always maintained you are the UNIX king.

one more solution on the same line is

while read line
do
echo "$line"
done < file_name;

This will work.

Ramesh

Hi All,
I am using the same while syntax for reading my file but its not working for my code.
Below is the line in my file
" 123 rteyu 566"
when I use below code the spaces are shifted especially for 1st variable
while read line
do
x=`echo "$line"|cut -c 1-7`
y=`echo "$line"|cut -c 8-15`
echo "$x"
echo "$y"
done < file

x is showing value "123 ".
The intial spacing is being trucated.
Can any one help me on this!!!!!!

Open a new thread for your problem.

# while read line
> do
> x=`echo "$line"|cut -c 1-7`
> y=`echo "$line"|cut -c 8-15`
> echo "$x"
> echo "$y"
> done < file
" 123 r
teyu 566