quick....what's the Unix command to find the total number of files in a directory?

bigrash

Lifer
Feb 20, 2001
17,648
28
91
did a search everywhere and the only thing that I could find was wc which is wordcount. I couldn't find any command that counts the number of files though.

btw..it's for my homework
 

bigrash

Lifer
Feb 20, 2001
17,648
28
91
hey thanks jaybittle. That's what I needed.


I tried to find the total number of files including hidden files and I tried this:

ls -la | wc -w

I got some weird number though. is that command correct
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
ls -la | wc -l

wc -l will count by line, the -l flag will give you 1 file per line.

EDIT: Thats how I do it (or I look at the Total at the end of the ls). I think wc -c will give you a character count...
 

bigrash

Lifer
Feb 20, 2001
17,648
28
91
thanks n0cmonkey. That's exactly what I was looking for.

DaHitman, that command doesn't show the number for hidden files
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You should probably use this:

ls -1aA | wc -l

The problem with ls -la is that it will give you 3 extra lines in the count:
total 8524
.
..


Note that the first argument to ls is a one, not a lower case L.

 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< 1 extra line. "." and ".." are files. :) >>



Well, yea ... everything is a file in unix :D.
I assumed he didn't want to count those two, but I could be wrong.

Another thing ... if you only want to count files which are not also directories, you can do this:

ls -1ap | grep -v $'/' | wc -l

You should be able to achieve the same thing with the -I option to ls, but I couldn't get it to work !?!