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 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 Bab00shka Shell Programming and Scripting 8 09-30-2008 03:13 PM
spaces in filenames, for do naviztirf Shell Programming and Scripting 4 10-17-2007 06:08 PM
Unix filenames and spaces x96riley3 Shell Programming and Scripting 2 01-31-2007 07:07 PM
spaces in filenames Hitori Shell Programming and Scripting 4 07-04-2006 05: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 Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-17-2006
rayne rayne is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 15
how to handle spaces in filenames

I'm trying to do something like that:
for $filename in `ls -1`
do
some_command $filename
done

but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around?
  #2 (permalink)  
Old 11-17-2006
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
file name should be in double quotes

Try this -

for $filename in `ls -1`
do
some_command "$filename"
done
  #3 (permalink)  
Old 11-17-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Try this :

Code:
ls | \
while IFS= read filename
do
   some_command "$filename"
done


Jean-Pierre.
  #4 (permalink)  
Old 11-19-2006
rayne rayne is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 15
Quote:
Originally Posted by aju_kup
file name should be in double quotes

Try this -

for $filename in `ls -1`
do
some_command "$filename"
done

No, it doesn't work.
aigles, I'm actually looking for a way to do this in a for loop, if possible. Thanks anyway.
  #5 (permalink)  
Old 11-19-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
Quote:
I'm trying to do something like that:
for $filename in `ls -1`
do
some_command $filename
done

but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around?
Removes the $ from the variable name in the for statement:

Code:
for filename in `ls -1`
do
   some_command $filename
done

Replaces the ls command by the * pattern:

Code:
for filename in *
do
   some_command $filename
done


Jean-Pierre.
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 07:30 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