Technical skill

Linux command: du

Find which folder uses space

When a computer says its storage is nearly full, folders with logs, downloads, or other files may be taking up the room. The du command, short for disk usage, adds up the space used by files under a folder, including its subfolders. This total helps you find which folder to inspect first. To see one readable total for the log folder, run:

du -sh /var/log

The -s option prints one total, and -h uses familiar units such as MB or GB. If the total is large, check the folders directly under /var:

sudo du -xhd1 /var

In du -xhd1, -x skips directories on other filesystems, and -d1 limits the display to one level below /var. sudo runs the check with elevated permissions. GNU Coreutils supports this option combination. Use elevated access only when needed. Permission errors mean the scan may be incomplete. Rerun the command inside the largest result to narrow down which program created the files before removing or rotating them.

Compare folder totals with the filesystem

df gives the total space and file-record capacity of a filesystem, the part of storage that holds a directory tree. du walks the visible paths inside that tree, while df -h /var reports how much space the whole filesystem has used or left. The numbers can differ. A deleted file that a running program still has open takes space that du cannot find by name. When installed, lsof +L1 can help locate such files. Close or restart the process only after checking what it does with the file.

Check file records, called inodes, with df -i /var if the filesystem has free space but cannot create more files. An inode is a record of a file's metadata and location. A filesystem can run out of inodes before it runs out of storage blocks.

Read the total correctly

By default, du adds the disk blocks allocated to files. That can differ from the file length shown by a listing. du --apparent-size reports logical file length instead. A sparse file can have a large logical length while using few blocks; compression and copy-on-write storage also affect the relationship. du -ah shows entries for individual files.

GNU du does not follow symbolic links by default. It normally counts a file with several hard links only once during a walk, and the starting-path order can affect which link it counts. It includes filesystems mounted below the starting path unless -x is used. If files change during a scan, the result reflects a directory tree that may be changing. GNU options such as -d are not universal across Unix systems. Before deleting data, check who owns it, its retention rules, and whether a backup is needed; a space scan cannot tell you whether a file is safe to remove.

Interactive exercises

Materials

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful