Count total number of files in particular directory with specific extension

Unfortunately this benign problem is difficult to solve in a way which supports all file names and is portable. This is safe (it handles hidden files, paths containing spaces, dashes and even newlines) and POSIX compatible:

find /path/to/directory -mindepth 1 -type f -name "*.mp4" -printf x | wc -c

If you don't want it to be recursive, simply add -maxdepth 1.

You shouldn't parse ls output.

Test:

$ cd -- "$(mktemp -d)"
$ touch -- -foo.mp4 .bar.mp4 .bat.mp4 'baz.mp4
> ban.mp4'
$ find . -mindepth 1 -type f -name "*.mp4" -exec printf x \; | wc -c
4

Compare with the accepted answer:

$ ls -lR ./*.mp4 | wc -l
3

Or other suggestions:

$ find . -name "*.mp4" | wc -l
5
$ ls -1 *.mp4 | wc -l
ls: invalid option -- '.'
Try 'ls --help' for more information.
0
$ find . -name "*.mp4" | wc -c # Answer fixed at a later time
51
$ find . -name "*.mp4" | wc -l
5
$ find . | grep -i ".mp4$" | wc -l
5
$ ls . | grep ".mp4$" | wc -l
3

Here you can do this way

ls -lR /path/to/dir/*.jpg | wc -l

This gives you count


This one finds, sorts, and lists all files by extension in order:

find . -type f | sed 's/.*\.//' | sort | uniq -c