Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Perl or Alternative: Count Files and Directories with one command

SOLVED
Go to Solution
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                    

 

 

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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.

4 REPLIES
Acclaimed Contributor Acclaimed Contributor

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.

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

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

Highlighted
Acclaimed Contributor Acclaimed Contributor

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.