Edited 1 week ago by ExtremeHow Editorial Team
File ManagementUbuntuDisk SpaceLinuxMaintenanceOperating SystemsCommand LineToolsSystemAdministration
This content is available in 7 different language
Managing disk space is an important task for everyone who uses a computer, and Ubuntu is no exception. Over time, files accumulate, and before you know it, you start running out of space. When this happens, it's common that a few large files are taking up too much of your disk space. In this guide, we'll explore how to find and delete these large files on an Ubuntu system. By the end of this guide, you should be able to manage your disk space efficiently and keep your system running smoothly.
Before jumping into the process of finding and deleting large files, it's important to understand how to check your overall disk usage. Ubuntu comes with several tools to help you do this.
df
(disk free) command is a standard Unix command used to display the amount of available disk space in various file systems.
df -h
-h
flag means "human-readable", which means it will display the size in K,M,G (kilobytes, megabytes, gigabytes), which is easier to read than blocks.
To get more detailed information about file and directory sizes, you can use du
(disk usage) command:
du -sh /path/to/directory
-s
flag gives you a summary of the total disk usage of the specified directory and -h
makes it human-readable. Replace "/path/to/directory" with the path you want to inspect.
Finding large files can be done with a few simple commands using the terminal. Let's discuss a few ways to achieve this:
find
command is powerful and flexible, often used to search for and locate files and directories. It can be used in conjunction with other commands to find large files.
For example, to find files greater than 100 MB in size in the / directory, use:
sudo find / -type f -size +100M
Let us understand this command:
sudo
: This ensures you have the necessary permissions, especially when searching through system directories.find
: The command used to search./
: The path to start the search from; "/" indicates the root directory.-type f
: This specifies that we are searching for files (f means files).-size +100M
: Searches for files larger than 100 megabytes.This command will list all files that meet your criteria, which you can review before deleting them.
du
command can be used to check which directories are taking up the most space on your system. While it doesn't directly list individual files, it can help point you in the right direction.
To find the largest directories in your home directory:
du -h ~ | sort -hr | head -n 10
This command performs the following functions:
du -h ~
: Lists the disk usage of the home directory and its subdirectories in human-readable format.sort -hr
: Sorts the output by human-readable numbers and in reverse order.head -n 10
: Displays the first 10 results, showing the top directories using the most space.For a more user-friendly way to analyze disk usage, ncdu
(NCurses Disk Usage) utility is a great option. It displays disk usage with an interactive user interface within the terminal.
You can install ncdu
with the following command:
sudo apt update
sudo apt install ncdu
Once installed, run ncdu
by typing the following:
ncdu /
This will analyze the root directory. Use the arrow keys to navigate through directories and view disk usage for subdirectories, making it easier to identify space hogs.
Once you identify the files or directories that are taking up too much space, it's time to delete them to regain disk space. However, be careful. Deleting system-critical files can cause your system to stop working.
rm
command is used to delete files and directories. Be careful when using it, as it does not move files to the trash; it deletes them permanently.
To delete a single file:
rm /path/to/largefile
To delete a directory and its contents:
rm -r /path/to/largedirectory
The -r
flag means "recursive", which allows the deletion of a directory and its contents.
You can combine the find
command with delete
action to delete all files larger than a specified size. Here's an example of finding and deleting files larger than 100 MB:
sudo find / -type f -size +100M -exec rm -i {} \;
The important parts of this order:
-exec
: Executes another command; in this case, rm
.rm -i
: Asks for confirmation before deleting each file, to make sure you don't accidentally delete something.{}
: A placeholder for each found file.\;
: indicates the end of -exec
command.It's important to review files before deleting them. Mistakes can happen, and deleting important files can result in data loss or system problems. Consider the following best practices:
Automation can save time, especially if you need to clear disk space regularly. You can create a script to find and delete files based on your criteria. Here's a basic example:
#!/bin/bash
find / -type f -size +100M -exec rm -i {} \;
To use the script:
chmod +x cleanup.sh
../cleanup.sh
.Finding and deleting large files on Ubuntu is a straightforward process, but it requires careful attention to ensure that important data is not lost. Using tools like find
, du
, and ncdu
, you can effectively find out where disk space is being used. Always remember to back up files and confirm deletions before proceeding. With these practices, you can maintain a healthy system and better manage disk space.
This guide covers various techniques and commands to help you find and delete large files on Ubuntu. By following the examples and steps provided, you will be able to effectively manage the storage of your Ubuntu system.
If you find anything wrong with the article content, you can