LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-30-2004, 11:06 AM   #1
atlep
LQ Newbie
 
Registered: Aug 2004
Posts: 14

Rep: Reputation: 0
Shell scripting, getting directories :SOLVED:


Hi,

I'm trying to get a shell script to work on every file in a directory. But I have a problem with spaces in the filenames. Here's a code example to illustrate:
Code:
#!/bin/sh

for f in $(ls -1)
do
  echo :"$f"
done
And here's what happens:
Code:
$ ls -1
aa
te st
zz
$ ../dirtest
:aa
:te
:st
:zz
As you can see, the file entries get split by their space. Any ideas on how to conquer this problem?

- script-newbie

Last edited by atlep; 09-30-2004 at 11:35 AM.
 
Old 09-30-2004, 11:17 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Shell scripting, getting directories

Quote:
Originally posted by atlep

I'm trying to get a shell script to work on every file in a directory. But I have a problem with spaces in the filenames. Here's a code example to illustrate:
Code:
#!/bin/sh

for f in $(ls -1)
do
  echo :"$f"
done
The for-statement can list files by itself. So there's no need to run "ls -1". You won't have the problem with spaces as well:
Code:
for f in * ; do
    echo :"$f"
done

BTW, the space-in-file-name issue can come up on other occasions as well, but there's almost always a solution.
 
Old 09-30-2004, 11:21 AM   #3
atlep
LQ Newbie
 
Registered: Aug 2004
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you! That was magic
 
  


Reply



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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Bash Scripting Directories with Spaces gtwilliams Linux - Software 3 07-06-2005 03:16 PM
shell scripting Prasun1 Linux - General 1 06-23-2005 07:38 AM
BASH scripting browsing multiple directories PokerFace Programming 3 10-02-2002 12:50 PM

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

All times are GMT -5. The time now is 07:16 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
Open Source Consulting | Domain Registration