Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short command to find total size of files matching a wild card

Tags:

linux

bash

I could envision a simple shell script that would accomplish what I want by just iterating through a list of files in a directory and summing the individual size but was wondering if there was already a more concise way to do that.

something like

ls -lh *.jpg 

that gives me the total size of just all the jpg files in the directory

like image 462
atxdba Avatar asked Jul 23 '14 19:07

atxdba


People also ask

How do you find the total file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do you find the total size of a file in Linux?

The best Linux command to check file size is using du command. What we need is to open the terminal and type du -sh file name in the prompt. The file size will be listed on the first column. The size will be displayed in Human Readable Format.

How do you check the size of the files in a directory?

How to view the file size of a directory. To view the file size of a directory pass the -s option to the du command followed by the folder. This will print a grand total size for the folder to standard output. Along with the -h option a human readable format is possible.


2 Answers

Try du to summarize disk usage:

du -csh *.jpg 

Output (for example):

8.0K sane-logo.jpg 16K sane-umax-advanced.jpg 28K sane-umax-histogram.jpg 24K sane-umax.jpg 16K sane-umax-standard.jpg 4.0K sane-umax-text2.jpg 4.0K sane-umax-text4.jpg 4.0K sane-umax-text.jpg 104K total 

du does not summarize the size of the files but summarizes the size of the used blocks in the file system. If a file has a size of 13K and the file system uses a block size of 4K, then 16K is shown for this file.

like image 60
Cyrus Avatar answered Oct 14 '22 13:10

Cyrus


You can use this function :

dir () { ls -FaGl "${@}" | awk '{ last_size += $4; print }; END { print last_size }'; } 

also you can use this command this is shorter and give you better result!

find YOUR_PATH -type f -name '*.jpg' -exec du -ch {} + 
like image 28
Mazdak Avatar answered Oct 14 '22 13:10

Mazdak