Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
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.
  1. #1
    Just 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:

    Code:
    ls -R /mp3s | wc -l
    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.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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...

  3. #3
    Just 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!

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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.

  5. #5
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    Code:
    ls -R /mp3s | grep '.mp3' | wc -l
    bash-2.05b$ ls -R music | grep '.ogg' | wc -l
    109

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Grrrr. You with your silly "bash scripting".

    :P.

  7. #7
    Linux Engineer
    Join Date
    Nov 2004
    Location
    home
    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

  8. #8
    Just Joined!
    Join Date
    Apr 2005
    Location
    Michigan
    Posts
    49
    Quote Originally Posted by Krendoshazin
    Code:
    ls -R /mp3s | grep '.mp3' | wc -l
    bash-2.05b$ ls -R music | grep '.ogg' | wc -l
    109
    Thanks =D

    Quote Originally Posted by Cabhan
    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.
    Could this be easily converted to an Irssi script?

  9. #9
    Linux Newbie
    Join Date
    Apr 2005
    Location
    Charlottesville, VA
    Posts
    175
    Here's a simple (snigger) command that does it....
    Code:
    ls -Ra1 /usr|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l
    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.
    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.

  10. #10
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    You guys!

    Code:
    find dir -name "*.mp3" -print | wc -l
    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?

Page 1 of 3 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •