The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 12:59 PM
How to delete multiple space or tabs from a read only file bisla.yogender Shell Programming and Scripting 6 05-14-2008 04:38 AM
how can I read the space in the end of line Ehab UNIX for Advanced & Expert Users 2 09-30-2007 07:13 AM
read list of filenames from text file, archive, and remove fxvisions Shell Programming and Scripting 5 03-20-2007 06:56 PM
How to keep white space is being deleted using read keelba Shell Programming and Scripting 1 05-30-2002 11:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-16-2006
Registered User
 

Join Date: Apr 2006
Posts: 15
Arrow How to read filenames with space in between

Hi All,

I need to read filenames with space in between in a for loop
like
Integration of utility projects
Integration of hdf projects

I copied these files from a windows system and as you know windows filename has spaces in between them.

But while unix is reading the filename in a for loop it reads word by word and in the end it's futile because all the filenames has space in between.

This is the command I use to read the filenames

for i in `ls -l`

Is there a way to accomplish this. Please help me on this.
Reply With Quote
Forum Sponsor
  #2  
Old 05-16-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
try
Code:
ls -1 | while read filename
do
  echo "$filename" 
done
Reply With Quote
  #3  
Old 05-16-2006
Registered User
 

Join Date: Apr 2006
Posts: 15
Thanks a lot.

It worked fine.

Can you please explain why is this not possible in a for loop instead of a while loop? What makes the difference.
Reply With Quote
  #4  
Old 05-16-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
Hi,

There is no reason that it doesn't work with for loop, try this:
Code:
for i in "`ls -1`"
do
echo "$i"
done
Reply With Quote
  #5  
Old 05-17-2006
Registered User
 

Join Date: Jan 2004
Posts: 17
you could also set the internal-field-seperator to a newline, if you use bash

IFS="
"

check man bash /IFS
Reply With Quote
  #6  
Old 05-17-2006
LivinFree's Avatar
Goober Extraordinaire
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Notice that the solution is 'ls -1' (one, not "el"). That puts each file on it's own line. When you read it, you're assigning the whole line to a variable, not per whitespace-delimited word.
Reply With Quote
  #7  
Old 05-18-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by macosta
Notice that the solution is 'ls -1' (one, not "el"). That puts each file on it's own line. When you read it, you're assigning the whole line to a variable, not per whitespace-delimited word.
Infact you dont need an "ls -1" as such. A simple ls would do. In this case, the output of ls -1 and ls is the same. The output would be a file per line.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0