Operating System - HP-UX
1832595 Members
2998 Online
110043 Solutions
New Discussion

Re: Count total files and Subdir with find & ll –lR is different?

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Count total files and Subdir with find & ll –lR is different?

When I tried to count total number of files and subdirectories (recursive), I got different results? Which on is correct?

/log_output#find . | wc -l
49541

/log_output#ll -lR|wc -l
83216


Thanks,

Gulam.
Everyday Learning.
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: Count total files and Subdir with find & ll –lR is different?

Hi Gulam:

An quick examination of the output will show you the difference --- the 'find' is accurate; the 'll' is *not*.

# cd /tmp; ll -lR | more

.
.
.
./sp6287:
total 4
-rw-r----- 1 root sys 44 Mar 23 09:45 SOURCE_ME
-rw-r----- 1 root sys 86 Mar 23 09:45 gentmp

NOTE the total block count for the subdirectory. There are blank lines, too to separate subdirectories from one another, inflating the lines counted.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Count total files and Subdir with find & ll –lR is different?

For an accurate count with ll you need to exclude some lines i.e.

# ll -lR . | egrep -v '^$|^total|^\.' | wc -l

The above should give you the same count as the find below:

# find . | wc -l

cheers!
Bill Hassell
Honored Contributor

Re: Count total files and Subdir with find & ll –lR is different?

The ll command is producing several extra lines per directory. One is a blank line separating each new directory, the name of the directory which was already listed previously, then a total line. So for every directory, you'll have 4 extra lines using ll.

It's better to count directories and files separately:

echo "Directories:"
find /someplace -type d | wc -l
echo "Files:
find /someplace -type f | wc -l


Bill Hassell, sysadmin