Linux - GeneralThis 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.
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.
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146
Rep:
Number of files in directory
Here is what I have so fare. It works but the only problem is that the count is not correct, it is also counting the directories in the total. I'm trying to get a total count of my MP3 files from My Music and all its subdirectories. Here is the directory structure.
My Music
| |_> Artist
| |_> Album
| |_> songname1.mp3
| |_> songname2.mp3
|__> Artist 2
|_> Album2
|_> songname1.mp3
Code:
ls -1R | wc -l
(note the "-1" option to 'ls' is hyphen-one, NOT lower-case L, which is
what the option to 'wc' is).
All of my music is in MP3 format so a *.mp3 would work I just don't know how to ad it into the command.
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.
Edit: Just adding, grep is a very, very useful tool, you might want to read the man page and learn to use it (this is presuming you don't know how to). Grep, awk, sed etc. are very powerful allies when you're working in the CLI!
Check out http://tldp.org for the beginner's guide to bash - amongst teaching you bash basics it will also teach you regexps, grep, awk and sed.
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146
Original Poster
Rep:
Quote:
Originally posted by Valindar Use a regular expression with grep:
Code:
ls -1R | grep .*.mp3 | wc -l
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.
Edit: Just adding, grep is a very, very useful tool, you might want to read the man page and learn to use it (this is presuming you don't know how to). Grep, awk, sed etc. are very powerful allies when you're working in the CLI!
Check out http://tldp.org for the beginner's guide to bash - amongst teaching you bash basics it will also teach you regexps, grep, awk and sed.
Thanks worked like a charm! I'm still working on learning all of the | in and outs. How are you a Newbie !
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146
Original Poster
Rep:
Quote:
Originally posted by Valindar I'm not a newbie - I just haven't posted much
For whoever reads this post and it helps them, I noticed that I had a few mp3's that weren't lowercase hints "MP3" "mp3" "Mp3". Any way I added another argument to grep to also grab those. Here it is.
I want to get the count of *.html files in a directory. And then use that count to iterate through all the .html files to perform a sed operation.
Thanks!
Actually, counting the files, and operating on them, should probably be performed separately -- it's simpler and easier to understand.
You can create a list of .html files and move through the list, performing any operations you want. Also, listing the files located in a particular directory, and listing the files located in an entire directory tree, are different operations.
Here's an example that lists the .html files in the current directory and operates on them:
Code:
ls -1 *.html | while read line
do
echo "do something to \"$line\" here."
done
This method is written to allow spaces in the file names. Note: the "-1" argument to the "ls" call is the number one, not a lowercase "L".
Now here's an example that lists all the .html files in a directory tree, but is otherwise the same as the first example:
Code:
path="/netbackup/data/Network/arachnoid"
find $path | grep "\.html$" | while read line
do
echo "do something to \"$line\" here."
done
The path in this example happens to be the location of my local Website archive, just change it to suit your own needs. Unlike the first example, this one doesn't need to be run in the target directory, in fact it can be run from any location.
One more thing. Because of how "find" operates, the generated list will not be in any predictable order. If you need the names to be in alphabetical order, add a "sort" command to the stream:
Code:
path="/netbackup/data/Network/arachnoid"
find $path | grep "\.html$" | sort | while read line
do
echo "do something to \"$line\" here."
done
Huh, all this discussion only about a simple thing, ow!
But i know that this is a simple thing, but when you want to get better of a command it gets more and more difficult to use!
Actually, counting the files, and operating on them, should probably be performed separately -- it's simpler and easier to understand.
You can create a list of .html files and move through the list, performing any operations you want. Also, listing the files located in a particular directory, and listing the files located in an entire directory tree, are different operations.
Here's an example that lists the .html files in the current directory and operates on them:
Code:
ls -1 *.html | while read line
do
echo "do something to \"$line\" here."
done
This method is written to allow spaces in the file names. Note: the "-1" argument to the "ls" call is the number one, not a lowercase "L".
Now here's an example that lists all the .html files in a directory tree, but is otherwise the same as the first example:
Code:
path="/netbackup/data/Network/arachnoid"
find $path | grep "\.html$" | while read line
do
echo "do something to \"$line\" here."
done
The path in this example happens to be the location of my local Website archive, just change it to suit your own needs. Unlike the first example, this one doesn't need to be run in the target directory, in fact it can be run from any location.
One more thing. Because of how "find" operates, the generated list will not be in any predictable order. If you need the names to be in alphabetical order, add a "sort" command to the stream:
Code:
path="/netbackup/data/Network/arachnoid"
find $path | grep "\.html$" | sort | while read line
do
echo "do something to \"$line\" here."
done
HTH
The way you explained was so much easier then what I was trying to do, thank you very much!
I just wanted to point out that the solution (#2) does not quite do what the author says, and will sometimes miscount
Quote:
Use a regular expression with grep:
Code:
Code:
ls -1R | grep .*.mp3 | wc -l
list all recursively one per line, piped through grep which will output only: any amount of characters, then a dot character then 'mp3' then piped through wc -l to count it.
Namely the dot is a wildcard in grep regular expressions (and * is the repetition) so this will count anything containing "mp3", rather than ".mp3". Thus if you store your mp3 files in a subdirectory of your music folder labelled mp3 (like I do), then you will miscount. To list only those with ".mp3" you need to use the escape character before the dot. For example
Code:
ls -1Rl | grep '\.mp3' | wc -l
will count the files with ".mp3" somewhere (the ".*" is unneeded as grep looks at substrings anyway). Even better we could look for ".mp3" at the end of the line (via $) and ignore case (so .MP3 is counted too)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.