Saturday, 24 November 2018

df Command in Linux with examples

There might come a situation while using LINUX when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the file system containing each file name argument.

Linux Tutorial and Material, Linux Guides, Linux Certification, Linux Study Material

◈ If no file name is passed as an argument with df command then it shows the space available on all currently mounted file systems
◈ . This is something you might wanna know cause df command is not able to show the space available on unmounted file systems and the reason for this is that for doing this on some systems requires very deep knowledge of file system structures.
◈ By default, df shows the disk space in 1 K blocks.
◈ df displays the values in the units of first available SIZE from –block-size (which is an option) and from the DF_BLOCK_SIZE, BLOCKSIZE AND BLOCK_SIZE environment variables.
◈ By default, units are set to 1024 bytes or 512 bytes(if POSIXLY_CORRECT is set) . Here, SIZE is an integer and optional unit and units are K, M, G, T, P, E, Z, Y (as K in kilo) .

df Syntax :


df [OPTION]...[FILE]...
OPTION : to the options compatible with df command
FILE : specific filename in case you want to know
the disk space usage of a particular file system only.

Using df command


Suppose you have a file named kt.txt and you want to know the used disk space on the file system that contains this file then you can use df in this case as:

// using df for a specific file //

$df kt.txt
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/the2        1957124      1512   1955612   1% /snap/core

/* the df only showed the disk usage
details of the file system that
contains file kt.txt */

Now, what if you don’t give any filename t df, in that case df displays the disk usage information for all mounted file systems as shown below:

//using df without any filename //

$df
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user
/dev/the2        1957124      1512   1955612   1% /snap/core

/* in this case df displayed
the disk usage details of all
mounted file systems */

Options for df command


◈ -a,- -all : It includes all the dummy files also in the output which are actually having zero block sizes.
◈ -B,- -block-size=S : This is the option we were talking in the above para which is used to scale sizes by SIZE like -BM prints sizes in units of 1,048,576 bytes.
◈ – -total : It is used to display the grand total for size.
◈ -h,- -human-readable : It print sizes in human readable format.
◈ -H,- -si : This option is same as -h but it use powers of 1000 instead of 1024.
◈ -i,- -inodes : This option is used when you want to display the inode information instead of block usage.
◈ -k : Its use is like –block-size-1k.
◈ -l,- -local : This will display the disk usage of only local file systems.
◈ -P,- -portability : It uses the POSIX output format.
◈ -t,- -type=TYPE : It will only show the output of file systems having type TYPE.
◈ -T,- -print-type : This option is used to print file system type shown in the output.
◈ -x,- -exclude-type=TYPE : It will exclude all the file systems having type TYPE from the output.
◈ -v : Ignored, included for compatibility reasons.
◈ – -no-sync : This is the default setting i.e not to invoke sync before getting usage info.
◈ – -sync : It invokes a sync before getting usage info.
◈ – -help : It displays a help message and exit.
◈ – -version : It displays version information and exit.

Examples of using df with options


1. Using -a : If there is a need to display all file systems along with those which has zero block sizes then use -a option with df.

//using df with -a//

$df -a
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user
/dev/sda3      174766076 164417964  10348112  95% /host
systemd                0         0         0    - /sys/fs/cgroup

/* in this case
systemd file system
having zero block
size is also displayed */

2. Using -h : This is used to make df command display the output in human-readable format.

//using -h with df//

$df -h kt.txt
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/the2           1.9G      1.5M      1.9G   1% /snap/core

/*this output is
easily understandable by
the user and all
cause of -h option */

In the above, G and M represents Gigabytes and Megabytes respectively. You can use -h with df only to produce the output in readable format for all the mounted file systems rather than just of the file system containing kt.txt file.

3. Using -k : This displays the file system information and usage in 1 K blocks.

//using -k with df//

$df -k kt.txt
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/the2        1957124      1512   1955612   1% /snap/core

/* no change cause the
output was initially
shown in the usage
of 1K bytes only */

4. Using – -total : This option is used to produce total for a size, used and available columns in the output.

//using --total with df//

$df --total
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user
/dev/the2        1957124      1512   1955612   1% /snap/core
total           21923492  15251540   5712260  92% -                     

/* the total row
is added in the
output */

5. Using -T : With the help of this option, you will be able to see the corresponding type of the file system as shown.

/using -T with df//

$df -T kt.txt
Filesystem     Type     1K-blocks      Used Available Use% Mounted on
/dev/the2      squashfs   1957124      1512   1955612   1% /snap/core

/* you can use
-T with df only
to display type of
all the mounted
file systems */

6. Using -t : This is used when you wish the disk usage information of the file systems having a particular type only.

//using -t with df//

$df -t squashfs
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
/dev/the2        1957124      1512   1955612   1% /snap/core

/*so the df
displayed only the
info of the file
systems having type
squashfs */

7. Using -x : Now, you can also tell df to display the disk usage info of all the file systems except those having a particular type with the help of -x option.

//using -x with df//

$df -x squashfs
Filesystem     1K-blocks      Used Available Use% Mounted on
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user

/* in this case info of
/dev/the2 and /dev/loop0
file systems aren't
displayed cause they are
of type squashfs */

8. Using -i : This option is used to display inode information in the output.

//using -i with df//

$df -i kt.txt
Filesystem     Inodes  IUsed    IFree Iuse% Mounted on
/dev/the2      489281     48   489233    1% /snap/core

/*showing inode info
of file system
having file kt.txt */

When -i option is used then the second, third, and fourth columns displays inode-related figures instead of disk related.

9. Using – -sync : By default, the df command produces output with – -no-sync option which will not perform the sync system call prior to reprting usage information. Now we can use – -sync option which will force a sync resulting in the output being fully up to date.

//using --sync option//

$df --sync
Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user
/dev/the2        1957124      1512   1955612   1% /snap/core

/* in this case
no change in the output
is observed cause it is
possible that there is no
update info present to
be reflected */

10. Using -l : When we run df command then by default it shows any externally mounted file systems which include those from external NFS or Samba servers. We can hide the info of these external file systems from output with -l option syntax of which is shown below.

$df -l

So, this is all about df command.

Related Posts

0 comments:

Post a Comment