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
# 8  
Old 05-18-2006
Quote:
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.
Not So,

default output of a simple ls is multiple column output with minimal of 2-columns and ls -1 enables single column output.

Output of single file per line is guaranteed with ls -1 only
# 9  
Old 05-18-2006
Quote:
Originally Posted by matrixmadhan
Not So,

default output of a simple ls is multiple column output with minimal of 2-columns and ls -1 enables single column output.

Output of single file per line is guaranteed with ls -1 only
Not so, Smilie

What ls does depends on the version of ls. I am aware of two behaviors:

1. ls defaults to one filename per line. (the original, but now rare behavior.)

2. ls examines stdout. If it is a tty, it switches to multiple filesnames per line, otherwise it puts 1 filename per line.

Since a pipe is not a tty, "ls|cat" will always be one filename per line. Ditto with using ls in a script in the manner shown above. If you really have an ls version with a third behavior, please tell us which version of unix you are using....after verifying that it is not an alias or something like that.
# 10  
Old 05-18-2006
I've verified all the all the claims.

1.It doesn't work with for loop

for i in "`ls -1`" considers the entire listing in one variable and goes thro' only one loop

2.changing IFS to newline doesn't work

3.And as per the last post even piping ls works.( Excellent finding Smilie )

I believe in the while loop read command makes all the difference. Correct me if I am wrong.

Cheers. Smilie
# 11  
Old 05-18-2006
Quote:
Originally Posted by dayanandra
I believe in the while loop read command makes all the difference. Correct me if I am wrong.
OK, you're wrong. It's a lot of stuff. At many places, the unix shell thinks in terms of words separated by spaces. Do:
echo `ls *`
You were beaten as soon as the ls finished. The backticks made the shell collect all of the output into one large line with a space between words. Then there is no way to tell the difference between:
"foot ball"
and
"foot" and "ball"
And what if a file has twenty spaces between two words in the filename? They get compressed to just one space.

The solution Jim posted avoids backticks. It also avoids using the "for" statement which loops on words. It is more what he avoided than what he used. His solution will work for embedded spaces and trailing spaces. But it will stumble on leading spaces which are very hard to support.
# 12  
Old 05-18-2006
just use *

for i in *
do
echo "$i"
done
# 13  
Old 05-19-2006
Quote:
What ls does depends on the version of ls. I am aware of two behaviors:

1. ls defaults to one filename per line. (the original, but now rare behavior.)

2. ls examines stdout. If it is a tty, it switches to multiple filesnames per line, otherwise it puts 1 filename per line.

Since a pipe is not a tty, "ls|cat" will always be one filename per line. Ditto with using ls in a script in the manner shown above. If you really have an ls version with a third behavior, please tell us which version of unix you are using....after verifying that it is not an alias or something like that.
Accepted Per.

But on doubt,
I verified the source code of ls from opensolaris.

I dont think the command is distinguishing itself with respect to tty or pipe ( or any other stream)
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