Operating System - HP-UX
1847108 Members
5283 Online
110263 Solutions
New Discussion

Re: Count total file command

 
SOLVED
Go to solution
Kenn Chen
Advisor

Count total file command

How could I count the total of files in my directory. There are many subdirectory in my directory. Thanks in advance.
Cyber Zen
5 REPLIES 5
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Count total file command

Hi,

To find the total number of files in your current directory and subdirectories, execute:

# find . -type f -print | wc -l

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Helen French
Honored Contributor

Re: Count total file command

Hi,

Try this:

# find dir_path -depth -type f -print | wc -l

HTH,
Shiju
Life is a promise, fulfill it!
Darrell Allen
Honored Contributor

Re: Count total file command

Hi,

To count files in your current directory only:

ll -a | grep ^- | wc -l

The ^ says to match lines beginning with the next character. You can change ^-
using the character from "man ls" shown below to count specific types of directory entries:

The first character indicates the entry type:

b Block special file
c Character special file
d Directory
l Symbolic link
n Network special file
p Fifo (also called a "named pipe") special file
s Socket
- Ordinary file


You can count multiple types using the format:

ll -a | egrep '^d|^-' | wc -l


To include sub-directories:

ll -aR | egrep '^-' | wc -l


Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Steven Gillard_2
Honored Contributor

Re: Count total file command

My personal favorite for counting the number of files in a single directory is:

$ ls -1 | wc -l

The -1 (thats the number one) causes ls to print one file name per line. Its a lot quicker than running ll because ls doesn't have to stat each file so its useful when you have a huge number of files in a directory.

Of course, if you want to include sub-directories as well the find command as already suggested is the way to go.

Regards,
Steve
Robin Wakefield
Honored Contributor

Re: Count total file command

Hi,

Just a word of clarification - if you are piping the output of "ls", then you don't need to use the -1 switch. If the command is not outputting to a login device, e.g. a pipe, then it *defaults* to one entry per line.

Rgds, Robin.