Linux

How to find and delete large files. Clear space in Linux Servers

One of the most important tasks of managing your Linux Server is keeping up with resource usage. If you run out of space in your Linux Server, services like Apache, MySQL and sometimes SSH will cease functioning. Here, we look at different ways to determine what is taking up space in Linux servers and how to clear space and delete files in Linux. Some very useful programs can automate this task so, we will install one called NCDU and also some commands that can help track down files that are using a lot of space in your server.

Install NCDU to Find and Delete Large Files In Linux

NCDU is a command line program that indexes the files on a Linux server and displays them in an ordered list. NCDU allows you to see where the file is and how much space it’s taking up. It also includes the option to delete the file.

We’ve created a Linux server and saved two 1GB files in random locations. Here’s the usage. In the next section, we will install NCDU on this server. NCDU should index the files on the server and point out where we stored the two 1GB files.

To install NCDU issue the below commands. NCDU can be installed on any Linux version.

CentOS 8 / RockyLinux 8/9 AlmaLinux 8/9

dnf install install epel-release -y
dnf update
dnf install ncdu -y

Ubuntu / Debian

apt install ncdu

Now NCDU is installed, issue the NCDU command. The command below will index the files in the / folder and produce a list.

ncdu /

By using the UP and DOWN keys together with the RIGHT and LEFT you can browse through the directories until you find the file that is taking up space.

clear space linux

Here we can see our 1GB file which was downloaded to the /usr/download folder. We can use NCDU to delete this file. Select the file using the arrows on your keyboard and press D to delete the file.

find and delete files linux

To exit NCDU press the Q button. NCDU is an effective Linux utility to clear space on Linux servers. But there are also some good Linux commands that will display usage in Linux servers.

Linux Commands To Display Space Usage

A simple command to display disk usage in Linux NVMe servers is the du command. You can use the du command followed by a directory and then the grep command to list the results. An example command below searches the / directory and then displays the results in GB.

du -h / | grep '[0-9\.]\+G'

By using this command we can see the /var and /usr/lib files contain 1.3GB and 1.2GB respectively.

1.3G    /var
1.2G    /usr/lib
0       /usr/share/locale/zh_CN.GB2312/LC_MESSAGES
0       /usr/share/locale/zh_CN.GB2312
1.8G    /usr
3.5G    /

Using this information you can dig further into the folder tree to find and delete files in Linux servers. Whilst commands are not as fast as NCDU they are just as effective.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button