[Solved] Linux machine run out of disk space. Steps to analyze, understand and fix the issue. Probably you are developer, with buffy laptop, and one day, one of machine-intensive tasks stops with critical error saying that there is no space on your disk.
Why developer?¶
Linux, itself is self aware. For instance logs are rotated, compressed, and removed after some time. Maybe you are running application that has all of that, but for the server? Or you are developing a lot of content - like films, music, with intermediate versions. When you are concentrated on day to day work - it is easy to overlook things like that. For instance how big is your local copy of git? Besides all of that I'm a developer, and it happens to me from time to time.
Can it go away?¶
When you restart modern Linux, then /tmp directory is wiped out. So, when you are doing something, what uses disk space extensively, but also there are a lot of temporal data stored in /tmp that can help. Don't expect it to be long term solution.
How to start?¶
Say you don't have that problem now, but you want to train. You can use keys Ctrl+Alt+function to switch between view ports. No worries - switch back to your favorite windows manager is possible. For instance, when I press Ctrl+Alt+F3, my Ubuntu 18.04 shows login prompt. To get back, I need to use Ctrl+Alt+F2. Go there and login.
If you are have the problem, like having only black screen with login prompt... Login, install ncdu - sudo apt install ncdu, go to /var, /home, and /opt directories, and run the tool. Go deep to find out what eats your space, then jump to section how to remove things.
If you wish to learn something please check this list of tools:
- man - manual - command line help system. You can read more about every tool presented here - for instance man cd - shows information about - cd tool.
- cd - change directory - when logged in, you are in your home directory, going to anything else requires directory change...
- ls - list directory - there are some switches like: - -a - show all files, so also hidden files. For Unix their name starts with a dot, - -A - to skip ., and .., - -l - long - instead showing just name, it shows permissions, size - -h - human - do see human-readable sizes, use -lh, - -t - order result by time.
- less - if results are too big to fit the sceen, you can use it to scroll - arrows, or search - /, to quit - press q. If you need more shortcuts - they are the same as in vim - which is command line text editor. So if you want to see all files in your documents directory - try: ls -Al ~/Documents | less That pipe sign (over enter) takes result of one command and passes it to next one.
- du - disk usage - works with files, and directories. For directories it goes recursively - takes time, but also gives good picture. To limit output use --max-depth. Say you want to know which directory, from your home directory is biggest - du -h --max-depth=1 ~ - you know -h from ls, --max-depth=1 presents only ~, and its subdirectories. ~ is special reference to your home directory.
- df - disk free - shows list of devices you have, with some useful information - last column is mount point - directory under which this device is seen by the system. You can use well known -h to see sizes in different format.
- sudo - switch user, and do - by default asks for your password, and calls command as root / admin.
Go and play. All that tools are read only. Scan your home directory, find files that are not needed. Other directories, like: /var, /opt should be also examined.
Usually I'm finding copy of project directory, or old data dumps, logs that I was searching... Sometimes old program or old version of the program in /opt - since that is the place I'm installing them manually.
Let's delete something¶
There are things you can delete without any fear. For instance: sudo apt clean - removes all installation caches, and leftovers.
To find old, compressed logs use: sudo find /var/log -iname '*.gz'. Not all directories can be seen by normal user - that is why sudo. To remove them - call: sudo find /var/log -iname '*.gz' -delete. That is temporal solution, so let's go to some permanent solutions.
Delete means removal, so be sure you know what you are deleting.
To remove use command rm. To be sure you can confirm every delete, use -i switch. To remove whole directory use -r - to make the call recursive. For instance rm -r ~/workspece/copy_of_the_project will remove whole directory with copy of old project. If files are marked read-only use -f - force switch. Be warned. If you put space between workspace and copy_of_the_project instead of / it will remove whole workspace... and you will have much more free space.
How to prevent¶
Do small cleaning from time to time. At the end of project, at start of new one. Use Disk Space Analyzer - default tool installed with Ubuntu, or other tools to have visual report. Do not keep old projects on your local drive if that is not needed. Remove Downloads from time to time. Just be aware.