banner



How To Check The Size Of A File In Linux

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

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

Actual Disk Usage and True Size

The size of a file and the space information technology occupies on your difficult bulldoze are rarely the same. Disk space is allocated in blocks. If a file is smaller than a block, an entire cake is still allocated to it considering the file arrangement doesn't accept a smaller unit of real estate to use.

Unless a file'due south size is an verbal multiple of blocks, the space information technology uses on the hard bulldoze must always exist rounded upward to the next whole block. For case, if a file is larger than two blocks but smaller than iii, it even so takes three blocks of infinite to store it.

Two measurements are used in relation to file size. The beginning is the bodily size of the file, which is the number of bytes of content that make up the file. The second is the constructive size of the file on the hd. This is the number of file organization blocks necessary to store that file.

An Case

Let's look at a uncomplicated example. We'll redirect a unmarried grapheme into a file to create a small file:

echo "1" > geek.txt

Now, we'll use the long format list,ls, to look at the file length:

ls -fifty geek.txt

The length is the numeric value that follows the dave dave  entries, which is 2 bytes. Why is it two bytes when we only sent one graphic symbol to the file? Let's have a look at what's happening within the file.

We'll employ the hexdump control, which will give us an verbal byte count and let the states to "see" non-press characters every bit hexadecimal values. We'll too employ the -C (canonical) choice to strength the output to show hexadecimal values in the body of the output, likewise as their alphanumeric character equivalents:

hexdump -C geek.txt

The output shows us that, beginning at first 00000000 in the file, in that location's a byte that contains a hexadecimal value of 31, and a one that contains a hexadecimal value of 0A. The right-hand portion of the output depicts these values as alphanumeric characters, wherever possible.

The hexadecimal value of 31 is used to stand for the digit 1. The hexadecimal value of 0A is used to stand for the Line Feed character, which cannot exist shown every bit an alphanumeric grapheme, so it's shown as a period (.) instead. The Line Feed character is added by repeat . By default,echostarts a new line later on it displays the text information technology needs to write to the final window.

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

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

Now, nosotros'll utilize the du command to look at the file size:

du geek.txt

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

In that location Are Blocks, and And then At that place Are Blocks

When du reports file sizes in blocks, the size it uses depends on several factors. You lot can specify which block size it should use on the control line. If you don't force du to utilise a particular cake size, information technology follows a set of rules to make up one's mind which i to use.

First, it checks the following environment variables:

  • DU_BLOCK_SIZE
  • BLOCK_SIZE
  • BLOCKSIZE

If whatsoever of these be, the block size is set, and du stops checking. If none are fix,du defaults to a cake size of 1,024 bytes. Unless, that is, an surround variable called POSIXLY_CORRECT is set. If that's the instance, du defaults to a block size of 512 bytes.

So, how practice we find out which i is in use? You can check each surroundings variable to work it out, but there's a quicker way. Allow'southward compare the results to the block size the file system uses instead.

To observe the block size the file system uses, we'll use the tune2fs program. We'll then use the -fifty (list superblock) pick, pipe the output through grep, and and then print lines that contain the discussion "Block."

In this example, we'll expect at the file organization on the first partition of the first difficult drive, sda1, and we'll demand to use sudo:

sudo tune2fs -fifty /dev/sda1 | grep Block

The file organization cake size is 4,096 bytes. If we divide that past the issue nosotros got from du (four), information technology shows thedu default block size is 1,024 bytes. Nosotros at present know several important things.

Get-go, nosotros know the smallest amount of file system real manor that can be devoted to storing a file is 4,096 bytes. This means even our tiny, ii-byte file is taking upwardly 4 KB of hard drive infinite.

The 2nd thing to go on in mind is applications dedicated to reporting on difficult drive and file system statistics, such as du, ls, andtune2fs, tin have unlike notions of what "block" means. The tune2fs application reports true file arrangement block sizes, whilels and du can be configured or forced to use other block sizes. Those cake sizes are not intended to chronicle to the file arrangement block size; they're just "chunks" those commands use in their output.

Finally, other than using unlike 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 iv blocks of i,024 bytes.

Using du

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

Let's have a look 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 yous wantdu to report on a different directory than the current one, 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 written report on a specific file, laissez passer the path to that file on the control line. Yous can also pass a shell design to a select a group of files, such every bit *.txt:

du ~/.bash_aliases

Reporting on Files in Directories

To have du report on the files in the current directory and subdirectories, employ the -a (all files) pick:

du -a

For each directory, the size of each file is reported, as well every bit a total for each directory.

Limiting Directory Tree Depth

You can tell du to list the directory tree to a certain depth. To do and then, apply the -d (max depth) choice 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 gear up a maximum directory depth of ane level, use this command:

du -d 1

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

To list directories i level deeper, employ this command:

du -d 2

Setting the Block Size

You tin can utilize the cake selection to set a block size for du for the electric current performance. To use a block size of ane byte, use the following command to become the exact sizes of the directories and files:

du --block=1

If you want to use a block size of i megabyte, you tin can use the -chiliad (megabyte) selection, which is the same as --block=1M:

du -m

If you want the sizes reported in the most advisable block size according to the deejay space used past the directories and files, use the -h (human-readable) selection:

du -h

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

du --apparent-size

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

du --credible-size -a

Each file is listed, along with its apparent size.

Displaying Merely Totals

If yous wantdu to report only the total for the directory, use the -s (summarize) option. You can too combine this with other options, such as the -h (human-readable) option:

du -h -s

Here, we'll employ it with the --apparent-size option:

du --credible-size -s

Displaying Modification Times

To run across the creation or last modification time and date, use the --time pick:

du --time -d 2

Strange Results?

If you see strange results from du , specially when y'all cantankerous-reference sizes to the output from other commands, it'southward usually due to the different block sizes to which different commands tin be set or those to which they default. It could also be due to the differences betwixt real file sizes and the disk space required to store them.

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

How To Check The Size Of A File In Linux,

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

Posted by: saunderscriver.blogspot.com

0 Response to "How To Check The Size Of A File In Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel