LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to count the number of files recursively with wild card (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-count-the-number-of-files-recursively-with-wild-card-420298/)

redir 02-28-2006 09:21 AM

How to count the number of files recursively with wild card
 
Hello,
How can I count the number of files in a directory. I need to know the number of files in one of my web sites. I do not want to include junk like pdf's and jpeg's so I just want to count the php and html pages. In windows for example I would use dir *.php and get a list with a number at the bottom. I of course need to do this recursively. I have found some use of the wc command but is'nt that just for line counting? Can that be piped through something else? Thanks in advance.

redir

muha 02-28-2006 09:48 AM

use -o in find to specify multiple extensions,
nl or wc can be piped to count anything, like so:
Code:

find . -name '*.html' -o -name '*.php' |wc -l
5
find . -name '*.html' -o -name '*.php' |nl
    1  ./bla/test.html
    2  ./old/store.html
    3  ./old/store_archive.html
    4  ./index.html
    5  ./test.html

the dot after find tells find to start in the current dir.
In general, relative to the current dir: find ./some_dir
In general, absolute: find /some_dir

kilgoretrout 02-28-2006 10:22 AM

You can combine find and wc to get a count of php files:

$ find -iname '*.php' | wc -l

The "find" part(find -iname '*.php') will give a recursive search for all php files in the current directory and print them out one line per file found matching the search. Run the find command separately and you will see what I mean. That output is piped through to wc which counts the new lines and prints the total. The above will only output the number from wc which is, in effect, the file count found by the find command.

Hobbletoe 02-28-2006 10:30 AM

Here is what I came up with ...
 
Code:

#!/bin/bash

F_dirs()
{
 if [ `ls -l ${1}| egrep '^d' | wc -l` -eq 0 ]
 then
        F_count ${1}
 else
        for j in `ls -l ${1} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
        do
                F_dirs ${1}/${j}
        done
 fi
}

F_count()
{
 echo "${1} - php: `ls -l ${1}/*php 2> /dev/null | wc -l`"
 echo "${1} - html: `ls -l ${1}/*html 2> /dev/null | wc -l`"
}

echo -n "Enter the base directory:  "
read base_dir

for i in `ls -l ${base_dir} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
do
        F_dirs ${i}
done

exit 0

I assumed that you wanted to know the count/directory, and not just a lump sum. If I was wrong, then the find commands listed above are what you are looking for. Just put this all into a script, and make it executable

Code:

chmod +x filename
It asks for the base directory that you want to look at then does the rest. You can add different extentions to the F_count() if you want to count whatever you are looking for.

If anything is a bit confusing, just ask, and I can tell you why I did whatever.

redir 02-28-2006 10:43 AM

Excellent, thank you. Now let me take this one step further. How can I omit certain subdirectories from this process. There are a few directories that I would like to not have their files counted. Possible?

Thanks again.

Hobbletoe 02-28-2006 11:25 AM

Well, if you are using the find command, pipe it through egrep, and return just the ones that you want. I.e.

Code:

find . -name "*php" | egrep -v 'directory1|directory2|...' | wc -l
However, if you are using the script I provided (you didn't specify, so we'll cover all of the bases here), I modified the script to take the base directory as an argument if supplied (if not, it will prompt). In which case it would be

Code:

script_name.sh /directory_path | egrep -v 'directory1|directory2|...'
My new script is below ...

Code:

#!/bin/bash

# Standard Errors
E_SUCCESS=0        # Return if the script runs successfully
E_WRONGARGS=65        # Return if more than 1 argument is given

#Functions

F_dirs()
{
 if [ `ls -l ${1}| egrep '^d' | wc -l` -eq 0 ]
 then
        F_count ${1}
 else
        for j in `ls -l ${1} | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
        do
                F_dirs ${1}/${j}
        done
 fi
}

F_count()
{
 echo "${1} - php: `ls -l ${1}/*php 2> /dev/null | wc -l`"
 echo "${1} - html: `ls -l ${1}/*html 2> /dev/null | wc -l`"
}

# Main Code

case "$#" in
 0)
  echo -n "Enter the base directory:  "
  read base_dir;;
 1)
  base_dir=${1};;
 *)
  echo " Please use no arguments, or the base directory as the soul argument."
  exit $E_WRONGARGS;;
esac

for i in `ls -l | egrep '^d' | tr -s [:blank:] ' ' | cut -d' ' -f9`
do
        F_dirs ${i}
done

exit $E_SUCCESS



All times are GMT -5. The time now is 05:13 PM.