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!

How do I count files in a directory 1

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
0
0
CH
I need a script to count files in a directory and its subdirectories

The results should be similar to ;
/ = 44
/bin = 100
/var = 20
/var/tmp = 2

Note: Not file sizes, but number of files
 
hi,

you can use

find /bin ¦ wc -l

or in a script do

Count()
{
total=`find $1 ¦ wc -l`
echo "$1 = $total"
}

Count /
Count /bin
Count /var
Count /var/tmp

Take care to this

in / you count all files on disks (and remote if mounted)
and in
/var you count also the file contained in /var/tmp

Sometime may be useful, I don't know if this is your case,
count file inside the filesystem without "traverse" it
also if the mount point of the 2nd fs is under the 1st

ex you have fs

/
/var
/home
/home/users
/usr
/usr/local
...

using

find /usr -xdev
find /home -xdev

you don't see files under /usr/local and /home/users filesystem

bye
 
Sorry but I need to extract the directory and subdir names with their counts.
 
hi,

the way may be always the find command using the flag

-type

find /var -type d for directories
find /var -type f for directories

but you have to think to the script structure

bye
 
for dir in `find $PWD -type d -depth -print`
do
echo $dir = `ls -la $dir|grep ^-|wc -l`
done
 
Gee that is kind of complicated for:

find /your_dir_search -type f | wc -l
 
Forgot to add for multiple dirs:

for dir in / /var /bin
do
find $dir -type f | wc -l
done

Still much simpler.
 
Hmm...I find that "find" is sometimes slow on larger filesystems.

You can also try:

du -a [starting point] | wc -l

This will list ALL files & directories (udnerneath) the starting point but there is no distinction between a directory and a file. For that you gotta use the find command.
 
HI,

After running the below code on a 200.000 files FS,I get a ksh "no space" error.

===================
for dir in `find $PWD -type d -depth -print`
do
echo $dir = `ls -la $dir|grep ^-|wc -l`
done
===================

Anything I can do about it? xargs ?

Long live king Moshiach !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top