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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KSH - Count Number of Entries Under Directories

Status
Not open for further replies.
Aug 23, 2002
27
US

Been thinking about this for a while. I have a few directories that I need to take a file count under each one.

I am trying to figure out how to have a script automatically change to each directory and count the files in that directory.

So far I'm limited to hard coding each directory into the script.

Any help would be appriciated.

Thanks

-Astrotrain
 
So far I'm limited to hard coding each directory into the script
Which script ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Make a list of your directory path's in a text file and then:
Code:
for dir in `cat DirFile.txt`
do
  echo "$dir has `ls -1|wc -l` files."
done
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

PHV - The script I am refereing to is a homegrown ksh script that I am trying to get to work.

Thanks


-Astrotrain
 
As you didn't post any code it's hard for us to help you getting it to work !

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Or if you want all of the directories under a certain point, the following should work...
Code:
#!/bin/ksh

TOP=$(pwd)

find ${TOP} -type d -print | while read DIR
do
    cd ${DIR}

    COUNT=$(ls -l | grep -v ^d | wc -l)
    (( COUNT -= 1 ))

    print "Directory ${DIR} has ${COUNT} files."
done
Directories aren't included in the count of files. No asure if that's what you want or not.
 
Or, if you want to be able to give it the starting directory as an optional parameter, change the "[tt]TOP=$(pwd)[/tt]" line to...
Code:
TOP=${1:-$(pwd)}
If you give it a directory, it will use it. Otherwise it will use your current directory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top