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:
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/*
an alternative way can also be followed to get a report about the disk usage of the first-level subdirectories. Simply use
sudo du -h --max-depth=1 /var
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
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.