Using du command in Linux to get file size

Posted on

System Operation           : Any Linux-based Operating System

Estimate file space usage using du command

One of the most useful command is ‘du’ which can be thought of the abbreviation of ‘disk usage’ to make it simple to remind. So, when we want to measure the usage of a disk or further more to check the information of disk usage of files and directories on a machine, we will automatically remember ‘du’ as an abbreviation of ‘disk usage’ if we associate it.

This is the syntax of doing it is :

du

The manual of the command itself also show that the command is used to estimate file space usage. We can get the information by typing it in the bash prompt :

man du
NAME
     du  - estimate file space usage

Below is one of the possible output which can be generated by executing the command :

username@hostname:~/test-folder$ du
16      .
username@hostname:~/test-folder$

The above command is used to estimate the size in the current active working directory. By default, the file size displayed is in blocks of data. All the reports is not displayed in bytes either kilobytes but it is in the bytes of data.  Most block size these days is 1024-byte blocks depends on the operating system. In the above command, we can see the total sizes in blocks of data. Another example

username@hostname:~/test-folder$ du *
4       test-file-1
4       test-file-2
4       test-file-3
username@hostname:~/test-folder$

The output of the command above seems unreadable since there aren’t any countable measurements displayed such as bytes, kilobytes or megabytes.

But by default, the command itself is actually showing size in 1kiB units. It means that if the output  displayed is 4 then the size will be 4×1024 bytes = 4096 bytes or 4kiB. And that is the actual measurement size of 1 block in the file system’s operating system currently used in the above command’s execution.

A block is a uniformly sized unit of data storage for a filesystem. Below is how to find out the size of a block in the currently used filesystem  :

root@hostname:/home/user/test-folder# tune2fs -l /dev/sda2 | grep -i 'block size'
Block size:               4096
You have new mail in /var/mail/root
root@hostname:/home/user/test-folder#

It is because the output of the ‘du’ command above is about displaying how much does the file actually allocated in the disk.  It only takes 1 block or 4096 bytes but the actual file size itself is different than that. To be able to get the actual file size, it need another parameter to the command.  Below is the parameter used to actually get the correct file size :

root@hostname:/home/user/test-folder# du -b *
32      test-file-1
32      test-file-2
32      test-file-3

You have new mail in /var/mail/root

root@hostname:/home/user/test-folder#

The output is 32 which is actually in bytes. Because the additional parameter, -b is actually for –bytes. So, the actual size of the file is 32 bytes.

One thought on “Using du command in Linux to get file size

Leave a Reply