Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sorry, but can't figure this out from my sys admin books or man. How do I determine the total number of files in a directory, and then most preferrably the total number of files in a directory tree including all subdirectories (like -R).
Ideally, I'd like to know how many subdirectories a directory has, and then how many files. Something at least equivalent to right-clicking in Windows Explorer on a directory and selecting properties. You'll get back the total number of files and total space allocated.
du -sh ("disk used" -s(ummary) -h(uman readable units)) will get you the recursive answer to how big everything is.
To get the number of files in a directory, you can use any combination of ls and wc -l that you like ("word count" - l(ines of output)).
ls -R | wc -l
will get you the recursive answer, not counting hidden files.
ls -Ra | wc -l will include the hidden files.
ls -Ra | grep -v ^l | wc -l will get you recursive and hidden, but no symlinks...and so on.
Last edited by rose_bud4201; 09-03-2005 at 11:47 AM.
I have a similar question. I want to know the number of specific files in a directory. For an example: I have a number of .dat, .eps, .pdf etc. files in my directory /home/manog/work/. I change to /home/manog/work/ and I want to know the number of .pdf files. ls -*.pdf only lists the .pdf files, it does not give the number of .pdf files.
ls -R | wc -l
will get you the recursive answer, not counting hidden files.
This command counts not only files, but directories too. And I believe it includes a count for blank lines before each directory (to increase legibility when viewing the ouput), so this will overestimate the number of directories and files.
The directory with the subdirectories contains three regular files: .hidden.file, file, and file.inside.directory.
The command to display the number of the files in that directory and its subdirectories is:
ls -laR | grep -v ^[.dlt] | grep -v ^$ | wc -l
1. ls -laR displays the listing (if you like to count the files in the present directory only without its subdirectories omit R in -laR expression; if you like to omit hidden files omit a in -laR expression).
2. grep -v ^[.dlt] omits lines beginning with ., d, l, or t (if you like to count symbolic links as well omit l in ^[.dlt] expression).
I know this is an old thread, but it came up high on a Google Search for me, so I thought I'd add to it for posterity.
The following makes more sense to me than what's already been proposed:
Code:
find /my/directory/path | wc -l
Although cumbersome, the "find" command is also quite versatile and will allow you to customize what you're searching for to a great degree. For instance, the following counts all PDF files in your home directory:
Code:
find ~/. -type "*.pdf" | wc -l
And this does a count of all directories:
Code:
find ~/. -type d | wc -l
Pretty nifty. Read up on "find" for more advanced scenarios.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.