Results 1 to 5 of 5

Thread: Counting all files in a directory (and subdirectories)

  1. #1
    Join Date
    Jul 2010
    Posts
    52

    Counting all files in a directory (and subdirectories)

    What I am going to perform is to just count entire mp3s that I have and then after output them within the IRC to display off the procedure of my mp3 stockade is. By the way, the only thing that I am runnign with the trouble with is counting entire files within the directory (and subdorectoies), and excluding the directories themselves. The command that I am issuing to count the entire files/directories is:

    Code:
    ls -R /mp3s | wc -l
    Any suggestion would be appreciated ...

  2. #2
    Join Date
    Dec 2007
    Posts
    1,727

    Re: Counting all files in a directory (and subdirectories)

    Commonly , the concept is regular expressions. I commonly execute the check over entire thing that "ls -1" returns (return one option per line) to find out the result if it is a directory or a file. If it is specified as the directory, I associated this as a special array, if a file, I issued with a regular expression check on the same :

    /\w+\.mp3/

    If it returns true, then I have associated one to my total songs. Let me find out if you need my script. You commonly just issue the same and it evaluates the number of mp3s are in that directory tree.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Counting all files in a directory (and subdirectories)

    There are some command and with the appropriate option and all of that would be helpful for you in this case, as you wanted to redirect the output. Go through the command suggested below and consider out the result evaluated. See on the commands :

    Code:
     ls -R /mp3s | grep '.mp3' | wc -l
    Code:
    bash-2.05b$ ls -R music | grep '.ogg' | wc -l
    109

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Counting all files in a directory (and subdirectories)

    I have created a shell script which should perform what you actually you wanted to do . It does not care what the extention of the specified file contains , it will just recursively move via each one directory and count any files stored in the same, plus recurse into any more directories it searches. Specify the same as a parameter the directory you need to go for the counting .

    Code:
    #!/bin/sh
    NUM=0
    cnt ()
    {
    	for temp in $1/* ; do
    		if [ -d "$temp" ] ; then
    			cnt "$temp"
    		elif [ -f "$temp" ] ; then
    			NUM=$(($NUM+1))
    		fi
    	done
    }
    cnt $1
    echo "$NUM files were found"

  5. #5
    Join Date
    May 2008
    Posts
    2,680

    Re: Counting all files in a directory (and subdirectories)

    This is the simple one command that does the same .

    ls -Ra1 /usr|grep -v /|grep -vx ""|grep -vx "\.*"|wc -l

    In case, if you are just wondering , what it does, the list of the files composed with the hidden ones (if you need not to go to make it hidden , make "-Ra1" into "-R1"), filters out any with the slash '/' in the name (that would make them directories), filters out with the blank statments , filters out "." and ".." listings, and counts the entire things .

    I am not sure about the procedure to make it quick. When I launched the same on /usr, it consumes near about 1 minute 58 seconds on a 750MHz PIII processor, and it provides me a grand total of 236935 listings,that seems about right.... Next, I will have to go for the it on my complete system, just out of curiosity.

Similar Threads

  1. How to remove windows empty subdirectories
    By Dr.Jones in forum Tips & Tweaks
    Replies: 1
    Last Post: 02-07-2012, 01:30 PM
  2. Why my ISO files are not stored in the root directory ?
    By Dimensioner in forum Windows Software
    Replies: 3
    Last Post: 25-02-2011, 09:06 AM
  3. Listing the Files or Subdirectories in a Directory
    By Henryosa in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 06:39 PM
  4. How to use chmod to access all subdirectories
    By CitricAcid in forum Operating Systems
    Replies: 3
    Last Post: 29-06-2009, 11:14 PM
  5. Replies: 3
    Last Post: 29-06-2009, 06:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,023,906.87172 seconds with 17 queries