Results 1 to 10 of 22
Ok, something similar to the title, except,:
What I'm trying to do is count all the mp3s I have and then output them into IRC to show off how big ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-03-2005 #1Just Joined!
- Join Date
- Apr 2005
- Location
- Michigan
- Posts
- 49
Counting all files in a directory (and subdirectories)
Ok, something similar to the title, except,:
What I'm trying to do is count all the mp3s I have and then output them into IRC to show off how big my mp3 stockade is. Anyways, the only thing I'm having trouble with is counting all the files in a directory (and subdorectoies), and excluding the directories themselves.
The command I'm using to count all the files/directories is:
Any ideas? Is there a parameter I can add to it to exclude folders? I've looked on a few places, and I've seen ways to count only files that start with A etc, but I'm not sure that this would work.Code:ls -R /mp3s | wc -l
- 05-03-2005 #2
Simply because I don't know much Bash, I would probably end up writing a Perl script for it.
As far as I am aware, there is no option for ls that would exclude directories.
Maybe I'll write a script for ya...
- 05-03-2005 #3Just Joined!
- Join Date
- Apr 2005
- Location
- Michigan
- Posts
- 49
Actually, I was trying to figure it out so I could write a script for Irssi to display it, but as I lack know how in both perl and bash, I'm trial and erroring it.
But if you wouldn't mind giving me some pointers that's be great!
- 05-03-2005 #4
Alrighty, well I just wrote a Perl script that works properly. It's probably horribly inefficient, but it works correctly, my testing shows.
Basically, the trick is regular expressions. I basically run a check over everything that "ls -1" returns (return one item per line) to see if it's a directory or a file. If a directory, I add it to a special array, if a file, I run a regular expression check on it:
/\w+\.mp3/
If it returns true, then I add one to my total songs.
Lemme know if you want my script. You basically just run it and it tells you how many mp3s are in that directory tree.
- 05-03-2005 #5Code:
ls -R /mp3s | grep '.mp3' | wc -l
bash-2.05b$ ls -R music | grep '.ogg' | wc -l
109Great GNU/Linux references and resources:
The Linux Documentation Project
Rute User's Tutorial and Exposition
GNU/Linux Man Pages
- 05-03-2005 #6
Grrrr. You with your silly "bash scripting".
:P.
- 05-03-2005 #7Linux Engineer
- Join Date
- Nov 2004
- Location
- Ft. Polk, LA
- Posts
- 796
I wrote here a shell script that should do what you want. It doesn't care what extention the file has, it will just recursively go through each directory and count any files within it, plus recurse into any more directories it finds. Give it as a parameter the directory you want to count.
Code:#!/bin/sh NUMBER=0 count () { for temp in $1/* ; do if [ -d "$temp" ] ; then count "$temp" elif [ -f "$temp" ] ; then NUMBER=$(($NUMBER+1)) fi done } count $1 echo "$NUMBER files were found"
Code:ray@jail:~$ fcount.sh music/ 209 files were found
- 05-03-2005 #8Just Joined!
- Join Date
- Apr 2005
- Location
- Michigan
- Posts
- 49
Thanks =D
Originally Posted by Krendoshazin
Could this be easily converted to an Irssi script?
Originally Posted by Cabhan
- 05-04-2005 #9Linux Newbie
- Join Date
- Apr 2005
- Location
- Charlottesville, VA
- Posts
- 175
Here's a simple (snigger) command that does it....
in case you're wondering, what it does is list the files including hidden ones (if you don't want hidden ones, make "-Ra1" into "-R1"), filters out any with a '/' in the name (which would make them directories), filters out blank lines, filters out "." and ".." listings, and counts them up.Code:ls -Ra1 /usr|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l
It's kinda slow, though, but I don't know how to make it faster. When I run it on /usr, it takes 1 minute 58 seconds on a 750MHz PIII processor, and it gives me a grand total of 236935 listings, which sounds about right.... Next, I'll have to try to run it on my whole system, just out of curiosity.---sxeraverx---
Linux without a C Compiler is like eating Spaghetti with your mouth sewn shut. It just doesn't make sense.
- 05-08-2005 #10Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
You guys!
This finds all mp3-suffixed filenames in the tree starting at "dir" and counts 'em. You know how to make a meal of things, don't you?Code:find dir -name "*.mp3" -print | wc -l



