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 -lIt 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?
110 Answers
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 -cIf 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
4Compare with the accepted answer:
$ ls -lR ./*.mp4 | wc -l
3Or 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 6 Here you can do this way
ls -lR /path/to/dir/*.jpg | wc -lThis gives you count
7This one finds, sorts, and lists all files by extension in order:
find . -type f | sed 's/.*\.//' | sort | uniq -c 4 I think it's very simple as following commands.
$ find . -name "*.mp4" | wc -l
8or
$ find . | grep -i ".mp4$" | wc -l
8I 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
8In addition, ls -lR can be used as find .
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.
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 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 -lif you want search to include subdirectories
ls -lR /path/to/directory | grep ".mp4$" | wc -l 9 In bash, one cold resort to using arrays with glob:
$ files=( *.mp4 )
$ echo ${#files[@]}
30 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 1 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