Jeffimgcls Hi, I'm Jeff! Resume Linkedin Mail

Linux Drive Space Commands

Our mission critical application is not working! We're down hard, help! How many times have you heard that one and how many times has it been related to not maintaining hard drive space? When your drive is full it's pretty much over for programs that need storage. Don't wait until your drives are full to start cleaning them up. Take a look at these commands that will help you maintain your drives in Linux.

df

df - report file system disk space usage.

df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown.

Use df with the a and h options (df -ah) to see all file systems in a human readable form. Pipe in sort -k 5 (df -ah | sort -k 5) to sort the output by total use % and you can easily see what directories are full.

lsblk

lsblk - lists information about all available or the specified block devices.

The lsblk command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support than it tries to read LABELs, UUIDs and filesystem types from the block device. In this case root permissions are necessary. The command prints all block devices (except RAM disks) in a tree-like format by default.

ncdu

If you prefer an interactive GUI to investigate your drive space usage, try out ncdu. You can install it on debian/ubuntu distributions with apt-get install ncdu.

Delete Files/Directories

If you are not familiar with how to delete files in linux, just use the rm command, sudo rm {filepath}. For directories, add the -r option.

Deleted Files Still Using Drive Space

So what if you go through all of your directories and clean them up removing things, but you still have not freed up enough space? What if df or du still shows large amounts of space being used, but you can't find the reason? You may have an issue with files that have been deleted, but the application still has them open.

sudo lsof +L1 will show you deleted files on your system. Use lsof -p {processID} to see all files open by a specific process ID.

If you find a large file with lsof +L1, then you can either kill/restart the process that has the file open or if that is not an option, truncate the file to clear the space. Use sudo truncate -s 0 {filepath} to clear the contents of the file and recover drive space without stopping any processes.

If you are having a problem with large deleted, but open files on your system, you will want to reach out to the developer of that application to address the problem, chances are it will happen again.

© jeffmdoyle.com, All rights reserved.