203

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

topless
  • 7,255
  • 9
  • 37
  • 43

9 Answers9

302

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.
BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
sagarchalise
  • 23,668
  • 12
  • 72
  • 85
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.

Egil
  • 14,002
  • 2
  • 45
  • 54
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
Seth
  • 57,282
  • 43
  • 144
  • 200
user38537
  • 683
  • 2
  • 7
  • 16
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.

tinlyx
  • 3,132
  • 6
  • 34
  • 53
not2qubit
  • 606
  • 8
  • 14
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.

user unknown
  • 6,447
  • 3
  • 34
  • 63
6

Use this command for each folder in the path

for D in *; do echo $D; find $D -type f| wc -l; done
Sriram Murali
  • 464
  • 5
  • 5
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

david
  • 2,410
  • 17
  • 10
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

Mike Bounds
  • 131
  • 2
  • 7
0

I'd go with this option myself:

ls -alR | grep -c ^-