75

I want to count the total number of files in particular directory that ends with ".mp4" extension.

I am getting following command:

ls -F |grep -v / | wc -l

It count all the files in particular directory, but I want the count of files that end with .mp4 extension.

Is there any Ubuntu command for that?

fossfreedom
  • 174,526
Prakash H
  • 2,631

10 Answers10

70

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
l0b0
  • 9,271
38

Here you can do this way

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

This gives you count

sk1712
  • 542
32

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

find . -type f | sed 's/.*\.//' | sort | uniq -c
Eric Carvalho
  • 55,453
squozen
  • 321
16

I think it's very simple as following commands.

$ find . -name "*.mp4" | wc -l
8

or

$ find . | grep -i ".mp4$" | wc -l
8

I think that above commands calculate count of files and directories names *.mp4

so I suggest you use -type f option as find parameter as following.

$ find . -name "*.mp4" -type f | wc -l
8

In addition, ls -lR can be used as find .

xiaodongjie
  • 2,874
4

You could use ls -1 *.mp4 | wc -l.

This will list all files ending on .mp4, printing each file on a new line (ls -1 *.mp4), pipe the output to wc which will count the number of new lines using the -l flag.

4

This should give you the list of file with .mp4

ls /path/to/directory | grep ".mp4$"

When combined with wc -l will give you count

ls /path/to/directory | grep ".mp4$" | wc -l

if you want search to include subdirectories

ls -lR /path/to/directory | grep ".mp4$" | wc -l
Back.Slash
  • 2,176
3

Check How To Count The Files By Extension In Linux?, it gives a good answer and explanation, you can use the following command:

find . -type f | sed -n 's/..*\.//p' | sort | uniq -c
Eliah Kagan
  • 119,640
2
ls | grep --count \.csv$

Replace (.csv with the extension you want)

Explanation: I think that a simple scheme is to fetch the list of files, and count the extension with grep. \. to match . and $ to match the extension at the end of line. It works because when the output of ls is piped, one file name is sent per line, which you can verify by running:

ls | cat
galoget
  • 3,023
2

You can always just use a for loop, which I think has the advantage of not requiring you to remember the flags of several different commands.

For example:

a=0; for i in ./*.jpg; do a=$(expr  $a + 1); done; echo $a 
zx485
  • 2,865
1

In bash, one cold resort to using arrays with glob:

$ files=( *.mp4  )
$ echo ${#files[@]}
30