LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-08-2005, 10:27 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
How to use foreach on filenames with spaces?


say I have some file names with spaces, like so:

$ ls
New Text Document (2).txt New Text Document.txt


(there are two files there)

and I want to loop over them and do something to them in a foreach loop, like so:


$ foreach f ( `find . -name \* -print0` )
foreach? echo $f
foreach? end
./New
Text
Document.txt
./New
Text
Document
(2).txt


obviously, that's not the output I want, I need to get the actual file name as opposed to their bits so I can do something to the file. Is there a way to do this? I'm using tcsh

Thanks
 
Old 08-08-2005, 11:02 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
Have you considered:
Code:
find . -print0 | xargs -0 THECOMMANDTOEXEC
? It might not work for your specific case, but it works a lot of the time.
 
Old 08-09-2005, 04:24 AM   #3
javeree
Member
 
Registered: Jun 2004
Location: Belgium
Posts: 49

Rep: Reputation: 15
just before issuing the command set IFS='\t\n'
after the command re-set it to ' \t\n' (a space as first character)
this results in bash not recognizing spaces as word separators. It remains a problem if the command you execute relies on that.
 
Old 08-09-2005, 12:15 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
You can't set IFS to ' \t\n'. For some silly reason, it doesn't expand escaped characters in it, you must set them as literals. So do "IFS='<space><tab><newline>'"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
filenames with spaces antony.booth Programming 5 11-01-2005 04:49 AM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 08:28 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
filenames with spaces - explain t3___ Linux - Newbie 14 02-17-2004 06:13 PM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM


All times are GMT -5. The time now is 01:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration