قالب وردپرس درنا توس
Home / Tips and Tricks / Viewing free space and disk usage through the Linux terminal

Viewing free space and disk usage through the Linux terminal



  Bash Shell on Unity Desktop Concept
Fatmawati Achmad Zaenuri / Shutterstock.com

The commands df and du of the bash shell used on Linux, macOS and many other Unix-like operating systems. With these commands, you can easily determine what is consuming your system's memory.

Viewing Total, Available, and Used Space

Bash contains two useful space-related commands. Use df (hard-disk file systems, sometimes referred to as hard-disk space) to determine available and used disk space. Use du (disk usage).

Enter df and press Enter to begin. You will see a lot of output similar to the picture below. If you use df without options, the available and used space is displayed for all mounted file systems. At first glance, it may seem impenetrable, but it's easy to understand.

  df 

  Output of the df command

Each line of the ad consists of six columns.

  • File System: The name of this file system.
  • 1K Blocks: The number of 1K blocks available on this file system.
  • Uses: The number of 1K blocks used This file system.
  • Available: The number of 1K blocks unused in this file system.
  • Usage%: The disk space specified as a percentage in this file system. [19659011] File: The name of the file system, if specified on the command line.
  • Deployed on: The mount point of the file system

You can replace the 1K block values ​​with more useful ones -B (block size) option. To use this option, type df, a space and then -B and a letter from the list of K, M, G, T, P, E, Z, or Y on. These letters represent the kilo, mega, giga, tera, peta, exa, zeta and yotta values ​​of the multiple of 1024.

For example, if you want to display the disk usage numbers in megabytes, use the following command. Note that there is no space between B and M.

  df -BM 

  Output of df command with -BM options

The option -h (human-readable) refers to df to use the most appropriate unit for the size of each file system. In the next issue, note that there are file systems with gigabytes, megabytes and even kilobytes.

  df -h 

  Output of the df command with option -h

If you need to see the information shown in Number of inodes use option -i ( inodes). An inode is a data structure used by Linux file systems to describe files and store metadata about them. On Linux, inodes contain data such as name, date modified, location on disk, etc. for each file and directory. This will not be useful to the majority of people, but system administrators sometimes have to refer to this kind of information.

  df -i 

  Output of the df command with option -i

If not unspecified, df returns information about all mounted file systems. This can lead to an overloaded high-output ad. For example, the entries / dev / loop in the lists are pseudo-file systems that can be used to mount a file as a partition. If you use the new Ubuntu snaps method to install applications, you can purchase many of them. The space available on these values ​​is always 0 because it's not really a file system, so we do not need to see it.

We can direct df to exclude filesystems of a certain type. For this we need to know what type of file system we want to exclude. The option -T (print type) gives us this information. It directs df to include the type of file system in the output.

  df -T 

  Output of df command with option -T

The loop / dev / entries are all squashfs file systems. We can exclude them with the following command:

  df -x squashfs 

  Output of the df command with the options df -x squashfs

This will give you a simpler output. To get a total, we can add the option - total .

  df -x squashfs --total 

  Output of the df command with df -x squashfs --total options [19659006] We can ask df to include only file systems of a particular type by using the Use option -t (type).

  df -t ext4 

  df command output with options df -t ext4

If you want to see the sizes of a set of file systems, you can specify them by name. Drive names in Linux are alphabetical. The first drive is called / dev / sda the second drive is / dev / sdb and so on. Partitions are numbered. / dev / sda1 is the first partition on drive / dev / sda . We say df that information about a particular file system should be returned by passing the name of the file system as a command parameter. Let's look at the first partition of the first hard drive.

  df / dev / sda1 

  Output of df command with options for df / dev / sda1

Note that you can use wildcards in the file system name, where * is for a any set of characters and stands for a single character. To see all the partitions on the first drive, we could use the following:

  df / dev / sda * 

We can ask df to report on a set of named file systems. It requests file system sizes / dev and / run and we want a total number.

  df -h --total / dev / run [19659045]   Output of the df command with options df -h --total / dev / run  

To further customize the display, you can use df specify which columns should be inserted. Use the option - output and specify a comma-separated list of required column names. Make sure there are no spaces in the comma-separated list.

  • Source: The name of the file system.
  • fstype: The type of file system.
  • itotal: The size of the file system in Inodes.
  • iused: The space used on the file system in inodes.
  • iavail: The available disk space on the file system in Inodes.
  • ipcent: Percent of occupied file system disk space in inodes.
  • Size: By default, the size of the file system is 1-KB blocks.
  • Used space: The space used by the file system by default in 1K blocks.
  • occ: The available space in the file system, by default in 1K blocks.
  • pcent: The percentage of disk space used in inodes, by default, in 1K blocks
  • file: The name of the file system, if specified on the command line.
  • target: The mount point for the file system m.

Let's ask df to report the first partition on the first drive with readable numbers and the columns source, fstype, size, used, avail, and pcent:

  df -h / dev / sda1 --output = source, fstype, size, used, avail, pcent 

 Output of the df command with df -h / dev / sda1 --output = source, fstype, size, used, Available pcent options

Long Commands are ideal candidates for converting to aliases. We can create an alias dfc (for df custom ) by typing the following and pressing Enter:

  alias dfc = "df -h / dev / sda1 - -output = source, fstype, size, used, avail, pcent "

 Creating an alias with the command alias dfc =

Typing dfc and pressing Enter same effect as entering long command. To make this alias persistent, add it to the file . bashrc or .bash_aliases .

We searched for ways to refine the output of df so that the displayed information meets your requirements. If you choose the reverse approach and want to return all information to df you may be able to use the option -a (all) and the option - output as shown below , The option -a (all) prompts df to include any file system, and uses the option - output without a comma-separated comma list of columns causes that df includes every column.

  df -a --output 

 Output of the df command with options -a and --output options

Piping The output of df by the command less is a convenient way to check the large amount of output that can be generated this way.

  df -a --output | less 

Finding out what is taking up space

Let's investigate and figure out what's taking up space on this PC. We start with one of our df commands.

  df -h -t ext4 

 Output options df -h -t ext4

There is 78% space on the first partition of the first disk. We can use the command du to indicate which folders contain the most data. If you issue the command du without options, a list of all directories and subdirectories will be displayed under the directory in which the command du was issued. When you do this, the listing from your home folder will be very long.

   du  

 Issue of the command du

The output format is very simple. Each line shows the size and name of a directory. By default, the size is displayed in 1K blocks. To force du to use a different block size, use the option -B (block size). To use this option, enter du a space and then -B and a letter from the list of K, M, G, T, P, E, Z and Y. , as we did above for df . To use 1M blocks, use this command:

  du-BM 

 Issue of the command du with the options of -BM

Such as df du has a human-readable option, -h which uses a series of block sizes according to the size of each directory.

  du -h 

 Output of the du command with the option -h

The option -s (summarized) specifies a sum for each directory, without to display the subdirectories in each directory. The following command requests du to return summary information and human-readable numbers for all directories (*) below the current working directory.

  du -h -s * 

 Output of the command du with the options -h -s *

The Picture folder contains by far the most data. We can ask du to sort the folders in size from largest to smallest.

  You -sm Pictures / * | sort -nr 

 Output of the command du with the option -sm Pictures / *! sort -nr options

RELATED: Lists files and directories by size on Linux

By refining the df and du It's easy to find out how much hard disk space is in use and to determine what takes up that space. You can then make an informed decision as to whether to move data to another storage, to insert another hard drive into your computer, or to delete unnecessary data.

These commands offer many options. We've described the most useful options here, but you can see a full list of options for the df command and for the du command on the Linux man pages.




Source link