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 |
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) |
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 |
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:
Quote:
Thanks! Cool |
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 |
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. |
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. . . |
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 |
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. . . |
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 |
\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 |
Yeah, it does, thanks!
Cool |
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. |
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 |
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 11:34 PM. |