Share your knowledge at the LQ Wiki.
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 07-10-2009, 05:19 PM   #1
LQ Newbie
Registered: Jul 2009
Posts: 5

Rep: Reputation: 0
Creating text file from ls output

Hello, i was trying to write a script that will list the contents of a directory in a file, but i need to add some prefix and suffix stuff to the listed files.

I want something like this:


so i thought about
echo -e "<url='$(ls -1 *.mp3)'/>"
but all i get is this:


Any ideas?
Old 07-10-2009, 05:34 PM   #2
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
You can loop through all files one by one. The basics below.
for f in $( ls -l *.mp3 ); do
        echo $f
Old 07-10-2009, 05:36 PM   #3
Senior Member
Registered: Oct 2003
Posts: 2,577

Rep: Reputation: 220Reputation: 220Reputation: 220

$ ls -l *.mp3 | xargs -n1 -i echo "<url='{}'/>"

Last edited by kilgoretrout; 07-10-2009 at 05:38 PM.
Old 07-10-2009, 05:50 PM   #4
LQ Newbie
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
for f in $( ls -1 *.mp3 ); do
echo -e "<url='$f'/>"

It works with files with no space in the name.
If there's a space it will jump to a new line dividing the file's name.
Is there a way to fix it, or better rename all files to replace the space with "_"?
Old 07-10-2009, 06:07 PM   #5
LQ Newbie
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
ok, solved!

for f in `find .`; do
file=$(echo $f | tr [:blank:] '_')
[ -e $f ] && [ ! -e $file ] && mv "$f" $file

for f in $( ls -1 *.mp3 ); do
echo -e "<url='$f'/>" >> playlist2.xml

Thank You all!!


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 parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 10:50 AM
Creating a postscript output from a image and text rdfoerster Linux - Software 3 08-31-2004 11:26 AM
creating file with default to? rohan208 Linux - General 3 05-10-2004 05:53 AM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 04:50 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:21 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