LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-23-2002, 10:56 PM   #1
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
List the contents of a directory to a file


Hi! I want to list the contents of a directory to a text file. So far I've tried various things like:
ls /mnt/whatever/* /mnt/whatever.txt (I understand there is no need for the suffix, this is just for visual help )

And I've tried grep, piping, and cat, but I am thinking I've either missed something, or performed one of these combo's incorrectly.

Anyway, can someone tell me how to do this? What I want is to make a list of the contents of a directory containing my tunes, and have them numbered, so let's say it looks something like this:
ls /mnt/music

lalala.mp3
lalalalala.mp3
lahalaha.mp3

Then I want to have a text file that would read:
1. lalala.mp3
2. lalalalala.mp3
3.lahalaha.mp3

And so on.

Thanks for any ideas, and if it's something really dumb, thanks for making fun of me for not figuring it out

Cool
 
Old 11-23-2002, 11:10 PM   #2
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
ls *.mp3 | awk '{print NR". "$1}' > filelist

If you didn't want to number them, you would just:
ls *.mp3 > filelist

(the right waka redirects output)

Last edited by moses; 11-23-2002 at 11:12 PM.
 
Old 11-23-2002, 11:19 PM   #3
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Awesome, thank you kind sir

Oh, I have 1 more request...

Is there a way to "Find and Replace" the .mp3 part of the filenames in ls?

So using:
ls *.mp3 | awk '{print NR". "$1}' > filelist

Could I also use something like "cut" somewhere in there? So maybe:
ls *.mp3 | awk '{print NR". "$1}' > filelist | cut -c=mp3 filelist

Or would that cut out every mention of m p or 3 in there if it wasn't together?

Thanks again for the info

Cool
 
Old 11-23-2002, 11:29 PM   #4
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Reading over the awk man, I am udderly confused, but it would appear I could eliminate the .mp3 portion using that somehow. I will read it a bit more to see what I can find.

Oh, and btw, it's not returning full names with spaces for some reason, maybe that is because of the spaces??? Anyway, the text file is looking something like this:
Quote:
1. /mnt/lfs/home/mp3/temp/(02)\
2. /mnt/lfs/home/mp3/temp/(Guns\
3. /mnt/lfs/home/mp3/temp/04-eminem-cleanin_out_my_closet-rns.mp3*
4. /mnt/lfs/home/mp3/temp/2\
5. /mnt/lfs/home/mp3/temp/80's\
6. /mnt/lfs/home/mp3/temp/80's\
7. /mnt/lfs/home/mp3/temp/Adam\
8. /mnt/lfs/home/mp3/temp/Aerosmith\
So the numbering is going great, but now it's chopping my filenames up, so instead of looking like this:

Quote:
1. /mnt/lfs/home/mp3/temp/(02)\Some\ Cool\ Song.mp3
2. /mnt/lfs/home/mp3/temp/(Guns\Some\ Cool\ Song.mp3
3. /mnt/lfs/home/mp3/temp/04-eminem-cleanin_out_my_closet-rns.mp3*
4. /mnt/lfs/home/mp3/temp/2\Some\ Cool\ Song.mp3
5. /mnt/lfs/home/mp3/temp/80's\Some\ Cool\Song.mp3
6. /mnt/lfs/home/mp3/temp/80's\Some\ Cool\ Song.mp3
7. /mnt/lfs/home/mp3/temp/Adam\Some\ Cool\ SOng.mp3
8. /mnt/lfs/home/mp3/temp/Aerosmith\Some\Cool\Song.mp3
It looks like the above example. Any ideas on fixing that, well short of renaming all my files to have _ rather than spaces?

Thanks!

Cool
 
Old 11-23-2002, 11:30 PM   #5
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Well, you could use sed, though I'm as familiar with it.

I'm also not entirely sure what you want to accomplish. If you just want
to get rid of the .mp3, you could use (assuming there are no filenames
with more than one "." ):
cut -d"." -f1

e.g.
ls *.mp3 | cut -d"." -f1 | awk . . . > filename

Always redirect at the end of your pipe, otherwise you can't be sure
what's coming down the pipeline once you have redirected (probably,
usually nothing). If you want to run another command after the redirect,
and it doesn't require the pipeline, you can use
command1 && command2
or
command1 ; command2

&& => run the second command only if the first exits w/o errors.
; => run the second command regardless of how the first exits.

cut -c=mp3
will just give you an error message.

info cut
 
Old 11-23-2002, 11:33 PM   #6
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Sorry, got my response in after your next message. . .

Ok, the awk is going to use a field separator of " " by default. You'll
need to change that to something that is more appropriate to your situation.

I'll check back in a bit, gotta change a baby's diaper.
 
Old 11-23-2002, 11:50 PM   #7
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
If you change your field separator to something like "\n", it should work:


ls *.mp3 | awk -FS"\n" '{print NR". "$1}'
WRONG

If you want to get rid of the .mp3 portion of the filename, you can do
the following (again, assuming the filenames don't have more than
one "." in them, otherwise, you'll have to be more creative):

ls *.mp3 | awk -F. '{print NR". "$1}'
You don't need "cut".

This is with gawk, which is what awk is a symlink to on the default
Slackware 8.x distro. . . I think awk may need the -FS rather than the
-F to separate the fields. . .

Last edited by moses; 11-23-2002 at 11:59 PM.
 
Old 11-24-2002, 01:24 AM   #8
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Wow, thank you very much, this sounds like it will work nicely for my needs. Very helpful of you to show me this, and I will read up on awk to see what I can learn.

Thanks again for this, I just checked it, working perfectly with:
dir /mnt/music | awk -FS"\n" -F. '{print NR". "$1}' > /mnt/music/musiclist

Cool
 
Old 11-24-2002, 02:16 AM   #9
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
Not sure if it's a typo or not, but you don't need to have
-FS"\n" and -F. in the same line. The -F. should be sufficient. In fact,
if you are doing this on Slack (as I assume), you are probably running
8.x, which means you are probably using gawk, which means -FS"\n" will
set the field separator to S"\n" (of course, you then immedietly change it
to . with the -F. option. . .

Sorry about the unclear posts above. . .
 
Old 11-24-2002, 11:32 AM   #10
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Yes, 8.1 (Good guess )

Thanks for the clarification.

Just out of curiosity what does \n mean, and where could I find a place to explain things like that for me a bit better?

Thanks
 
Old 11-24-2002, 12:12 PM   #11
NSKL
Senior Member
 
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207

Rep: Reputation: 47
\n is a escape character, that adds a newline. I learned about these in a C programming book, since C uses same escape characters, here are some:
\n - newline
\a - comp. bell
\t - Tab
\\ - Backslash
\" - Quotatient mark
Well hope that helps,
-NSKL
 
Old 11-24-2002, 12:16 PM   #12
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Yeah, it does, thanks!

Cool
 
Old 11-24-2002, 02:57 PM   #13
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
The backslash "\" is usually used as an escape mechanism, that is,
whatever comes after it is not interpreted by the shell, but is passed on
to the command, so if you wanted to copy a file with spaces in it, you
could do:

cp file\ with\ spaces.mp3 file_without_spaces.mp3

Now, that by itself doesn't mean \n is a special character, it just tells the
shell not to interpret the "n" (pass it on to the command, rather than
using it as a command), so the awk command get an "n", which isn't
terribly useful. So, the next step is to escape the entire sequence, \n,
which is done with another escaping mechanism (different, but similar),
double quotes (""). So, when we want a newline character, we need to
escape twice, thus the sequence; "\n".
You could use the double quotes to escape the spaces from your file
as:
cp "file with spaces.mp3" file_without_spaces.mp3
This may be a little simplified, but it gets the idea across, I think.
 
Old 11-24-2002, 03:52 PM   #14
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Original Poster
Rep: Reputation: 69
Is the " " pretty much usable across the board, or do only certain apps recognize it? For example, let's say I wanted to mpg123 file\ with\ spaces.mp3, could I:
mpg123 "file with spaces.mp3"?

Or is it only with certain apps, maybe just developer/development apps (such as awk), that it recognizes the " " to give the escape?

Cool
 
Old 11-24-2002, 04:04 PM   #15
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 50
It's completely application independent, the shell will see the escape,
and ignore any special characters (such as a space) as far as shell
processing goes, and just pass those characters, as they are, on to
whatever is supposed to get them. Whatever is supposed to get the
special (w.r.t the shell) characters just gets them and deals with them
however it needs to. So, when you escape out the spaces in your
filename, you could use vi, mpg123, xv, whatever, they'll get the
spaces, and understand that this is one complete filename. If you
didn't escape the spaces, the shell would send three filenames to vi,
mpg123, xv, etc., and you would probably get errors from the application.

mpg123 file\ with\ spaces (1 argument to mpg123)
is the same as
mpg123 "file with spaces" (1 argument to mpg123)
is VERY different from
mpg123 file with spaces (3 arguments to mpg123)

Escape character are not necessarily shell independent, but I can't think
of a shell that uses something different from the de facto standards. . .
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to list contents of a .tar.gz without unzip? leontini Linux - General 14 04-21-2010 03:07 AM
cannot list directory contents kpachopoulos Linux - General 2 11-29-2005 06:24 PM
Samba - XP can't list folder contents p0tw0r Linux - Networking 1 05-31-2005 04:37 AM
won't list contents of cdrom gillmb Linux - Newbie 2 10-19-2004 03:09 PM
How to list total file size of a directory phil1076 Linux - General 3 12-18-2003 04:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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