Atlantic.Net Blog

How to Check Size of Files and Directory on Linux

If you are a system administrator, then you need to be aware of the total disk space occupied by the file system so you can identify unwanted files and directories, remove them and free the disk space. The du command stands for “Disk Usage” and can be used to check the information of disk usage of files and directories in your system.

In this guide, we will show you how to check the size of files and directories in Linux.

Step 1 – Basic Syntax

The basic syntax of the du command is shown below:

du [OPTION]... [FILE]...

To display all options available with the du command, run:

du --help

You should see the following screen:
du help page

Step 2 – Display Disk Usage Summary of a Directory

Running the du command without any options will display the disk usage summary of a directory.

For example, to check the disk usage of the directory /opt, run the following command:

du /opt/

This will print the disk usage summary in kilobytes in the first column:

440	/opt/google/earth/pro/shaders
8804	/opt/google/earth/pro/resources/gdal
16	/opt/google/earth/pro/resources/flightsim/aircraft
12	/opt/google/earth/pro/resources/flightsim/hud
56	/opt/google/earth/pro/resources/flightsim/controller
12	/opt/google/earth/pro/resources/flightsim/keyboard
8	/opt/google/earth/pro/resources/flightsim/planet
112	/opt/google/earth/pro/resources/flightsim
12660	/opt/google/earth/pro/resources
15220	/opt/google/earth/pro/lang
1180	/opt/google/earth/pro/plugins/imageformats
468	/opt/google/earth/pro/plugins/bearer
396	/opt/google/earth/pro/plugins/platforms
80	/opt/google/earth/pro/plugins/printsupport
72	/opt/google/earth/pro/plugins/audio
18892	/opt/google/earth/pro/plugins
244252	/opt/google/earth/pro
244256	/opt/google/earth
2828	/opt/google/chrome/swiftshader
10140	/opt/google/chrome/WidevineCdm/_platform_specific/linux_x64
10144	/opt/google/chrome/WidevineCdm/_platform_specific
10156	/opt/google/chrome/WidevineCdm
20	/opt/google/chrome/cron

Step 3 – Display Total Size of a Directory

You can use the du command with the -c option to display the disk usage summary of each file with total size.

du -c /opt

You should see the following output:

440	/opt/google/earth/pro/shaders
8804	/opt/google/earth/pro/resources/gdal
16	/opt/google/earth/pro/resources/flightsim/aircraft
12	/opt/google/earth/pro/resources/flightsim/hud
56	/opt/google/earth/pro/resources/flightsim/controller
12	/opt/google/earth/pro/resources/flightsim/keyboard
8	/opt/google/earth/pro/resources/flightsim/planet
112	/opt/google/earth/pro/resources/flightsim
12660	/opt/google/earth/pro/resources
15220	/opt/google/earth/pro/lang
1180	/opt/google/earth/pro/plugins/imageformats
468	/opt/google/earth/pro/plugins/bearer
396	/opt/google/earth/pro/plugins/platforms
80	/opt/google/earth/pro/plugins/printsupport
72	/opt/google/earth/pro/plugins/audio
18892	/opt/google/earth/pro/plugins
244252	/opt/google/earth/pro
244256	/opt/google/earth
2828	/opt/google/chrome/swiftshader
10140	/opt/google/chrome/WidevineCdm/_platform_specific/linux_x64
10144	/opt/google/chrome/WidevineCdm/_platform_specific
10156	/opt/google/chrome/WidevineCdm
20	/opt/google/chrome/cron
599088	/opt
599088	total

Step 4 – Display Disk Usage in Human Readable Format

By default, the du command shows the size of all files and directories in kilobytes. You can use the -h option to display the size of all files and directories in human-readable format:

du -h /opt

You should see the following output:

440K	/opt/google/earth/pro/shaders
8.6M	/opt/google/earth/pro/resources/gdal
16K	/opt/google/earth/pro/resources/flightsim/aircraft
12K	/opt/google/earth/pro/resources/flightsim/hud
56K	/opt/google/earth/pro/resources/flightsim/controller
12K	/opt/google/earth/pro/resources/flightsim/keyboard
8.0K	/opt/google/earth/pro/resources/flightsim/planet
112K	/opt/google/earth/pro/resources/flightsim
13M	/opt/google/earth/pro/resources
15M	/opt/google/earth/pro/lang
1.2M	/opt/google/earth/pro/plugins/imageformats
468K	/opt/google/earth/pro/plugins/bearer
396K	/opt/google/earth/pro/plugins/platforms
80K	/opt/google/earth/pro/plugins/printsupport
72K	/opt/google/earth/pro/plugins/audio
19M	/opt/google/earth/pro/plugins
239M	/opt/google/earth/pro
239M	/opt/google/earth
2.8M	/opt/google/chrome/swiftshader
10M	/opt/google/chrome/WidevineCdm/_platform_specific/linux_x64
10M	/opt/google/chrome/WidevineCdm/_platform_specific
10M	/opt/google/chrome/WidevineCdm
20K	/opt/google/chrome/cron
16K	/opt/google/chrome/MEIPreload
21M	/opt/google/chrome/locales
92K	/opt/google/chrome/default_apps
255M	/opt/google/chrome
493M	/opt/google

Step 5 – Display Total File Size of a Directory

You can use the -s option to display the only total size of the directory.

du -hs /opt

You should see the following output:

586M	/opt

Step 6 – Sort File and Directory by Size

You can use the du command with sort to display and sort all files and directories by their size.

du /opt | sort -n -r

You should see the following output:

599088	/opt
504580	/opt/google
260320	/opt/google/chrome
244256	/opt/google/earth
244252	/opt/google/earth/pro
94268	/opt/ffmpeg
58604	/opt/ffmpeg/bin
28400	/opt/ffmpeg/lib
21360	/opt/google/chrome/locales
18892	/opt/google/earth/pro/plugins
15220	/opt/google/earth/pro/lang
12660	/opt/google/earth/pro/resources
10156	/opt/google/chrome/WidevineCdm
10144	/opt/google/chrome/WidevineCdm/_platform_specific
10140	/opt/google/chrome/WidevineCdm/_platform_specific/linux_x64
8804	/opt/google/earth/pro/resources/gdal
5972	/opt/ffmpeg/share
5668	/opt/ffmpeg/share/doc
5664	/opt/ffmpeg/share/doc/ffmpeg
2828	/opt/google/chrome/swiftshader
1288	/opt/ffmpeg/include

Step 7 – Find the Largest File or Directory

You use the du command with sort to find the largest file or directory in your system.

Run the du command with -a option to find and display the largest file and directory:

du -a / | sort -n -r | head -n 10

You should see the following output:

1653740	/
931232	/usr
483812	/swapfile
425916	/usr/lib
268820	/usr/src
164476	/usr/share
156912	/var
151216	/usr/lib/x86_64-linux-gnu
142644	/var/lib
137388	/usr/lib/modules

Conclusion

In this guide, we explained how to check the size of files and directories with different options. This will help you to track the file system usage on Linux. Get started on VPS hosting from Atlantic.Net.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year