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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to count number of file?

Status
Not open for further replies.
Oct 22, 2001
215
US
What would be the best way to do a count of file(s) in a dir? I don't want to count any sub dir that may reside in the dir that I am presently in. TIA
 
ls -l | grep -v "^d" | wc -l

ls -l to get long output
grep -v "^d" filters off all lines beginning with "d" (which in ls -al output are directories)
wc -l counts the number of lines in the resulting output
 
You might also want to grep -v "^total" if your ls -l prints a total as the first line.
 
What about hidden files (with a dot a first character). If you want to count them use
Code:
ls -lA
(not needed if you are root).
 
Another way to get all files, including hidden files [note: although this uses find, it does not recurse]
Code:
find . \( -type d ! -name . -prune \) -o \( -type f -print \) | wc -l
Cheers, Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top