How to read filenames with space in between


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read filenames with space in between
# 1  
Old 05-16-2006
CPU & Memory 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. Smilie
# 2  
Old 05-16-2006
try
Code:
ls -1 | while read filename
do
  echo "$filename" 
done

# 3  
Old 05-16-2006
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.
# 4  
Old 05-17-2006
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

# 5  
Old 05-17-2006
you could also set the internal-field-seperator to a newline, if you use bash

IFS="
"

check man bash /IFS
# 6  
Old 05-17-2006
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.
# 7  
Old 05-18-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Read space in script

Hi All, Please let me know how to read space between 2 words in script. It is reading only first chars ES,NZ. But after space it is not reading next 3 chars. ES LPI NZ GBL FR LPI DK LPI for v_sob_cntry_cd in `cat /shared/set_of_books_cntry_cd_list.lst` do ... (6 Replies)
Discussion started by: kiranparsha
6 Replies

2. Solaris

Read Only Permission after the space is full.

Hi, Is there any chance that a file system that mounted on the server becomes read only when the space in that file system becomes full? Regards, Sreejith (9 Replies)
Discussion started by: Sreejith K
9 Replies

3. Shell Programming and Scripting

space after read prompt?

Hello, Unix-Forums. How can I make a Space after a read prompt? let's assume: read -p "Are you good?:" varthe output would be ( | is the cursor ): Are you good?:|But I want it to be: Are you good?: |That's what I mean. How would I do that? (2 Replies)
Discussion started by: intelinside
2 Replies

4. Shell Programming and Scripting

Read filenames in sorted order

Hi , My requirement is to scan a directory for file names with LTR.PDF* and send those files via ftp to another server one by one. Now the the problem is file names are like LTR.PDF ,LTR.PDF1 ,LTR.PDF2.....LTR.PDF10..upto 99 and these needs to be sent in sorted order. is there a way to get... (10 Replies)
Discussion started by: nishantrk
10 Replies

5. Shell Programming and Scripting

How to read the filenames with space character in it

Hi, My Requirement is to read the filenames which possess space charatcer in it and pass the same file name to the other shell script as input parameter. Please provide your valuable suggestion. (5 Replies)
Discussion started by: cnraja
5 Replies

6. Solaris

How to read EMC disk space

Hi I am using Solaris8.I want to find the total disk space of my server.Can anyone please tell that which field in below mentioned code signifies the disk space and whether this space is in Mb or GB. c11t0d52 <EMC-SYMMETRIX-5264 cyl 4 alt 2 hd 15 sec 64> ... (4 Replies)
Discussion started by: sharmaankur85
4 Replies

7. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

8. UNIX for Advanced & Expert Users

how can I read the space in the end of line

cat file1|while read i do echo "$i"|wc done with this command the space in the end of the line not considered how can solve that for example: read h "hgyr " echo "$h"|wc 4 (2 Replies)
Discussion started by: Ehab
2 Replies

9. Shell Programming and Scripting

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

10. Shell Programming and Scripting

How to keep white space is being deleted using read

I am using Posix shell to write a script. The problem I am having is that when I use the read command to go through a file I lose the tabs. How can I keep this from happening? (1 Reply)
Discussion started by: keelba
1 Replies
Login or Register to Ask a Question