LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-03-2005, 11:03 AM   #1
ElricM
LQ Newbie
 
Registered: Jul 2005
Posts: 5

Rep: Reputation: 0
How to determine # files in a directory


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.

Thanks!
 
Old 09-03-2005, 11:45 AM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
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.
 
Old 10-02-2005, 01:37 PM   #3
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
Hi,

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.

Thank you very much for your idea.
 
Old 10-02-2005, 01:40 PM   #4
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
sorry, there was a small typo in my privious post:

ls -*.pdf should be ls *.pdf
 
Old 10-02-2005, 02:21 PM   #5
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
I found the answer. It is

ls *.pdf | wc -l
 
Old 04-06-2009, 10:36 AM   #6
zylstra
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by rose_bud4201 View Post
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.
 
Old 04-06-2009, 11:12 AM   #7
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Here's the directory listing:

ls -laR
Code:
.:
total 0
drwxr-xr-x 3 .
drwxr-xr-x 3 ..
-rw-r--r-- 1 .hidden.file
drwxr-xr-x 2 directory
-rw-r--r-- 1 file
lrwxrwxrwx 1 link.to.directory -> directory
lrwxrwxrwx 1 link.to.file -> file

./directory:
total 0
drwxr-xr-x 2 .
drwxr-xr-x 3 ..
-rw-r--r-- 1 file.inside.directory
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).

3. grep -v ^$ omits empty lines.

4. wc -l counts the number of the lines.

Last edited by w1k0; 04-06-2009 at 11:28 AM.
 
Old 04-06-2009, 11:51 AM   #8
zylstra
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
I was just coming back to say that the previous answer also double-counted directories, but your solution gets it exactly.
 
Old 09-03-2010, 04:10 AM   #9
tkalfaoglu
Member
 
Registered: Oct 2003
Location: Izmir, Turkey
Distribution: Fedora Core 10
Posts: 68

Rep: Reputation: 15
How about adding the subdirectory name to the output,
to get something like:

/var/log 54656
/var/etc 2
/var/lib 2222

etc
 
Old 01-24-2012, 02:37 PM   #10
creatorbri
LQ Newbie
 
Registered: Aug 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine what files are in what package? encomium Slackware 11 04-30-2009 04:59 PM
Determine os install directory Jebrew Linux - Software 2 08-12-2005 10:48 PM
Bash scripting and trying to determine whether a directory exists? obelxi Programming 9 04-18-2005 11:22 PM
Determine Executable Starting Directory codeblue Programming 4 10-17-2003 10:11 AM
trying to determine the size of all files in a directory riddlebox80 Programming 7 06-11-2003 07:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration