Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-28-2006, 08:00 AM   #1
Registered: Mar 2006
Distribution: FC4
Posts: 184

Rep: Reputation: 31
howto write a script to echo files with spaces in the name

I wrote a simple script to ls files in the directory
and echo them on the screen. The condition being

1 if $x is a directory: echo $x is a directory, or echo it is a file...

I have a couple of directorys in my home folder with spaces in their
names because of which I got the following error.

Please advise on how to correct this problem..


ls -1 | while read file
if [ -d $file ]
echo "$file is Directory"
echo "$file is a file"

./ line 5: [: Testing: binary operator expected
Testing Books is a file
thunderbird is Directory

Old 11-28-2006, 08:18 AM   #2
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Try changing the test to the following (note the quotes)

if [ -d "$file" ]


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 do I prevent echo eating spaces? essdeeay Linux - General 3 11-01-2005 04:11 PM
how to write a script to move files from one directory to another myeire Linux - Newbie 7 02-25-2005 11:01 AM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Networking 1 02-24-2005 04:49 AM
How can I write a shell script to transfer files and keep permissions? LancerNZ Linux - Wireless Networking 0 02-24-2005 01:19 AM
mp3 files - replacing blank spaces with _ script? mymojo Linux - Newbie 4 12-08-2003 04:33 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:06 PM.

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