banner



How To Check File Size In Linux

A Linux terminal window on an Ubuntu-style laptop.
Fatmawati Achmad Zaenuri/Shutterstock

When you use the Linux du control, you obtain both the actual deejay usage and the true size of a file or directory. We'll explain why these values aren't the aforementioned.

Actual Disk Usage and Truthful Size

The size of a file and the infinite it occupies on your hard drive are rarely the aforementioned. Disk space is allocated in blocks. If a file is smaller than a cake, an entire block is even so allocated to it considering the file system doesn't take a smaller unit of real estate to utilise.

Unless a file'southward size is an exact multiple of blocks, the infinite it uses on the difficult drive must ever be rounded upwardly to the next whole cake. For instance, if a file is larger than two blocks but smaller than 3, information technology still takes iii blocks of space to store it.

Two measurements are used in relation to file size. The start is the actual size of the file, which is the number of bytes of content that brand up the file. The 2d is the effective size of the file on the hard disk. This is the number of file system blocks necessary to store that file.

An Case

Let'due south look at a uncomplicated example. Nosotros'll redirect a single grapheme into a file to create a small file:

echo "1" > geek.txt

Now, nosotros'll use the long format listing,ls, to look at the file length:

ls -l geek.txt

The length is the numeric value that follows the dave dave  entries, which is two bytes. Why is it two bytes when nosotros only sent one character to the file? Let's take a look at what'south happening inside the file.

We'll use the hexdump command, which will give us an exact byte count and permit us to "see" non-printing characters every bit hexadecimal values. We'll also use the -C (canonical) option to force the output to show hexadecimal values in the torso of the output, equally well as their alphanumeric character equivalents:

hexdump -C geek.txt

The output shows us that, starting time at showtime 00000000 in the file, there'due south a byte that contains a hexadecimal value of 31, and a one that contains a hexadecimal value of 0A. The right-mitt portion of the output depicts these values equally alphanumeric characters, wherever possible.

The hexadecimal value of 31 is used to stand for the digit one. The hexadecimal value of 0A is used to represent the Line Feed character, which cannot be shown as an alphanumeric character, then it's shown as a period (.) instead. The Line Feed grapheme is added by echo . By default,echostarts a new line later on information technology displays the text it needs to write to the terminal window.

That tallies with the output fromls and agrees with the file length of two bytes.

RELATED: How to Employ the ls Command to List Files and Directories on Linux

At present, we'll employ the du command to look at the file size:

du geek.txt

Information technology says the size is four, simply four of what?

There Are Blocks, and then There Are Blocks

When du reports file sizes in blocks, the size it uses depends on several factors. Yous can specify which cake size information technology should apply on the command line. If you lot don't force du to use a particular block size, it follows a gear up of rules to decide which one to use.

Outset, it checks the following environment variables:

  • DU_BLOCK_SIZE
  • BLOCK_SIZE
  • BLOCKSIZE

If whatsoever of these exist, the cake size is set, and du stops checking. If none are set,du defaults to a block size of 1,024 bytes. Unless, that is, an environment variable called POSIXLY_CORRECT is ready. If that'southward the case, du defaults to a block size of 512 bytes.

So, how do nosotros discover out which one is in utilize? You lot tin can bank check each surround variable to piece of work it out, but there's a quicker way. Allow'due south compare the results to the cake size the file organization uses instead.

To discover the cake size the file organisation uses, we'll utilise the tune2fs program. We'll and so use the -l (list superblock) pick, pipe the output through grep, and so impress lines that contain the word "Block."

In this example, we'll expect at the file system on the starting time partitioning of the first hard drive, sda1, and nosotros'll need to use sudo:

sudo tune2fs -l /dev/sda1 | grep Block

The file system cake size is 4,096 bytes. If we split up that by the result we got from du (four), it shows thedu default block size is 1,024 bytes. We at present know several of import things.

First, we know the smallest amount of file system real estate that can be devoted to storing a file is 4,096 bytes. This means even our tiny, two-byte file is taking upwards 4 KB of difficult drive space.

The second thing to continue in mind is applications dedicated to reporting on difficult drive and file system statistics, such as du, ls, andtune2fs, tin can take different notions of what "block" ways. The tune2fs application reports truthful file arrangement block sizes, whilels and du can be configured or forced to apply other cake sizes. Those cake sizes are non intended to relate to the file system block size; they're just "chunks" those commands apply in their output.

Finally, other than using different cake sizes, the answers from du and tune2fs convey the same meaning. The tune2fs result was one block of 4,096 bytes, and the du result was four blocks of 1,024 bytes.

Using du

With no command line parameters or options, du lists the total disk space the electric current directory and all subdirectories are using.

Let's accept a await at an example:

du

The size is reported in the default block size of 1,024 bytes per block. The unabridged subdirectory tree is traversed.

Using du on a Different Directory

If you wantdu to report on a different directory than the current ane, y'all can pass the path to the directory on the command line:

du ~/.cach/evolution/

Using du on a Specific File

If you wantdu to report on a specific file, pass the path to that file on the command line. You can also laissez passer a trounce pattern to a select a group of files, such as *.txt:

du ~/.bash_aliases

Reporting on Files in Directories

To have du study on the files in the electric current directory and subdirectories, use the -a (all files) option:

du -a

For each directory, the size of each file is reported, also as a total for each directory.

Limiting Directory Tree Depth

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

du -d i

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

To list directories one level deeper, use this command:

du -d ii

Setting the Block Size

You can use the block option to set a block size for du for the electric current functioning. To use a cake size of 1 byte, utilize the following control to get the verbal sizes of the directories and files:

du --block=1

If you want to apply a block size of one megabyte, you tin use the -m (megabyte) choice, which is the same equally --block=1M:

du -m

If you lot want the sizes reported in the most appropriate block size according to the disk infinite used by the directories and files, utilise the -h (human-readable) pick:

du -h

To meet the credible size of the file rather than the amount of hard drive space used to store the file, use the --apparent-size option:

du --credible-size

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

du --apparent-size -a

Each file is listed, along with its credible size.

Displaying Only Totals

If you desiredu to written report only the total for the directory, use the -s (summarize) option. Y'all can also combine this with other options, such equally the -h (homo-readable) option:

du -h -s

Here, we'll apply it with the --apparent-size choice:

du --apparent-size -s

Displaying Modification Times

To encounter the cosmos or last modification time and date, use the --time choice:

du --fourth dimension -d 2

Strange Results?

If you meet strange results from du , especially when yous cantankerous-reference sizes to the output from other commands, it'due south usually due to the different block sizes to which dissimilar commands can be set or those to which they default. It could also be due to the differences betwixt existent file sizes and the deejay space required to store them.

If you need to match the output of other commands, experiment with the --cake option in du.

How To Check File Size In Linux,

Source: https://www.howtogeek.com/450366/how-to-get-the-size-of-a-file-or-directory-in-linux/

Posted by: diazmays1961.blogspot.com

0 Response to "How To Check File Size In Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel