How much space do you have free on your Linux drive?
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, we’ll learn how to use the df command to check disk space on a Linux system and the du command to display file system disk space usage.
Prerequisites
- A Linux-based system
- A terminal window / command line
- A user account with sudo or root privileges
Overview of Linux commands available to check disk space:
df command
– Shows the amount of disk space used and available on Linux file systems.du command
– Display the amount of disk space used by the specified files and for each subdirectory.btrfs fi df /device/
– Show disk space usage information for a btrfs based mount point/file system.
Linux check disk space with df command
- Open the terminal and type the following command to check disk space.
- The basic syntax for df is:
df [options] [devices]
Type: df
df -H
Sample outputs:
df command in action, The items in square brackets are optional. You can simply type the df command (i.e. no arguments), to see a table that lists for each device name on the system.
See information about specific filesystem
You can give a device or mount point as an argument, and df report data only for the filesystem physically residing on that device. For example, the following command provides information only for the partition /dev/sda:
$ df /dev/sda
$ df -h /dev/sdc1
$ df /data/
Sample outputs:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda 2930266584 69405248 2859579472 3% /data
Understanding df command output
The valid fields are as follows:
Display name | Valid field name (for --output option) | Description |
---|---|---|
Filesystem | source | The source of the mount point, usually a device. |
1K-blocks | size | Total number of blocks. |
Used | used | Number of used blocks. |
Available | avail | Number of available blocks. |
Use% | pcent | Percentage of USED divided by SIZE. |
Mounted on | target | The mount point. |
You can pass the output format defined by ‘valid field name’ as follows:
$ df --output=field1,field2,...
$ df --output=source,used,avail /data/
Sample outputs:
Filesystem Used Avail
/dev/md0 5.4G 115G
udev 0 11M
tmpfs 6.2M 414M
tmpfs 4.1k 1.1G
tmpfs 4.1k 5.3M
tmpfs 0 1.1G
/dev/md2 818G 688G
tmpfs 0 210M
tmpfs 0 210M
/dev/mapper/cryptvg-mybackup 77G 526G
You can print all available fields, enter:
$ df --
Sample outputs:
Filesystem Type Inodes IUsed IFree IUse% 1K-blocks Used Avail Use% File Mounted on
udev devtmpfs 379248 333 378915 1% 10240 0 10240 0% - /dev
tmpfs tmpfs 381554 498 381056 1% 610488 9704 600784 2% - /run
/dev/sdc1 ext3 956592 224532 732060 24% 14932444 7836056 6331204 56% - /
tmpfs tmpfs 381554 1 381553 1% 1526216 0 1526216 0% - /dev/shm
tmpfs tmpfs 381554 4 381550 1% 5120 0 5120 0% - /run/lock
tmpfs tmpfs 381554 14 381540 1% 1526216 0 1526216 0% - /sys/fs/cgroup
/dev/sda btrfs 0 0 0 - 2930266584 69405248 2859579472 3% - /data
tmpfs tmpfs 381554 4 381550 1% 305244 0 305244 0% - /run/user/0
Express df output in human readable form
Pass the -h option to see output in human readable format. You will device size in gigabytes or terabytes or megabytes:
$ df -h ### Human format
$ df -m ### Show output size in one-megabyte
$ df -k ### Show output size in one-kilobyte blocks (default)
Display output using inode usage instead of block usage
An inode is a data structure on a Linux file system that stores all information about file. To list inode information, enter:
$ df -i
$ df -i -h
Sample outputs:
Filesystem Inodes IUsed IFree IUse% Mounted on
udev 371K 333 371K 1% /dev
tmpfs 373K 498 373K 1% /run
/dev/sdc1 935K 220K 715K 24% /
tmpfs 373K 1 373K 1% /dev/shm
tmpfs 373K 4 373K 1% /run/lock
tmpfs 373K 14 373K 1% /sys/fs/cgroup
/dev/sda 0 0 0 - /data
tmpfs 373K 4 373K 1% /run/user/0
Find out the type of each file system displayed
Pass the -T option to display the type of each filesystems listed such as ext4, btrfs, ext2, nfs4, fuse, cgroup, cputset, and more:
$ df -T
$ df -T -h
$ df -T -h /data/
Sample outputs:
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda btrfs 2.8T 67G 2.7T 3% /data
Limit listing to file systems of given type
The syntax is:
$ df -t ext3 #Only see ext3 file system
$ df -t ext4 #Only see ext4 file system
$ df -t btrfs #Only see btrfs file system
Exclude given file system type
To list all but exclude ext2 filesystem pass the -x TYPE option, enter:
$ df -x ext2
Show all file system
Pass the -a or --all option to the df command to include in its output filesystems that have a size of zero blocks, run:
$ df -a
Filesystem 1K-blocks Used Available Use% Mounted on
sysfs 0 0 0 - /sys
proc 0 0 0 - /proc
udev 10240 0 10240 0% /dev
devpts 0 0 0 - /dev/pts
tmpfs 610488 9708 600780 2% /run
/dev/sdc1 14932444 7836084 6331176 56% /
securityfs 0 0 0 - /sys/kernel/security
tmpfs 1526216 0 1526216 0% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 1526216 0 1526216 0% /sys/fs/cgroup
cgroup 0 0 0 - /sys/fs/cgroup/systemd
pstore 0 0 0 - /sys/fs/pstore
cgroup 0 0 0 - /sys/fs/cgroup/cpuset
cgroup 0 0 0 - /sys/fs/cgroup/cpu,cpuacct
cgroup 0 0 0 - /sys/fs/cgroup/blkio
cgroup 0 0 0 - /sys/fs/cgroup/memory
cgroup 0 0 0 - /sys/fs/cgroup/devices
cgroup 0 0 0 - /sys/fs/cgroup/freezer
cgroup 0 0 0 - /sys/fs/cgroup/net_cls,net_prio
cgroup 0 0 0 - /sys/fs/cgroup/perf_event
systemd-1 - - - - /proc/sys/fs/binfmt_misc
fusectl 0 0 0 - /sys/fs/fuse/connections
debugfs 0 0 0 - /sys/kernel/debug
mqueue 0 0 0 - /dev/mqueue
hugetlbfs 0 0 0 - /dev/hugepages
/dev/sda 2930266584 69405248 2859579472 3% /data
rpc_pipefs 0 0 0 - /run/rpc_pipefs
tmpfs 305244 0 305244 0% /run/user/0
binfmt_misc 0 0 0 - /proc/sys/fs/binfmt_misc
These file systems omitted by default.
Getting more help about the df command
Pass the --help option see a brief help message:
$ df --help
Or read its man page by typing the following command:
$ man df
Linux check disk space with the du command
The du command is very useful to track down disk space hogs. It is useful to find out the names of directories and files that consume large amounts of space on a disk. The basic syntax is:
du
du /path/do/dir
du [options] [directories and/or files]
To see the names and space consumption of each of the directories including all subdirectories in the directory tree, enter:
$ du
Sample outputs:
16 ./.aptitude
12 ./.ssh
56 ./apcupsd
8 ./.squidview
4 ./kernel.build
12 ./.elinks
8 ./.vim
8 ./.config/htop
12 ./.config
648 .
The first column is expressed in kilobytes (file size) and the second column is the filename or directory name.
See du output in human readable format
Pass the -h option to display size in K (kilobytes), M (megabytes), G (gigabytes) instead of the default kilobytes:
$ du -h
Sample outputs:
16K ./.aptitude
12K ./.ssh
56K ./apcupsd
8.0K ./.squidview
4.0K ./kernel.build
12K ./.elinks
8.0K ./.vim
8.0K ./.config/htop
12K ./.config
648K .
Finding information about any directory trees or files
To find out /etc/ directory space usage, enter:
# du /etc/
# du -h /etc/
The following will report the sizes of the thee files named hdparm, iptunnel and ifconfig that are located in the /sbin directory:
$ du /sbin/hdparm /sbin/iptunnel /sbin/ifconfig
$ du -h /sbin/hdparm /sbin/iptunnel /sbin/ifconfig
Sample outputs:
112K /sbin/hdparm
24K /sbin/iptunnel
72K /sbin/ifconfig
How do I summarize disk usage for given directory name?
Pass the -s option to the du command. In this example, ask du command to report only the total disk space occupied by a directory tree and to suppress subdirectories:
# du -s /etc/
# du -sh /etc/
Sample outputs:
6.3M /etc/
Pass the -a (all) option to see all files, not just directories:
# du -a /etc/
# du -a -h /etc/
Sample outputs:
4.0K /etc/w3m/config
4.0K /etc/w3m/mailcap
12K /etc/w3m
4.0K /etc/ConsoleKit/run-seat.d
4.0K /etc/ConsoleKit/seats.d/00-primary.seat
8.0K /etc/ConsoleKit/seats.d
4.0K /etc/ConsoleKit/run-session.d
20K /etc/ConsoleKit
...
....
..
...
4.0K /etc/ssh/ssh_host_rsa_key
4.0K /etc/ssh/ssh_host_rsa_key.pub
4.0K /etc/ssh/ssh_host_dsa_key
244K /etc/ssh/moduli
4.0K /etc/ssh/sshd_config
272K /etc/ssh
4.0K /etc/python/debian_config
8.0K /etc/python
0 /etc/.pwd.lock
4.0K /etc/ldap/ldap.conf
8.0K /etc/ldap
6.3M /etc/
You can also use star ( * ) wildcard, which will match any character. For example, to see the size of each png file in the current directory, enter:
$ du -ch *.png
52K CIQTK4FUAAAbjDw.png-large.png
68K CX23RezWEAA0QY8.png-large.png
228K CY32cShWkAAaNLD.png-large.png
12K CYaQ3JqU0AA-amA.png-large.png
136K CYywxDfU0AAP2py.png
172K CZBoXO1UsAAw3zR.png-large.png
384K Screen Shot 2016-01-19 at 5.49.21 PM.png
324K TkamEew.png
8.0K VQx6mbH.png
64K fH7rtXE.png
52K ipv6-20-1-640x377.png
392K unrseYB.png
1.8M total
The -c option tells du to display grand total.
Putting it all together
To list top 10 directories eating disk space in /etc/, enter:
# du -a /etc/ | sort -n -r | head -n 10
Sample outputs:
8128 /etc/
928 /etc/ssl
904 /etc/ssl/certs
656 /etc/apache2
544 /etc/apache2/mods-available
484 /etc/init.d
396 /etc/php5
336 /etc/sane.d
308 /etc/X11
268 /etc/ssl/certs/ca-certificates.crt
For more information on the du command, type:
$ man du
$ du --help
Conclusion
Here is quick summary for Linux check disk space commands. Use the du command when you need to estimate file space usage. To report Linux file system disk space usage use the df command. Fore more info see GNU coreutils page here. You can read man pages locally too. Just try the following help command or man command:
man du
man df
man btrfs
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
.
Thank you for your time and I hope you have learned quite a bit!
If you’ve enjoyed this class, do make sure to share this on Facebook, Twitter, LinkedIn or anywhere! It helps us grow and allows for more people to enjoy this as well!
Leave a Reply