Disk space and usage in Linux
notes
Linux
Using
du and df commands to collect disk information in Linux
TL;DR
To check the disk space use df (for *disk free`):
$ df -hThis will list mounted drives and list the directory where they are mounted, together with the total, used and available space in human readable form.
To check how much space a given directory and its subdirectories or files take, use du (for disk usage):
du [path_to_directory] -h -d [depth]For example:
$ du . -h -d 2 -a
8,0K ./posts
52K ./notes
4,0K ./README.md
8,0K ./publications.qmd
142M .This will list all (-a) directories and files in the current directory (.), and one level below (-d 2) together with the amount of disk space they take in human readable form (-h).
Breakdown
- As usual with Linux commands, the built-in help is the best source of truth:
du --helpordf --helpto obtain all available options. - In both commands
-his short for human-readable, which for me is a must unless the goal is to pipe the output to some other tool. - In the case of
duanother quite useful options is--timewhich adds the date and time the file or directory was last modified.
For example:
$ du . -h -d 1 -a --time
8,0K 2024-12-26 13:55 ./posts
52K 2024-12-27 12:11 ./notes
4,0K 2024-12-23 12:52 ./README.md
8,0K 2024-12-23 12:52 ./publications.qmd
142M 2024-12-27 12:11 .Resources
ducommand explained in 101 Linux commands e-bookdfcommand explained in 101 Linux commands e-book- Post about
duin redhat.com. - Post about
dfin redhat.com.