Find the exact size of certain files in Linux

man du

One day, there may be a lot of files in more than one extension in the macOS, Linux or server you use, and you may be wondering about the size of these files or just want to see the size of the files with the corresponding extension for a specific study.

The other day, the server I was working on had both * .webm and * .mp4 file extensions, and I had to upgrade the server’s disk, or I would delete those with the same .mp4 extension from the same videos.

There are multiple ways to do this. I will only describe three different and most frequently used commands.

For example, if there is no hidden folder in the directory you are in, you can learn the size of .jpg files in that directory with the simplest method with the following command.

First of all, since I want to list the jpg files that exist in my directory and see one, I run the command below and display its output. Let’s check my Documents folder on my Mac, what I have.

➜  Documents ls -la | grep jpg
[email protected]  1 ercanermis  staff  1607683 Feb 26 10:14 wallpaper-for-vertical-monitor.jpg
[email protected]  1 ercanermis  staff   242165 Aug 19  2019 10155854826986546_245396163509039923.jpg
[email protected]  1 ercanermis  staff   272199 Feb 26 10:00 C2014_Q2.jpg

Then what are the sizes of these jpg files? For the answer to the question, the following command is sufficient.

➜  Documents du -ch *.jpg
1.5M	wallpaper-for-vertical-monitor.jpg
240K	10155854826986546_245396163509039923.jpg
268K	C2014_Q2.jpg
2.0M	total

If the total size of these files is important to you, you can do as follows;

➜  Documents du -ch *.jpg | grep total
2.0M	total

Is it really 3 jpg files into my Documents folder?

I don’t think so and I decided to check what I have inside. Because I will have some hidden files or folders. Let’s check deeper what are the sizes of all jpg files?

➜  Documents find . -type f -name '*.jpg' -exec du -ch {} + | grep total
4.6M	total

Hmm. Now, it says 4.6M total but previously, I mean “du -ch *.jpg | grep total” command output was 2.0M. How it possible? Because I have hidden files and folders. Let’s check them.

➜  Documents find . -type f -name '*.jpg' -exec du -ch {} +
1.5M	./free-wallpaper-for-vertical-monitor.jpg
268K	./C2014_Q2.jpg
240K	./10155854826986546_245396163509039923.jpg
372K	./git/Resources/main/1/std-front.jpg
324K	./git/Resources/main/3/std-front.jpg
272K	./git/Resources/main/2/std-front.jpg
696K	./git/Resources/main/splash_background.jpg
4.0K	./git/Resources/popup/social/bg-description.jpg
 40K	./git/Resources/popup/news/background.jpg
 40K	./git/Resources/popup/rooms/bg.jpg
 80K	./git/Resources/popup/bundle-bg-2.jpg
 88K	./git/Resources/popup/bundle-bg-3.jpg
 88K	./git/Resources/popup/bundle-bg-1.jpg
 96K	./git/Resources/popup/bundle-bg-5.jpg
4.0K	./git/Resources/popup/eventLeftScroll.jpg
 92K	./git/Resources/popup/eventBg.jpg
 40K	./git/Resources/popup/progression/upperBg.jpg
8.0K	./git/Other/nd_gcsdk_image_default.jpg
4.0K	./git/Other/nd_gcsdk_loading_bg.jpg
 20K	./git/Other/docs/img.jpg
 84K	./git/Other/docs/framework_architecture.jpg
 44K	./Do_My_Verification/1.jpg
264K	./Do_My_Verification/2.jpg
4.6M	total

There you go! I forgot my git folder and I have some jpg files into it!

Conclusion

In two different ways, you learned to check the specific file extensions in a folder and list their size. You can run these commands on CentOS, ubuntu or macOS operating systems.

It will also work on different Linux distributions, such as Raspberry Pi.

0 0 vote
Article Rating