Open In App

How to Delete All Files Before a Certain Date in Linux

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Linux, an advanced and open-source operating system, has several file-management commands and utilities. Deleting files in Linux is an essential skill for all users, whether beginner or experienced. In this article, we will learn how to delete all files before a certain date in Linux.

In Linux, you can use the find command with the -exec argument to delete all files created before a specific date.

We will use terminal commands to do all forms of deletion operations, so open your terminal and run all commands carefully to avoid data loss.

How to Delete All Files Before a Certain Date in Linux

Let us find out how to delete all files before a certain date in Linux:

Example 1: Delete all files older than a year:

In this example, we will use the find command to delete all files modified older than 365 days ( 1 year ) in the specified directory, find command helps us to find all files or files with specific extensions within a specified directory. Below is the command to delete all files modified older than 1 year:

find /path/to/your/directory -type f -mtime +365 -exec rm {} \;

here:

  • find – This command looks for files and directories within a given directory structure. It provides numerous of options for setting search criteria.
  • /path/to/your/directory – Replace this with the full path to the location where you wish to look for files. This is where the find command will begin.
  • -type f – This option instructs the search to look exclusively for files (not directories). The -type f filter restricts the search to normal files only.
  • -mtime +365 – This filter is time-based. It is an abbreviation for modification time, and it chooses files based on when they were last modified. It chooses files that were changed more than 365 days ago in this situation.
  • -exec rm {} \; – This section of the command is used to run a command on the files found by the find command. In this situation, it’s using the rm (remove) command to delete the files. The {} is a placeholder for the file name, and the \; indicates the end of the -exec option.

In this command you can use the -atime instead of -mtime flag to delete all files that have not been accessed in last 365 days.

find /path/to/your/directory -type f -atime +365 -exec rm {} \;
delete1

delete all fies inside gfg directory modified before 1 year

Example 2: Delete files modified within a specific date range:

In this example, we will delete all files modified between a period of time. Let’s say If we want to delete all files modified between 1 january 2023 and 31 decemeber 2023 then we can use the following command:

find /path/to/your/directory -type f -newermt "2023-01-01" ! -newermt "2023-12-31" -exec rm {} \;

here:

  • -newermt “2023-01-01” – This filter selects files modified after 1 january 2023.
  • ! -newermt “2023-12-31” – This filter selects files that are not newer than December 31, 2023.
delete2

Delete all files inside gfg directory modified between 1 jan 2023 and 31 dec 2023

Example 3: Delete all files with specific extension:

In this example, we will select all files with .txt extension and delete the files modified before 365 day or 1 year. here is the command to delete all text files modified before 1 year.

find /path/to/your/directory -type f -name "*.txt" -mtime +365 -exec rm {} \;
delete3

Delete all text files inside gfg directory modified before 1 year.

Notes:

  • Before executing any deletion commands, double-check the criteria and directory path to avoid unintentional data loss.
  • Always back up important data before performing file deletions to prevent irreversible consequences.
  • Use the exec echo {} \; option for testing before applying exec rm {} \; to visualize which files will be affected.
  • Ensure you have the necessary permissions to delete files in the specified directories.

Conclusion:

Using the find command in conjunction with these approaches, you can selectively delete files depending on numerous criteria, ensuring a well-organized and optimized file system.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads