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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
11-23-2002, 10:56 PM
|
#1
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Rep:
|
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
|
|
|
11-23-2002, 11:10 PM
|
#2
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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.
|
|
|
11-23-2002, 11:19 PM
|
#3
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
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
|
|
|
11-23-2002, 11:29 PM
|
#4
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
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
|
|
|
11-23-2002, 11:30 PM
|
#5
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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
|
|
|
11-23-2002, 11:33 PM
|
#6
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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.
|
|
|
11-23-2002, 11:50 PM
|
#7
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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.
|
|
|
11-24-2002, 01:24 AM
|
#8
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
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
|
|
|
11-24-2002, 02:16 AM
|
#9
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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. . .
|
|
|
11-24-2002, 11:32 AM
|
#10
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
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
|
|
|
11-24-2002, 12:12 PM
|
#11
|
Senior Member
Registered: Jan 2002
Location: Rome, Italy ; Novi Sad, Srbija; Brisbane, Australia
Distribution: Ubuntu / ITOS2008
Posts: 1,207
Rep:
|
\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
|
|
|
11-24-2002, 12:16 PM
|
#12
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
Yeah, it does, thanks!
Cool
|
|
|
11-24-2002, 02:57 PM
|
#13
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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.
|
|
|
11-24-2002, 03:52 PM
|
#14
|
LQ Guru
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613
Original Poster
Rep:
|
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
|
|
|
11-24-2002, 04:04 PM
|
#15
|
Senior Member
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152
Rep:
|
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. . .
|
|
|
All times are GMT -5. The time now is 02:06 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|