What is the Linux command to find and delete unused files?

Background

Disk space management is an essential part of server hosting. Fortunately Linux includes utilities to see which files haven’t been accessed in a while. This file attribute is called atime (access time) and can be used with the find command.

How to find old files

find /home -atime +365

In the example above, all files from the /home directory is searched where the last accessed (atime) is older than 365 days.

This will give an accurate overview of which files haven’t been accessed in XX number of days.

The command to delete those actual files would be:

rm `find /home -atime +365`

The above high risk command may need to be used with -f to avoid the system from asking about every file deletion.

We do not recommend using automated deletions without taking utmost care. There is a chance that application files are deleted causing programs to stop working. Instead use the first command find command without the rm to identify old files, and use a file manager or something like Midnight Commander to manually delete clearly identified files.

Reference

https://how-to.fandom.com/wiki/How_to_find_and_remove_old_unused_files_in_unix

Share this article

Leave a Reply

Your email address will not be published.

Scroll to Top