The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
spaces in filenames, for do naviztirf Shell Programming and Scripting 4 10-17-2007 05:08 PM
Unix filenames and spaces x96riley3 Shell Programming and Scripting 2 01-31-2007 07:07 PM
how to handle spaces in filenames rayne Shell Programming and Scripting 4 11-19-2006 01:37 PM
spaces in filenames Hitori Shell Programming and Scripting 4 07-04-2006 04:35 PM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-05-2002
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
spaces in filenames

Hi

I hope someone will be able to resolve this little teaser!

I am running a script

for file in `ls directory`
do
echo "$file"
...other code here....
done

this works fine unless we receive a file with a name which has a space in it

ie

"filena me"

(I know its not good to have a space in a filename but we have no control over the filenames which are ftp'd to us.)

If the file "filena me" is in the directory, the above script will do the following:

filena
...do other stuff....

me
...do other stuff....

whereas I would like it to do

filena me
....do other stuff....


Can anyone help me please?

Thanks
Helen
  #2 (permalink)  
Old 07-05-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Replace
for file in `ls directory`
with
cd directory
for file in *
  #3 (permalink)  
Old 07-05-2002
Bab00shka Bab00shka is offline
Registered User
  
 

Join Date: Apr 2002
Location: Chesterfield, UK
Posts: 123
Thumbs up

Thanks Perderabo, This works fine!
  #4 (permalink)  
Old 07-06-2002
taher_n taher_n is offline
Registered User
  
 

Join Date: Jun 2002
Posts: 9
1 more solution

u can also use

for fine in `ls directory | tr -s " "`
{
loop
}
  #5 (permalink)  
Old 09-29-2008
ben_40 ben_40 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 2
filenames starting with . and having spaces in filename

I am trying to run the following

for fname in *
do
echo $fname
done

to list all the files in a specific directory. This does not seem to include the files starting with . character (e.g .classpath)

then i modified the program to include ls -A

for fname in $(ls -A)
do
echo $fname
done

This is including the . files. But it splits the file containing spaces into multiple entries (Hello name.txt => Hello, name.txt)

Just wondering is there a way to modify the * option to include the hidden files.

Any help is very much appreciated.

Thanks
ben
  #6 (permalink)  
Old 09-29-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Try:
for name in * .* ; do

Get the spacing right! First asterisk is surrounded by spaces, 2nd is not.
  #7 (permalink)  
Old 09-29-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 242
Think the other way round...

Hi Ben,
I think "ls -A" is the best way to go, using "ls * .*" will also list contents of directories, even above, and I guess You don't want that. Well, just a guess anyway.

One of the beauties of ls (and many other utilities) is that when piped to another program it will regard each file/entry as a line, as opposed to when written to screen where it will all appear continuously, also when produced as a string for the "for fname in " construct.

So if You use "while" and try something like this instead,

Code:
ls -A | while read fname ; do echo $fname ; done
maybe You will be a bit closer to a solution? It of course depends on what You want to do with the names, are the entries interesting only if they are files, then use something like
Code:
ls -A|while read line;do [ -f "$line" ]  && echo $line;done
/Lakris
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0