Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count files in a directory

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
Is there a command to count files in a specific directory?
 
I've never come across an easy way but it can be done either this way;

ls -l|grep ^-r|wc -l

(this assumes all "files" have at least owner read permission - not a good way to get an accurate count) ..

or like this

ls -F|grep -v \/|wc -l

(This lists all files and puts a / after directory names ... filter out the directory name ... then count the files!)

Greg.
 
Oh ... also ... if there are no subdirectories in the directory you want to count, the best way is;

find . -type f|wc -l

.. but as I say, this only works if there are no subdirectories

Greg.
 
ls | wc -l

will count all files, inc those files that happen to be a directory themselves, in the current directory

ls -l | grep -v '^d' | wc -l

will exclude the directories Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Not being picky Mike, but ls -l | grep -v ^d | wc -l will give you 1 more than the total files because of the "total nnn" shown by ls -l at the top of the list ...

Sorry ... had to do that! ;-)

Greg.
 
Thanks for all the responses!
 
doh!

ok then....

ls -l | grep -v '^d|^total' | wc -l

you got me greg! <grin>
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top