Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

files count 1

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
0
0
PT
Hi,

I need to obtain the total count of number of files in directories and subdirectories under the current dir excluding 2 directories from the list.

can anyone help ?

TIA
 
I won't be surprised if someone comes up with something more elegant, but the following seems to work.

Since you say you want "the total count of number of files in directories and subdirectories under the current dir" I'm assuming you don't want to count files in the starting directory. If I'm wrong about that, get rid of if (level > 0) { and the associated close paren.
Code:
function file_count(dirname, count, exclude, level,
                    cmd, line, nf, temp, filename)
{
    cmd = "ls -l " dirname
    while (cmd | getline line > 0) {
        nf = split(line, temp)
        if (nf == 9) {
            if (match(line, "^d")) {
                filename = temp[nf]
                if (filename in exclude) {
                    continue
                }
                count = file_count(dirname "/" filename, count, exclude, level + 1)
            } else {
                # Don't count files in starting dir
                if (level > 0) {
                    count++
                }
            }
        }
    }
    close(cmd)
    #print "level:",level, "dirname:", dirname, "count:",count
    return count
}

BEGIN {
    # Add names of dirs to exclude, blank separated
    tc = split(". ..", temp)
    for (i=1; i<=tc; i++) {
        exclude[temp[i]]++
    }
    count = 0
    level = 0
    # Get starting dir
    dirname = ARGV[1]
    print file_count(dirname, count, exclude, level)
}
HTH


 
This is a little neater. Got rid of all the local variables in file_count except 1. The BEGIN block is as before.
Code:
function file_count(dirname, count, exclude, level,
                    cmd)
{
    # Returns count of files in all subdirs of dirname
    # Dirs in exclude are skipped
    cmd = "ls -l " dirname
    while (cmd | getline > 0) {
        if (NF == 9) {
            if (match($0, "^d")) {
                if (!($NF in exclude)) {
                    count = file_count(dirname "/" $NF,
                        count, exclude, level + 1)
                }
            } else {
                # Don't count files in starting dir
                if (level > 0) {
                    count++
                }
            }
        }
    }
    close(cmd)
    return count
}

 
Nice piece of excessive multipurpose awk mike :) I'd just add -A to ls
Code:
ls -ARp | awk '/^\.:$/,/^$/ { next } ! /\/$|^\.\/.*:$|^$/' | wc -l
Or..
Code:
shopt -s dotglob
find [-L] startdir/*/ -type f | wc -l
Code:
  find . -type f | grep /.\*/ -c
( find . -type f | awk -F/ '$3' | wc -l )
( find startdir/ -type f | grep /.\*/.\*/ )

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
xmb, the first thing I thought of was
Code:
find . -type f | wc -l
but this would count files in the starting directory, too, which OP doesn't want to do if I read his post correctly. Also wouldn't allow him to exclude those 2 directories he says he wants to exclude.

Does what you posted exclude the starting directory and allow for the exclusion of certain directories? If so, could you explain how? Thanks.

 
Hi mike,

Thnx for the code above.. I am trying to invoke it but somehow not getting there.

I added a line "{ print file_count($0) }" in the above code, saved is as test.awk file and executed it with the cmd >awk -f test.awk and inputted a dir name which gave me the result:

>awk -f files.awk
Repubs
awk: cannot be used as an array.
The input line number is 1.
The source line number is 12.

What am I doing wrong.. thnx again,
 
Also,

I came up with the follwing using Unix and awk commands:

find . -name "*" | awk '{if (int($NF)!=0) {a+=1}} END {print int(a)}' a=0 FS="/"

Which seems to be working but am unable to exclude the directories.

Any suggestions for altering this ?

thnx
 
I need to obtain the total count of number of files in directories and subdirectories under the current dir excluding 2 directories
find . -type f | grep -Evc '/excluded1/|/excluded2/'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top