WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Find and Delete Large Files on Ubuntu

Edited 1 week ago by ExtremeHow Editorial Team

File ManagementUbuntuDisk SpaceLinuxMaintenanceOperating SystemsCommand LineToolsSystemAdministration

How to Find and Delete Large Files on Ubuntu

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.

Understanding disk usage

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.

Using the df command

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.

Using the du command

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

Finding large files can be done with a few simple commands using the terminal. Let's discuss a few ways to achieve this:

Using the find command

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:

This command will list all files that meet your criteria, which you can review before deleting them.

Using the du command for directory analysis

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:

Using ncdu for interactive analysis

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.

Deleting large files

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.

Using the rm command

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.

Using the find command with delete

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:

Please review carefully before deleting

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:

Automating the process

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:

  1. Copy the script into a text file.
  2. Save it with a .sh extension (for example, cleanup.sh).
  3. Make the script executable with chmod +x cleanup.sh.
  4. Run the script using ./cleanup.sh.

Conclusion

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


Comments