207

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

9 Answers 9

307

Maybe something like this will do the trick:

find . -type f | wc -l

Try the command from the parent folder.

  • find . -name <pattern> -type f finds all files in the current folder (.) and its subfolders.
  • -name <pattern> only looks for certain files that match the specified pattern. The match is case-sensitive. If you need the match to be case-insensitive, use -iname instead.
  • The result (a list of files found) is passed (|) to wc -l which counts the number of lines.
5
  • 3
    The solution will fail on files which names contain a newline. Commented Apr 8, 2011 at 14:23
  • 2
    @user unknown: find . -type f -ls | wc -l
    – arrange
    Commented Nov 22, 2013 at 21:24
  • 2
    even faster: find . -type f -print0 | tr -d -c '\0' | wc -c
    – arrange
    Commented Nov 22, 2013 at 21:44
  • 18
    @arrange: even faster: find . -type f -printf . | wc -c - I adopt the print for my solution instead of my -exec echo . Commented Nov 23, 2013 at 3:41
  • 1
    Be aware that this also counts hidden files starting with a dot. I consider this a feature rather than a bug, but it is good to know.
    – cgogolin
    Commented Jan 17, 2016 at 13:13
34

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.

24

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
4
  • 2
    Those will not count hidden files. Commented Apr 20, 2019 at 2:42
  • True. I'm more inclined to accept and use your answer as the solution.
    – user38537
    Commented Apr 23, 2019 at 18:17
  • Actually, not counting hidden files / files in hidden directories is an useful feature while working inside a subversion or git repository!
    – lfurini
    Commented Apr 8, 2020 at 13:00
  • And this is very slow on large folders because ls -l will sort the output.
    – Dr_Zaszuś
    Commented Jul 12, 2020 at 7:25
20

The fastest and easiest way, is to use tree. Its 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.

13
find -type f -printf . | wc -c

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

3
  • 5
    Filenames containing new lines is an incredibly rare edge case. Commented Jul 22, 2013 at 13:49
  • 8
    @DisgruntledGoat: So an error will be extremely hard to find. Commented Jul 23, 2013 at 2:41
  • I like a good edge case, especially many years later.
    – user38537
    Commented Apr 20, 2019 at 2:16
6

Use this command for each folder in the path

for D in *; do echo $D; find $D -type f| wc -l; done
3

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

2
  • This solution counts also the folders, I gave the mark cause it matched my occasion that I didnt want to count them in :)
    – topless
    Commented Apr 8, 2011 at 12:12
  • The solution will fail on files which names contain a newline. Commented Apr 8, 2011 at 14:23
1

find seems to be quicker than tree so I used below to count files in each directory of the current working directory (ignoring files in CWD) with allowing directories to have spaces:

ls -d */ | while read dir_line do echo -n "$dir_line :" find "$dir_line" -type f | wc -l done

2
  • Parsing output of ls is very bad idea.
    – sourav c.
    Commented Jul 21, 2016 at 14:49
  • Great code, how can I arrange the output lines say in an increasing or decreasing count of files Commented Nov 19, 2017 at 10:08
0

I'd go with this option myself:

ls -alR | grep -c ^-

1
  • 4
    Please add some details ... Commented May 29, 2016 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .