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 ...
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.
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
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"
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.