Linux Count number of Files in Directory

Hi Guys,

I thought this may help you in some way.

I am only a intermediate user of Linux but like to share stuff that I think could be useful to others.

One of those things is How to count number of files in a given directory

Well in simplest form browse to your directory and type this

[geshi lang=”text” nums=”1″ target=”_self” ]

# ls   | wc -l

What this does is that its piping the output of list command to word count command. so if ls  return N number of lines then wc -l will be N.

Second way to do it is just executing ls -l command as shown below

[geshi lang=”text” nums=”1″ target=”_self” ]

[user@web folder]# ls -l
total 1397304

Notice the total value that the number of lines there. Now in the above command symbolic links are counted as files and also subdirectories get returned.

You can have subdiretories excluded as shown with another Linux command called find

[geshi lang=”text” nums=”1″ target=”_self” ]

-type f means read files only. So no symlinks are returned.

 

I hope that you found this post useful

Cheers,

 

Advertisement

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.