Linux view disk information command description _linux shell_ Script home

Description of commands for viewing disk information in Linux

Updated: April 01, 2024 08:59:45 Author: Tang Qingfeng
This article mainly introduces the Linux view disk information command,df and du command,df The du command is used to check the amount of used and available disk space on the file system. The du command is used to check the amount of disk space used by directories or files on the system. There are code examples in this paper for your reference

I. df

1. Introduction

df, short for disk free, has been a part of UNIX and Unix-like operating systems since their early days. It is designed as a tool to monitor the amount of disk space used and available on your system.

The df command is used to check the amount of used and available disk space on the file system. This is especially important when managing server systems, where running out of disk space can cause serious problems.

If no file name is specified, the space available on all currently mounted file systems is displayed. Spaces are displayed in 1K blocks by default, unless the environment variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.

The amount displayed is in bytes by default.

principle

Command to retrieve disk information from /proc/mounts or /etc/mtab.

2. Common options

  • -a, --all: includes pseudo-(pseudo-file systems with 0 blocks (not directly bound to a physical device), duplicate, and inaccessible file systems.
df -a
  • -h, --human-readable: Print in a human-readable manner, such as: KB, MB, GB, print size in 1024 units.
df -h

Filesystem    Size  Used Avail Use% Mounted on
devtmpfs       863M     0  863M   0% /dev
tmpfs          893M  168K  893M   1% /dev/shm
tmpfs          893M  9.5M  883M   2% /run
tmpfs          893M     0  893M   0% /sys/fs/cgroup
/dev/map[...]   17G  6.9G   11G  41% /
/dev/sda1     1014M  255M  760M  26% /boot
tmpfs          179M  120K  179M   1% /run/user/1000
  • -H, --si: Similar to -h, the print size is in 1000 units.
df -H
  • -k: Displays all mounted file system information and usage in 1024-byte blocks, with kilobytes (kb) as the size.
df -k
  • -m: Displays size in megabytes
df -m
  • -i, --inodes: Lists inode information instead of block usage.

Inodes are data structures that store file and directory information, such as ownership, permissions, and timestamps.

df -i

Filesystem    Inodes IUsed IFree IUse% Mounted on
devtmpfs        216K   393  216K    1% /dev
tmpfs           224K     3  224K    1% /dev/shm
tmpfs           224K   857  223K    1% /run
tmpfs           224K    17  224K    1% /sys/fs/cgroup
/dev/map[...]   8.5M  168K  8.4M    2% /
/dev/sda1       512K   310  512K    1% /boot
tmpfs           224K    74  224K    1% /run/user/1000

Node information field description:

Filesystem: Filesystem name Inodes: the total number of inodes on the file system IUsed: the number of unused inodes IFree: the number of unused inodes IUse% : The percentage of used inodes Mounted on: the directory where the file system is mounted
  • -l, --local: Limits output to the local file system.
df -l
  • --output[=FIELD_LIST]: Customize the output field.
df -h --output=source,avail,pcent,target Filesystem Avail Use% Mounted on devtmpfs 863M 0% /dev tmpfs 893M 1% /dev/shm tmpfs 883M 2% /run tmpfs 893M 0% /sys/fs/cgroup /dev/map[...]  11G 41% / /dev/sda1 760M 26% /boot tmpfs 179M 1% /run/user/1000
  • -P, --portability: Uses POSIX output format
df -P
  • --total: Delete all items that are not important to the available space and sum the total statistics.
df -h --total

Filesystem     Size  Used Avail Use% Mounted on
devtmpfs       863M     0  863M   0% /dev
tmpfs          893M  168K  893M   1% /dev/shm
tmpfs          893M  9.5M  883M   2% /run
tmpfs          893M     0  893M   0% /sys/fs/cgroup
/dev/map[...]   17G  6.9G   11G  41% /
/dev/sda1     1014M  255M  760M  26% /boot
tmpfs          179M  120K  179M   1% /run/user/1000
total           22G  7.2G   15G  33% -
  • -t, --type=[TYPE]: Lists only information about the specified file system type.
df -t ext4

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/nvme0n1p3 222284728 183666112  27257432  88% /
/dev/sda1      480588496 172832632 283320260  38% /data
  • -T, --print-type: Prints the file system type
df -T

Filesystem     Type     1K-blocks     Used Available Use% Mounted on
/dev/sda1      ext4     102384432 45735432  51335636  47% /
tmpfs          tmpfs      4145120        4   4145116   1% /dev/shm
  • -x, --exclude-type=[TYPE]: Excludes the specified file system type
df -x tmpfs
  • --help: Displays the help information

  • --version: Displays the version information

3. Example commands

  • Common usage
df

Filesystem     1K-blocks      Used Available Use% Mounted on
dev              8172848         0   8172848   0% /dev
run              8218640      1696   8216944   1% /run
/dev/nvme0n1p3 222284728 183057872  27865672  87% /
tmpfs            8218640    150256   8068384   2% /dev/shm
tmpfs            8218640         0   8218640   0% /sys/fs/cgroup
tmpfs            8218640        24   8218616   1% /tmp
/dev/nvme0n1p1    523248    107912    415336  21% /boot
/dev/sda1      480588496 172832632 283320260  38% /data
tmpfs            1643728        40   1643688   1% /run/user/1000

Output field description:

Filesystem: Name of a file system 1k-blocks: size of a file system (in 1K blocks) Used: Used space in 1K blocks Available: available space in 1K blocks Use% : percentage of used space Mounted on: Directory to which the file system is mounted
  • df and grep together print only the total amount of space
df -h --total|grep ^total

total  22G  7.2G  15G  33% -
  • Prints the space usage for the specified mount point
df -h/Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-stream 17G 6.9G 11G 41% / df -h /boot Filesystem Size Used Avail Use% Mounted on /dev/sda1 1014M 255M 760M 26% /boot
  • Specify the file name and view information about the mount point where the file name resides
df -h abc.txt
  • df combined sort is sorted by usage size
df --output=size,target | sort -n -r

Size  Mounted on
98G   /
4.0G  /dev/shm

4. Output the format field

  • source: indicates the source of the file system

  • fstype: indicates the type of the file system

  • itotal: indicates the total number of inodes

  • iused: indicates the number of inodes that have been used

  • iavail: indicates the number of available index nodes

  • ipcent: percentage of used inodes

  • size: total disk space

  • used: indicates the used disk space

  • avail: indicates the available disk space

  • pcent: percentage of used disk space

  • file: Specifies the file name on the command line

  • target: The directory to which the file system is mounted

5.man pages

Ii. du

1. Introduction

du, short for disk usage, has been a part of UNIX and Unix-like systems since the early days. It is designed to provide a summary of disk usage of the directory tree (including its subdirectories).

The du command is used to know the disk space used by directories or files on the system. It's especially handy when trying to identify large files or directories that take up most of your disk space.

2. Common options

  • -0, --null: Ends each output line with NUL instead of line feed.
du -0
  • -a, --all: Displays disk usage for each individual file, not just directories.
du -a
  • -B, --block-size=[SIZE]: Print the specified size format
du --block-size=1M
  • --apparent-sizeThe file size of the print surface, not the disk usage, although the surface file size may be small, but because of the file size increase there may be some internal fragmentation in the file, actually occupy a larger disk.
du --apparent-size
  • -c, --total: Provides a total of disk usage.
du -c

/home/abc/article_submissions/
12K    /home/abc/article_submissions/my_articles
36K    /home/abc/article_submissions/community_content
48K    /home/abc/article_submissions/
48K    total
  • -d, --max-depth=N: Specifies the depth of the recursion
du --max-depth=1
  • -h, --human-readable: Print in human-readable units
du -h

64K  ./test_dir
128K .
  • --inodes: Lists inode usage, not block usage
du --inodes
  • -k: The value is displayed in KB
du -k is equal to: du --block-size=1K
  • -m: Output in MB(megabytes)
du -m is equal to: du --block-size=1M
  • -S, --separate-dirs: does not contain the subdirectory size
du -S 
  • --si: similar to-h, using powers of 1000 instead of 1024
du --si 
  • -s, --summarize: Displays only the total number of each parameter
du -s 
  • --time: Displays the last modification time of all files in a directory or subdirectory of the directory
du --time 
  • --time=[WORD]: Displays the specified time format instead of the default modification time, for example: atime, access, use, ctime, status
du --time=atime 
  • -x, --exclude-from=[FILE] : excludes files matching any pattern in [FILE]

  • --exclude=[PATTERN] : excludes matched files

du -ah --exclude="*.dll" 

PATTERN is a shell pattern (not a regular expression). A pattern? Matches any character, and * matches any string (consisting of zero, one, or more characters). For example :*.o will match any file ending in.o. Therefore, the command du --exclude='*.o' will skip all files and subdirectories ending in.o (including the *.o file itself).

  • -x, --one-file-system: Skips directories on different file systems
du -x 
  • --help: Print help information
du --help 
  • --version: Prints version information
du --version 

3. Example commands

  • -hAccess to specified directory
du -h /home/user/documents 
  • --excludeAccess to specified directory
du -h --exclude='*.txt' /home/user/documents 
  • Used in conjunction with the sort command, sort by file usage
du -h --max-depth=1 | sort -hr

128K    .
64K     ./test_dir
  • Prints the total usage of all files in the current directory
du -sh . 

4.man pages

The above is the Linux view disk information command detailed content, more information about Linux view disk information please pay attention to script home other related articles!

Related article

Latest comments