Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to find the total count of the number of files under a folder and all its sub folders.

share|improve this question

7 Answers 7

up vote 32 down vote accepted

May be something like

find . -type f | wc -l

would do the trick. Try the command from the parent folder.

share|improve this answer
    
The solution will fail on files which names contain a newline. –  user unknown Apr 8 '11 at 14:23
1  
@user unknown: find . -type f -ls | wc -l –  arrange Nov 22 '13 at 21:24
    
even faster: find . -type f -print0 | tr -d -c '\0' | wc -c –  arrange Nov 22 '13 at 21:44
2  
@arrange: even faster: find . -type f -printf . | wc -c - I adopt the print for my solution instead of my -exec echo . –  user unknown Nov 23 '13 at 3:41

Use the tree command. You might need to install the tree package.

It will list all the files and folders under the given folder and list a summary at the end.

share|improve this answer
find -type f -printf . | wc -c

Don't count the output lines of find, because filenames, containing 99 newlines, will count as 100 files.

share|improve this answer
1  
Filenames containing new lines is an incredibly rare edge case. –  DisgruntledGoat Jul 22 '13 at 13:49
1  
@DisgruntledGoat: So an error will be extremely hard to find. –  user unknown Jul 23 '13 at 2:41

To count files (even files without an extension) at the root of the current directory, use:

ls -l | grep ^- | wc -l

To count files (even files without an extension) recursively from the root of the current directory, use:

ls -lR | grep ^- | wc -l
share|improve this answer

The fastest and most easy way, is to use tree. It's speed is limited by your output terminal, so if you pipe the result to tail -1, you'll get immediate result. You can also control to what directory level you like the results, using the -L option. For colorized output, use -C. For example:

$ tree share/some/directory/ | tail -1
558 directories, 853 files

$ tree -L 2 share/some/directory/ | tail -1
120 directories, 3 files

If it's not already there, you can get it here.

share|improve this answer

You can use find . | wc -l

find . will list all files and folders and theire contents starting in your current folder.
wc -l counts the results of find

share|improve this answer
    
This solution counts also the folders, I gave the mark cause it matched my occasion that I didnt want to count them in :) –  topless Apr 8 '11 at 12:12
    
The solution will fail on files which names contain a newline. –  user unknown Apr 8 '11 at 14:23

Use this command for each folder in the path

for D in *; do echo $D; find $D -type f| wc -l; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.