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 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?

share|improve this question
1  
The simple, correct, straightforward answer is @louis-matthijssen one. ls -1 handling of newline characters make, in this case, sensible to parse ls output. The marked one is wrong for the use of -R flag. –  Rmano Apr 24 at 16:25

6 Answers 6

up vote 0 down vote accepted

Here you can do this way

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

This gives you count

share|improve this answer
1  
Why the -R? Why doing a full stat when you just need the filename? Why not ls -1 *.jpg| wc -l? (ok, not working if you have filenames with newlines in it. In that case you deserve it ;-) ...) –  Rmano Apr 24 at 16:14
    
This is for listing subdirectories recursively. –  shrikant1712 Apr 24 at 16:19
2  
...and the question was "counting the number of files in a directory". Moreover, this will list recursively all content of subdirectories which name end in .jpg, not files in subdirectories ending in .jpg. Have you ever tested it? –  Rmano Apr 24 at 16:28
1  
The question says "files in a specific directory", which implies that subdirectory recursion is not wanted. –  David Richerby Apr 24 at 16:58
1  
This also fails if there are too many files in the directory (because *.jpg is expanded by the shell, not by ls) and if there are files whose names begin with hyphens. –  David Richerby Apr 24 at 18:41

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" -exec 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
share|improve this answer
1  
you do not need the "-exec printf ..." , just find | wc -l . I do not think you want the -c option –  bodhi.zazen Apr 24 at 15:54
1  
No, you really do need it. find | wc -c counts the number of characters in the file listing, and find | wc -l gives you the wrong result for file names containing newlines. –  l0b0 Apr 24 at 15:56
1  
+1 for the "don't parse ls" link. It's a best practice, and vital if you're going to act on the output. A simple ls is probably pretty safe, but I like encouraging good practices. :) –  K. Darien Freeheart Apr 24 at 16:01
2  
find . -name "*.mp4" | wc -l –  bodhi.zazen Apr 24 at 16:08
1  
@bodhi.zazen You didn't even run my code, you might want to try that. –  l0b0 Apr 24 at 17:09

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
share|improve this answer
    
does not give a count –  bodhi.zazen Apr 24 at 16:15
    
gave it to me when used with wc -l –  Back.Slash Apr 24 at 16:16
    
use ls without piping to grep and shorten your answer. It has been posted at least twice now. –  bodhi.zazen Apr 24 at 16:19
    
@bodhi.zazen ls /directory/*.mp4 causes the shell to expand the glob and execute something like ls /directory/file1.mp4 /directory/file2.mp4 ... This will fail if the directory contains more mp4 files than can be passed as arguments to ls. –  David Richerby Apr 24 at 17:03
    
@DavidRicherby - it is not my ls command , I use find ;) –  bodhi.zazen Apr 24 at 17:14

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 .

share|improve this answer

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.

share|improve this answer
    
Not sure why this was downvoted, it works –  bodhi.zazen Apr 24 at 16:13
    
Yes. Even for files with newlines embedded --- ls -1 output a question mark in that case, which is a sane behavior... +1 from me. A lot of people comment without testing. –  Rmano Apr 24 at 16:21
    
Since *.mp4 is expanded by the shell, not ls, this will fail if there are so many .mp4 files in the directory that the list of them can't be passed to ls as arguments. –  David Richerby Apr 24 at 17:06
    
@Rmana Test it in a directory containing a file called --.mp4 –  David Richerby Apr 24 at 19:33
    
@DavidRicherby yes, you are right. -- will solve the second case; the former will rise an error message (you need a lot of files!) and another nice question here. Corner cases, but worth noticing, yes. –  Rmano Apr 25 at 12:59

First, change to the directory you want. For example:

cd ~/Videos

Then:

ls -la | grep ".mp4" | wc -l

This lists the contents of your directory, including hidden files, with one file per line. It passes the result to grep which will only show items containing the .mp4 extension. This list is then passed to "word count" to count the lines.

The result will output the number of lines that match, which will correspond to the number of .mp4 files you have in that directory.

share|improve this answer
    
While this also works, just use ls without the pipe. –  bodhi.zazen Apr 24 at 16:14
    
This does not work. It gives the wrong answer if the directory contains a file called ".mp45", for example. It also fails if there are files with newlines in their names. By the way, "grep -c" outputs just the number of matching lines. –  David Richerby Apr 24 at 16:53

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.