From the course: Linux Tips

Managing space on the file system - Linux Tutorial

From the course: Linux Tips

Managing space on the file system

- [Instructor] On any system, managing the available storage space is important. In this episode, I want to show you a few tools to view the available space and to find large files that you might want to keep an eye on if you're running low on disk space. The first tool I want to show you is df and it gives you very high-level view of how much space is available on your file systems. I like to use it with the -h option for human-readable sizes. Here, I can see that I have 87 gigabytes available of the 98 gigabytes available on my root file system. We can use also use the command with the path to show just one file system to narrow down the display a little bit. I'll write df -h and slash for the root file system. And here I can see that isolated from all of the other things. To take a look at storage on the system from the other side so to speak, we can see how much space a certain file or directory takes up. To do that, we'll use the du command followed by some options and the path to look at. Let's take a look at the etc directory to see how much space it takes up. du reports the size for each file and it recurses into directories and that can be useful, but here it's kind of annoying. So we can use a few options to make this a bit easier to read. I'll write du -hd1 /etc. The h option displays the sizes as human readable instead of as blocks. And d sets the depth to display. I set that to 1 so whenever du finds a directory, it goes to maximum depth of one directory. It'll still add up everything inside those directories, but we won't see a line for each deeper item. We'll get the summary of the sizes for each directory that's one level deeper than etc and this can be useful. I'll run that, but if we just want the size of the directory, we can tell du to go zero directories deep with du -hd0 /etc and we can get the same result with du -sh and the path. Here, s stands for summary. So du is good for exploring how much space a file or a directory takes up. And there's some interesting tools based on du like duviz, dutree, and ncdu, but they're not part of the standard complement of tools so I won't go over them here. A third way of looking at space is to find files larger than a given size. Rather than hunting around for big directories and trying to peer down what files contribute to the size of the folders, we can use the find command to look for large files. I'll type find and then a slash for the root of the file system. Then, I'll look for object of type file with a size greater than two megabytes. The +2M here looks for files taking up more than two megabytes. And I could use a minus to look for files smaller than a specified size too. I'd also use capital G for gigabytes and lowercase k for kilobytes. Okay, there's a few files larger than two megabytes on my system. We can add a little more information here by sending the results of the search through ls to get the size of the files. And for that, we'll use the exec option and send the response through ls with -sh for summary and human-readable sizes. After that, I'll use a set of braces to represent the result for each line and then I'll add an escape semicolon. And here on the left, I can see the size of the files that my user has permission to access. We can take this one step further by sorting the results by size, piping them through sort with -h for human size sorting. Once again, I can see the size of each file on the left, but now they're sorted. This kind of thing can be useful for periodic reports that you make into script and run automatically. If you're working with a system where lots of people store files, you can summarize large files now and then, and see if those files represent a problem in terms of space. You can also wrap up the output of df in a script for a report on what percentage of the file system is free or to look for a minimum amount of free space. But remember, if you're using bash to do math, you'll want to use numeric values, not human-readable values so you don't have to parse as much. One tricky place to manage free space is the boot file system. Occasionally, if your boot file system is on a partition that's too small, a long history of updates or a broken-up date can end up filling up the file system causing problems with later updates or with booting the system at all. It's important to keep an eye on the free space on boot and manage it in a way that's appropriate for your system. You can use apt autoremove on Debian and use rpm in the Red Hat ecosystem to remove old kernels that you're not using. And when you're done, run update-grub to remove those old kernels from the boot menu. If you have lots of users storing files on your system, it can be worth looking into quotes as well. Quotas limit users to only taking up a pre-defined amount of space so if you get your math right, you can prevent them from filling up your storage completely. Running out of space is one of those things you don't want to have sneak up on you. When a system is out of space, it can cause all sorts of interesting problems including preventing the system from starting up correctly. So if you manage many systems or just one, keep an eye on your available space.

Contents