LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-11-2005, 08:40 AM   #1
hesher
LQ Newbie
 
Registered: Jun 2003
Posts: 17

Rep: Reputation: 0
file name spaces problem


Hi,
im trying to list the contents of directory that has files with space in their filename.
my directory: (with ls -b)

01\ -\ Tear\ Me\ Down.mp3 08\ -\ Exquisite\ Corpse.mp3
02\ -\ The\ Origin\ of\ Love.mp3 09\ -\ Midnight\ Radio.mp3
03\ -\ Angry\ inch.mp3 10\ -\ Nailed.mp3
....

the command i am running

#!/bin/tcsh -f
set pw=`pwd`
foreach fn ( `ls | add_quotes`)
printf "%s/%s\n",$pw,$fn
end

the output i am getting:

,/mnt/wind/Media/Music/Soundtracks/Hedwigangry/inch,"01
,/mnt/wind/Media/Music/Soundtracks/Hedwigand/the
,/mnt/wind/Media/Music/Soundtracks/Hedwigangry/inch,-
,/mnt/wind/Media/Music/Soundtracks/Hedwigand/the
,/mnt/wind/Media/Music/Soundtracks/Hedwigangry/inch,Tear
.
.
.

As you can see, it is splitting the filename in the space although i do use the "\" (ls with -b)

Anyone???!?!?!?

 
Old 02-11-2005, 10:34 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 60
How about something like this...
Code:
#!/bin/bash
cd /home/images
for i in * ; do
   echo $i
done
 
Old 03-06-2005, 12:12 PM   #3
alruin
LQ Newbie
 
Registered: Mar 2005
Location: Maastricht, NL
Posts: 1

Rep: Reputation: 0
Re: file name spaces problem

Hi hesher,

Came across your post while looking for clues on how to solve a similar problem.
Didn't find any good clues, so I thought "let's RTFM...".
And behold...:

Here's how to do it in tcsh:
Code:
#!/bin/tcsh -f
set pw="`pwd`"
foreach fn ( "`ls -1`" )
  printf "%s/%s\n" "$pw" "$fn"
end
Explanation (from the "Command substitution" section of the tcsh man page):

Normally, tcsh breaks the output of command substitution ( i.e. `...` ) into 'words'
(components of variables) at spaces and newlines. The breaks at spaces are
suppressed if you enclose the command substitution in double quotes.

So even though it looks weird, and you'd expect the loop to execute only once, it
does works grace to the newlines produced by "ls -1" (BTW, that's the DIGIT 1;
not the letter l...). And you need to quote $pw and $fn too, or you may still
get rubbish.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 06:15 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 12:43 PM
Spaces in file names JohnKFT Slackware 3 11-09-2004 04:44 PM
Spaces reading a file twantrd Programming 4 09-28-2004 10:00 PM
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 11:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration