Moving files with space, in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files with space, in for loop
# 1  
Old 05-07-2004
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!
# 2  
Old 05-07-2004
put $x in quotes -

Code:
 "$x"

# 3  
Old 05-07-2004
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:

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

# 4  
Old 05-10-2004
Excellent

Nice one Perderabo, it works perfectly.

That is why I have always maintained you are the UNIX king.
# 5  
Old 05-10-2004
one more solution on the same line is

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

This will work.



Ramesh
# 6  
Old 02-04-2009
not working in my code

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!!!!!!
# 7  
Old 02-04-2009
Quote:
Originally Posted by yabhi_22
Can any one help me on this!!!!!!
Open a new thread for your problem.
Code:
# 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Space moving to next column (awk HTML)

Hi I have create a report and have converted the text output to HTML but in the output there is a sentence "The transaction was aborted by the user.", the spaces between this sentence is considered as separate column. How can I overcome the same? I am providing my code, text output and... (7 Replies)
Discussion started by: Dumpi16
7 Replies

2. UNIX for Beginners Questions & Answers

For loop with space in file name

Hi All I have a source file named ABC-20150613 to 20150613.zip. I was trying to execute the below command on this source file, but its telling file is not available in that path and giving me some random file names. ls -ltr| for z in ABC-????????*to*????????*.zip do unzip $z -d done I... (5 Replies)
Discussion started by: ginrkf
5 Replies

3. Shell Programming and Scripting

Moving multiple filetype in a single loop

Hi, I am using the below code to move *.sh files to another directory. use File::Copy qw(move); while(<C:/Users/pandeesh/Desktop/*.sh>) { move $_,"C:/Users/pandeesh/Desktop/Projects"; } My requirement is i want to move *.sh,*.txt,*.xlsx,*.doc,*.pdf and *.epub files to the specified... (2 Replies)
Discussion started by: pandeesh
2 Replies

4. Red Hat

On CentOS, moving space from large free directory to another

Hi. My "/usr" folder is running out of space. My "/home" folder is quite large and has a lot of free space. As follows: Filesystem Type Size Used Avail Use% Mounted on ... /dev/sda5 ext3 9.7G 2.6G 6.7G 28% / /dev/sda7 ext3 152G 16G 128G 11% /home /dev/sda3 ... (7 Replies)
Discussion started by: pkiula
7 Replies

5. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

6. AIX

Moving free space from one LV to another LV

in the same VG? Is there a way we can do this? We basically have a test server that used to be a production server. Now the newly created test directories have run out of space and the old production directories have alot of free space. Can we transfer that free space over? If so how? Have... (2 Replies)
Discussion started by: NycUnxer
2 Replies

7. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

8. Shell Programming and Scripting

Escape space in for loop

I have a file with the following contents # more hello.txt man hello man whereru The shell script i have tries to echo the contents of the file hello.txt for i in `cat hello.txt` do echo $i done but the output i am getting is taking the space as a new line.. #... (3 Replies)
Discussion started by: Tuxidow
3 Replies

9. UNIX for Dummies Questions & Answers

Use of For loop with Space

Hi, I am trying to query the database to get the list of portfolio and for each portfolio, I am using the for loop, but the problem is some of the portfolio is having the spaces. The Code PORT=`${EFG_ISQL} -b <<-! set nocount on use ${EFG_DB} go select portId from PORTFOLIO go... (3 Replies)
Discussion started by: peter35james
3 Replies

10. UNIX for Advanced & Expert Users

moving space from one partition to another

How can I move some space allocated to one partition to another, i.e. from "/var" to "/" . Thanks! (4 Replies)
Discussion started by: jason6792
4 Replies
Login or Register to Ask a Question