Space Is Left on Disk - dejanu/linux GitHub Wiki
Linux Filesystem
From a high-level point of view Linux filesystem is a structured collection of files on a disk drive or partition. One important aspect is that filesystems are of different types: ext2, ext3, ext4 (most widely used), btrfs, jfs, etc. Basically this comes into play as a choice when it comes to partition the disk, a comprehensive tutorial regarding file systems can be found here.
- File system disk space usage:
df [option] [file] - displays the amount of disk space available on the filesystem containing each file name as argument. If no file name is given, the space available on all currently mounted file systems is shown.
"Disk free" command is at its best when a quick view of the filesystems and all mounted disks is needed. It shows the disk size, used space, available space, usage percentage and what partition the disk is mounted on. The -h
flag is useful to show the output in human-readable format Kilo, Mega, or Giga as appropriate.
$ df -h
Filesystem Size Used Avail Use% Mounted on
- Disk usage
du [option] [file] - estimates disk usage of the set of files, recursively for directories.
An important aspect concerning du is that it only counts the inodes that it can read, so it cannot count files that you as a user cannot access. To clearly see the difference just run du -shc /
and then sudo du -shc /
A practical approach when we run du without sudo is to redirect the standard error (stderr) to /dev/null e.g. : du -shc / 2>/dev/null
- Find
find - search for files in a directory hierarchy based on some user-specified criteria. So if we want to tailor our search we can use find.
A concrete example is if we want to find files bigger than a certain size, let's say 4096 bytes so we're gonna run: find . -type f -size +4096c
Putting it together
When investigating space issues, do not go straight to du
, use df
to find the partition that causes problems, and afterward proceed to use du
.
One important aspect is that du can be depth restricted, more exactly:
* GNU Linux: `du --max-depth 1`
* BSD Linux: `du -d 1`
Caveats
Sometimes disk space is reported differently when running du and df, that's due to the fact that du
is not aware of filesystem metadata versus df
which checks the filesystem disk allocation maps.
One common example is when a file is deleted and it's still open by at least one process. In this case, the entry for such a file is removed from the associated directory which makes the file "inaccessible" thus df
doesn't take it into account and may report a smaller value. More information concerning what happens when we delete or move a file that has open file handles can be found here.