delete unused files in linux

How to Find & Remove Unused Files in Linux

Often you may need to find and remove unused files from your Linux system. In fact, this is a common requirement for system administrators who want to free up disk space. In this article, we will look at how to find and remove unused files in Linux.


How to Find & Remove Unused Files in Linux

Here are the steps to find & remove unused files in Linux.


1. Command to Find Unused Files

You can easily find unused files using find command with -atime option. Here is the command to find all files that were accessed more than 365 days ago.

find /home -atime +365

If you want to find files that were accessed more than 180 days ago, just change 365 above to 180. Also if you want to find files in a specific folder other than /home, then replace it in the above command, with the folder location of your choice.

The above command will return list of file paths to files that were accessed more than 365 days ago.

You can remove these files by passing its output to rm command as shown below.

$ rm `find /home -atime +365`

The above command may ask for confirmation for some of the files. If you want to ignore these prompts, use -f option with rm command.

$ rm -f `find /home -atime +365`


2. Automate File Deletion

This step is optional. If you want to regularly, delete unused files on your system, then you can create a cronjob to automatically run the above commands at regular intervals.

Open crontab with the following command.

$ crontab -e

Add the following line to run the command every day at 10.a.m.

0 10 * * * rm `find /home -atime +365` >/dev/null 2>&1

Save and close the file. You can modify the above command as per your requirement, or create your own crontab command using third-party crontab generators.

That’s it. In this article, we have looked at how to find and delete unused files in Linux. You can use these commands for all Linux distributions since find and rm commands are available in most of them.

However, please be careful before your automate file deletion, as it may delete important files without your notice, and cause problems with working of some of the applications & processes on your system. Nevertheless, it is advisable to run the find command mentioned above separately, review the output of find command, and then use rm command, once you are sure you want to delete those files.

Also read:

How to Sort Files by Size in Linux
How to Combine Multiple PDF Files into Single File
Python Script to Run SQL Query
Su vs Sudo in Linux
How to Parse Command Line Arguments in Bash

Leave a Reply

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