Operating System - HP-UX
1751840 Members
5433 Online
108782 Solutions
New Discussion

Re: Perl or Alternative: Count Files and Directories with one command

 
SOLVED
Go to solution
support_billa
Valued Contributor

Perl or Alternative: Count Files and Directories with one command

hello,

 

i want to count files and directories with one command.

 

my alternative  due 2 find commands :

find <dir> -type f  | wc -l

find <dir> -type d | wc -l

 

i adapted the last perl one liner of :

 

Perl script sum of all files

 

dir="/tmp" perl -MFile::Find -le '$dir=$ENV{"dir"};$cnt_d=0;$cnt_f=0;find(sub{-d and ++$cnt_d;-f and ++$cnt_f;},$dir);END{print "$cnt_f:$cnt_d"}'

 

questions

 

- is the a better way to pass the directory to perl one liner ?

- better handling to count files and directories ?

 

why i want to count with one command : we have huge filesystems where find runs a long time

 

regards                    

 

 

4 REPLIES 4
Dennis Handly
Acclaimed Contributor
Solution

Re: Perl or Alternative: Count Files and Directories with one command

You could do something like this to give you a list of files with a "F" or "D":

find  path -type d -exec echo D {} \; -o -type f -exec echo F {} \;

But this invokes echo for each file.

 

You could do this: find  path -exec ll -dog {} + | magic-script

This will invoke ll for huge number of files.  All you magic script needs to do is look for the leading "d" and count up the two types of "files".

 

Otherwise using perl to both find and count seems fine.

support_billa
Valued Contributor

Re: Perl or Alternative: Count Files and Directories with one command

hello,

 

You could do something like this to give you a list of files with a "F" or "D":

find  path -type d -exec echo D {} \; -o -type f -exec echo F {} \;

 

the output is handle by example a "awk" like this ?

find  . -type d -exec echo D {} \; -o -type f -exec echo F {} \; | awk '
BEGIN { cnt_d = 0;  cnt_f = 0; }
/^D/  { cnt_d++ }
/^F/  { cnt_f++ }
END   { print cnt_f ":" cnt_d }
'

 

regards

support_billa
Valued Contributor

Re: Perl or Alternative: Count Files and Directories with one command

 

Information about performance ( 60000 files and 350 directories )

 

1.  : perl

2. : find  ( with 2 seperate commands)

3.  find with echo ( very,very slow )

 

regards

Dennis Handly
Acclaimed Contributor

Re: Perl or Alternative: Count Files and Directories with one command

>the output is handle by example a "awk" like this ?

 

Exactly.

 

>3.  find with echo (very, very slow)

 

Yes, that's why I suggested the "-exec ll -dog {} +" solution.  With a slightly modified awk script.