LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2005, 01:25 PM   #1
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Shell Scripting and Files With Spaces


A little tip:

If you're writing a shell script that's going to use filenames that have spaces in them, and you need to iterate through them, you may find that bash splits the files in the wrong place. For example, if I have a file called "A file with spaces", bash may try to treat that as 4 files.

So, here's the trick... use the -Q option to ls. It automatically quotes your filenames for you.
Code:
#!/bin/bash
for file in `ls -Q`
    do echo $file
done
 
Old 08-12-2005, 09:49 PM   #2
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Blog Entries: 9

Rep: Reputation: 48
i believe you can also write it as
Quote:
A\ file\ with\ spaces
treating the ' ' as a special character in the filename, the same way you would with $,etc
 
Old 08-13-2005, 02:16 AM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
You can, but that won't solve the problem with ls.
 
Old 08-13-2005, 09:14 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Original Poster
Rep: Reputation: 128Reputation: 128
Yeah, this wasn't supposed to be "everything", but it was supposed to be an easy way when dealing with ls.
 
Old 08-13-2005, 12:50 PM   #5
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Blog Entries: 9

Rep: Reputation: 48
oops, my bad. Sorry to be off topic
 
Old 08-13-2005, 02:01 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Original Poster
Rep: Reputation: 128Reputation: 128
microsoft/linux: no, it's cool, it was very much related, but it just addresses two separate needs. I just discovered the -Q and --quote-style options to ls and thought it was worth sharing, with all the troubles I've seen on here with filename splitting.
 
Old 08-13-2005, 03:10 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
A neat trick indeed, but if it's GNU-specific your script won't be portable to UNIX systems. Better would be
Code:
#!/bin/sh
ls | while read name
do
    echo "$name"
done

Last edited by eddiebaby1023; 08-13-2005 at 03:15 PM.
 
Old 08-13-2005, 03:26 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Original Poster
Rep: Reputation: 128Reputation: 128
Running your script gives:
Code:
 $ touch "This is a test file"
 $ touch "This is also a test file"
 $ ./script.sh
This is a test file
This is also a test file
script.sh
Running it with it modified to put backslashes in front of the quotes gives:
Code:
 $ ./script.sh
"This is a test file"
"This is also a test file"
"script.sh"
Which is, surprisingly, intended behavior. My interpretation of the read man page was incorrect.
 
Old 08-14-2005, 08:07 AM   #9
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
I put quotes around the echo()ed argument to preserve the whitespace in the filenames. Try the two variations with more than a single space (or tabs) between the words in the filename and see the difference.
 
Old 08-15-2005, 05:04 PM   #10
julian_s
Member
 
Registered: Jan 2004
Location: Colombia
Distribution: Ubuntu 5.10
Posts: 35

Rep: Reputation: 15
I find this topic doing my first ever script, and I still haven't found how to solve it, here is what I did, based on what you said:

Code:
for file in `ls -Q $1 | grep .m3u`
	do
	
	echo "$1""$file"
		
	done
And this is the output:

Quote:
/home/julian/"how
/home/julian/to
/home/julian/dismantel
/home/julian/an
/home/julian/atomic
/home/julian/bomb.m3u"
/home/julian/"Tyrannosaurus
/home/julian/hives.m3u"
And the correct ouput will be
Quote:
/home/julian/"how to dismantel an atomic bomb.m3u"
/home/julian/"Tyrannosaurus hives.m3u
So what do you suggest??

Last edited by julian_s; 08-15-2005 at 05:15 PM.
 
Old 08-15-2005, 06:21 PM   #11
julian_s
Member
 
Registered: Jan 2004
Location: Colombia
Distribution: Ubuntu 5.10
Posts: 35

Rep: Reputation: 15
Never mind, I found another way:


Code:
#!/bin/bash

OLDIFS="$IFS"
IFS='
'
for fic in $(ls); do
        echo $fic
done
IFS="$OLDIFS"
Sweeeeeeet!!!!!!!!!!!!!!!
 
Old 08-16-2005, 06:16 AM   #12
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
I think that

Code:
for i in *.m3u; do ...
also works. Does't it?
 
Old 08-17-2005, 01:43 AM   #13
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Original Poster
Rep: Reputation: 128Reputation: 128
enemorales: try that with files containing spaces in the names?
 
  


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
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 07:30 AM
Shell scripting problem for importing Firefox files montylee Programming 6 06-03-2005 01:52 AM
shell scripting: checking for modified files topcat Linux - General 4 12-08-2004 04:43 AM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM
Remove white spaces in HTML files using shell : Possible???????? cpanelskindepot Programming 2 08-05-2004 11:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:18 AM.

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
Open Source Consulting | Domain Registration