Perl or Alternative: Count Files and Directories with one command (186 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 5 (186 Views)
Accepted Solution

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                    

 

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,779
Registered: ‎03-06-2006
Message 2 of 5 (170 Views)

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.

Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 3 of 5 (152 Views)

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

Please use plain text.
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 4 of 5 (149 Views)

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,779
Registered: ‎03-06-2006
Message 5 of 5 (144 Views)

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

[ Edited ]

>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.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation