Linux get drive size

How to find the size of a filesystem?

How can I find out the size of a filesystem in Linux? By that I mean the exact number of bytes that are used from the partition, not just the output of df , as that can differ from the true size when compression or deduplication is used on the filesystem. The size of the partition itself can be printed with:

$ blockdev --getsize64 /dev/sda 

I am looking for something similar for the filesystem. PS: This is for LVM grow/shrink stuff. I am mostly interested in ext2/3/4 and btrfs, but any other filesystem info is appreciated as well.

No, I mean «filesystem». A filesystem size can be different from the partition it is on, this is common when you grow or shrink partitions.

3 Answers 3

If you want filesystem information and not partition/volume information, I think you’ll have to use filesystem-specific tools.

In the case of the extN systems, that would be dumpe2fs . And dumpe2fs doesn’t directly print the size in bytes, as far as I can tell. It does, however, print the block count and the size of blocks, so you can parse the output instead:

$ dumpe2fs -h /dev/sda1 |& awk -F: '/Block count/ /Block size/ END' 29999980544 

In my case, this size is slightly different from the partition size:

$ parted /dev/sda u b p Model: ATA ST500LT012-1DG14 (scsi) Disk /dev/sda: 500107862016B Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17408B 30000000511B 29999983104B ext4 boot, esp 2 30000807936B 453049843711B 423049035776B ext4 5 453049843712B 495999516671B 42949672960B ext4 3 495999516672B 500102862847B 4103346176B linux-swap(v1) 4 500102862848B 500107845119B 4982272B bios_grub 

The partition size is 29999983104 bytes, 2560 bytes more than a multiple of the block size, which is why the size reported by dumpe2fs is less.

Читайте также:  Kali linux интернет wifi

Источник

How to Check Disk Space in Linux

Managing disk space on a Linux server is an important task. For example, package manager applications notify you how much disk space will be required for an installation. For that information to be meaningful, you should know how much space your system has available.

In this tutorial, learn how to use the df command to check disk space in Linux and the du command to display file system disk space usage.

Tutorial on checking available disk space in Linux

  • A Linux-based system
  • A terminal window / command line
  • A user account with sudo or root privileges

Check Linux Disk Space Using df Command

You can check your disk space simply by opening a terminal window and entering the following:

Command to check disk space on Linux.

The df command stands for disk free, and it shows you the amount of space taken up by different drives. By default, df displays values in 1-kilobyte blocks.

Display Usage in Megabytes and Gigabytes

You can display disk usage in a more human-readable format by adding the -h option:

This displays the size in kilobytes (K), megabytes (M), and gigabytes (G).

Command to check disk space in human-readable format of kilobytes, megabytes,and gigabytes

Understanding the Output Format

The df command lists several columns:

Filesystem Size Used Avail Use% Mounted on udev 210M 0 210M 0% /dev tmpfs 49M 1004K 48M 3% /run /dev/sda2 7.9G 4.3G 3.2G 58% /

Your output may have more entries. The columns should be self-explanatory:

  • Filesystem – This is the name of each particular drive. This includes physical hard drives, logical (partitioned) drives, and virtual or temporary drives.
  • Size The size of the filesystem.
  • Used – Amount of space used on each filesystem.
  • Avail – The amount of unused (free) space on the filesystem.
  • Use% – Shows the percent of the disk used.
  • Mounted on – This is the directory where the file system is located. This is also sometimes called a mount point.
Читайте также:  Настройка сетевых папок линукс

The list of filesystems includes your physical hard drive, as well as virtual hard drives:

  • /dev/sda2 – This is your physical hard drive. It may be listed as /sda1, /sda0, or you may even have more than one. /dev stands for device.
  • udev This is a virtual directory for the /dev directory. This is part of the Linux operating system.
  • tmpfs – You may have several of these. These are used by /run and other Linux processes as temporary filesystems for running the operating system. For example, the tmpfs /run/lock is used to create lockfiles. These are the files that prevent multiple users from changing the same file at the same time.

Display a Specific File System

The df command can be used to display a specific file system:

You can also use a backslash:

This displays the usage on your primary hard drive. Use the mount point (in the Mounted on column) to specify the drive you want to check.

Command to display the usage of a specific file system.

Note: The df command only targets a full filesystem. Even if you specify an individual directory, df will read the space of the whole drive.

Display File Systems by Type

To list all file systems by type, use the command:

This lists drives with the ext4 type, in human-readable format.

Command to list drives with the ext4 type with size and available space show

Display Size in 1000 Instead of 1024

You can display disk usage in units of 1000 instead of 1024:

This can address a point of confusion in storage technology. Hard drive manufacturers sell hard drives in sizes based on 1000 bytes = 1 kilobyte.

However, operating systems divide that space up so that 1024 bytes = 1 kilobyte. Because of this, a 1000-gigabyte hard drive ends up with roughly 930 gigabytes of usable storage.

Читайте также:  Enable port forwarding in linux

Note: Check out our overview of the Linux free command used to check total, used, shared, free, and available memory and swap space.

Check Disk Space in Linux With du Command

The du command displays disk usage. This tool can display disk usage for individual directories in Linux, giving you a finer-grained view of your disk usage. Use it to display the amount of space used by your current directory:

Du command for displaying disk usage in Linux.

Like the df command, you can make du human-readable:

It displays a list of the contents of the current directory, and how much space they’re using. You can simplify the display with the -s option:

This shows how much space the current directory uses.

example of how much space is left in a linux directory

To specify the directory or file, check use the following options:

With the second command, you may have noticed a permission denied error message. This means the current user doesn’t have the privileges to access certain directories. Use the sudo command to elevate your privileges:

Note: If you’re working on CentOS Linux, you may need to use the su command to switch to the root user to access protected directories.

You should now understand how to use df and du commands to check disk space on your Linux system. Remember, to display a complete list of options, use either df —help or du —help .

Check out our article on how to use fsck command to run a filesystem check as preventive maintenance or when there is an issue with your system.

Источник

Оцените статью
Adblock
detector