How to Get the Size of a Directory on Linux 2024

How to Get the Size of a Directory on Linux

To determine the actual size of a directory in Linux, use the du command, which stands for “disk usage“. Unlike the ls command, which displays the space used to store the meta-information for the directory, du calculates the total disk space used by the directory and its contents. It is important to note that the size of directories listed by ls is typically 4096 bytes (4 KB) and does not reflect the actual size of the directory’s contents. Therefore, use the du command to measure a directory’s disk usage accurately.

How to get the Size of a Directory

If you need to check the amount of file space used by specific files or directories, the du command can help. This command summarizes disk usage for each subdirectory in a directory if the specified path is a directory. If no path is given, du reports the disk usage of the current working directory. By default, du displays disk usage in bytes for the given directory and subdirectories. However, for better readability, it’s recommended to use a human-readable format. To get the total size of the /var directory, simply run this command:

sudo du -sh /var

The output will look like this:

Output

85G /var

Let’s understand the commands and the arguments:

  • To successfully execute the du command on the files and directories inside the /var directory, you must include sudo in the command. This is because most of the files and directories within the /var directory are owned by the root user and are inaccessible to regular users. Without including sudo, the “du” command will not be able to read the directory and will return an error message stating “du: cannot read directory.” So, include sudo in your command to avoid encountering this error.
  • s – shows only the total size of the specified directory, but cannot show the total file size for subdirectories.
  • h – display sizes in a readable format.
  • /var – The path of the targeted directory that you want the size of.

How to display disk usage of your first-level subdirectories on Linux

If you’re looking to showcase the disk usage of your first-level subdirectories, you have two choices to achieve this. The first option is to utilize the asterisk symbol (*) to match all directories that don’t commence with a period (.) character. This can be accomplished by appending the -c option to the du command, which provides a comprehensive sum of all sizes:

sudo du -shc /var/*

Output

24K	/var/db
4.0K	/var/empty
4.0K	/var/games
77G	/var/lib
4.0K	/var/local
0	/var/lock
3.3G	/var/log
0	/var/mail
4.0K	/var/opt
0	/var/run
196K	/var/spool
28K	/var/tmp
85G	total

an alternative way can also be followed to get a report about the disk usage of the first-level subdirectories. Simply use --max-depth option:

sudo du -h --max-depth=1 /var

Output

77G	  /var/lib
24K	  /var/db
4.0K	/var/empty
4.0K	/var/local
4.0K	/var/opt
196K	/var/spool
4.0K	/var/games
3.3G	/var/log
5.0G	/var/cache
28K	/var/tmp
85G	/var
85G	total

How to determine the actual size of a directory on Linux

If you’re looking to check the disk space used by a particular directory or file, the du command is all you need. However, if you want to determine the actual size of a directory, you’ll need to utilize the
–apparent-size option. The term “apparent size” refers to the amount of data that is present within a file.

sudo du -sh --apparent-size /var

When transferring a directory through protocols such as SCP, Rsync, or SFTP, it is essential to note that the amount of data transmitted over the network corresponds to the apparent size of the files. As a result, the space occupied on the source disk, displayed using du (without the –apparent-size option), may differ from the space used on the target destination. It’s crucial to keep this in mind to ensure a seamless and efficient transfer process.

Combine other commands with du on Linux

The other commands can be combined with du command with the pipe. for example, If you want to find the top 5 largest directories in the /var directory, you can use the du command with the sort and head commands. By piping the output of du to sort, you can sort the directories by their size, and then pipe the output to head to display only the largest 5 directories. This technique can help you quickly identify and manage large directories in your system.

sudo du -h /var/ | sort -rh | head -5

Output

85G	/var/
77G	/var/lib
75G	/var/lib/libvirt/images
75G	/var/lib/libvirt
5.0G	/var/cache/pacman/pkg

Conclusion

The du command can be used to find the size of the directory on a Linux system and by piping other commands you can get more information in your Linux system.

This tutorial has shown you how you can use the du commands and others comprehensively. If you have any questions feel free to comment below.

James

James

Hi, this is James, a tech specialist and a core member of the TrioTeam. I like to research and write content about various interesting things especially tech-related stuff, and also have an interest in philosophical discussion. I have been writing solutions for technical problems, how-to tutorials, technology reviews, tools and websites, and so on for TrioGuide.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo