How to determine the size of a file or directory on Linux

If you are using Linux du Command gives you both the actual disk usage and the true size of a file or directory. We explain why these values ​​are not the same.

Actual disk usage and size

The size of a file and the space it takes up on your hard drive are rarely the same. The storage space is allocated in blocks. If a file is smaller than a block, a whole block is still assigned to it, since the file system does not have a smaller basic unit available.

If the size of a file is not an exact multiple of blocks, the space it occupies on the hard disk must always be rounded up to the next whole block. To the exampleif a file is larger than two blocks but smaller than three, it will still take three blocks of space to store it.

Two measurements are used with respect to file size. The first is the actual size of the file, which is the number of bytes of content that make up the file. The second is the effective size of the file on the hard drive. This is the number of file system blocks required to store this file.

An example

Let’s look at a simple one example. We redirect a single character to a file to make a small file:

echo "1" > geek.txt


Now we’re going to use the long format listing, lsto see the file length:

ls -l geek.txt

The length is the numeric value that follows it dave dave Entries are two bytes. Why is it two bytes when we only sent one character to the file? Let’s see what happens in the file.

We’ll use that hexdump Command that gives us an exact number of bytes and enables us to “see” non-printable characters as hexadecimal values. We will too -C (Canonical) option to force the output to show hexadecimal values ​​in the body of the output as well as their alphanumeric character equivalents:

hexdump -C geek.txt

The output shows us that starting at offset 00000000, there is a byte in the file that contains a hexadecimal value of 31 and one byte that contains a hexadecimal value of 0A. If possible, these values ​​are shown as alphanumeric characters in the right part of the output.

The hexadecimal value 31 is used to represent the digit one. The hexadecimal value 0A is used to represent the newline character, which cannot be displayed as an alphanumeric character, so it is displayed as a period (.) Instead. The line feed character is added by echo . By default, echostarts a new line after displaying the text that needs to be written in the terminal window.

This is the same as the output from ls and matches the file length of two bytes.

How to use the ls command to list files and directories on Linux

Now let’s use that du Command to display the file size:

du geek.txt


It says the size is four, but four of what?

There are blocks and then there are blocks

When du reports file sizes in blocks, the size used depends on several factors. You can specify which block size to use on the command line. If you don’t force du To use a particular block size, it follows a number of rules to decide which one to use.

First it checks the following Environment variables:

  • DU_BLOCK_SIZE
  • BLOCK SIZE
  • BLOCK SIZE

If one of these is present, the block size is set, and du stop checking. If none are set, du has a standard block size of 1,024 bytes. Unless an environment variable called POSIXLY_CORRECT is set. If that’s the case, du has a standard block size of 512 bytes.

So how do we find out which one is used? You can check any environment variable to find out, but there is a faster way. Instead, let’s compare the results to the block size the file system is using.

To find out the block size that the file system is using, we’ll use the tune2fs Program. We then use the -l (List superblock) Option, pass the output through grep, and then Print lines that. contain the word “block”.


In this example, let’s look at the file system on the first partition of the first hard drive, sda1and we have to use sudo:

sudo tune2fs -l /dev/sda1 | grep Block

The block size of the file system is 4,096 bytes. If we divide that by the result we got du (four), it shows the du The standard block size is 1,024 bytes. We now know some important things.

First, we know that the smallest amount of file system space that can be used to store a file is 4,096 bytes. That means that even our tiny 2-byte file takes up 4 KB of disk space.

The second thing to be aware of are applications designed for reporting disk and file system statistics such as: du, ls, and tune2fs, may have different ideas about what “block” means. the tune2fs Application reports true file system block sizes while ls and du can be configured or forced to use different block sizes. These block sizes are not intended to relate to the file system block size; they are just “blocks” that use these commands in their output.

Finally, apart from using different block sizes, the responses from du and tune2fs convey the same meaning. the tune2fs The result was a block of 4,096 bytes, and the du The result was four blocks of 1,024 bytes.

Using du

Without any command line parameters or options, du lists the total space used by the current directory and all subdirectories.

Let’s take a look at one example:

du


The size is specified in the standard block size of 1,024 bytes per block. The entire subdirectory tree is traversed.

Using du in another directory

If you want du To report a different directory than the current one, you can pass the path to the directory on the command line:

du ~/.cach/evolution/

Using du on a specific file

If you want du To report on a specific file, pass the path to that file on the command line. You can also pass a shell pattern to a selected group of files, such as: *.txt:

du ~/.bash_aliases

Reports on files in directories

Have du Report on the files in the current directory and subdirectories, use the -a (all files) Option:

du -a

For each directory, the size of each file and a total size for each directory are reported.

Restrict the directory tree depth

You can say du to list the directory tree to a certain depth. Use the -d (max. depth) and enter a depth value as a parameter. Note that all subdirectories are scanned and used to calculate the reported totals, but not all are listed. To set a maximum directory depth of one level, use this command:

du -d 1


The output lists the total size of this subdirectory in the current directory and also provides a total size for each one.

To list directories one level down, use this command:

du -d 2

Setting the block size

You can use the … block Option to set a block size for du for the current operation. To use a one byte block size, use the following command to get the exact size of the directories and files:

du --block=1

If you want to use a one megabyte block size, you can use the -m (Megabyte) option that is the same as --block=1M:

du -m

If you want the sizes to appear in the most appropriate block size according to the space used by the directories and files, use the -h (human readable) option:

du -h

To see the apparent size of the file rather than the hard drive space used to save the file, use the --apparent-size Opportunity:

du --apparent-size


You can combine this with that -a (all) option to show the apparent size of each file:

du --apparent-size -a

Each file is listed along with its apparent size.

Show totals only

If you want du to report the grand total for the directory only, use the -s (summarize) option. You can also combine this with other options, such as: -h (human readable) option:

du -h -s

Here we use it with the --apparent-size Opportunity:

du --apparent-size -s

Show change times

To view the time and date it was created or last modified, use the --time Opportunity:

du --time -d 2

Strange Results?

If you see strange results from du , especially when you reference sizes to the output of other commands, it is usually due to the different block sizes that various commands can be set to or which they are set to by default. This could also be due to the differences between the actual file sizes and the space required to store them.

If you need to customize the output of other commands, experiment with the --block Option in du.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts