The Wayback Machine - https://web.archive.org/web/20240714004546/https://www.geeksforgeeks.org/how-to-delete-all-files-before-a-certain-date-in-linux/
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.



Similar Reads

Tools to Securely Delete Files from Linux
Every time you delete a file from your Linux system using the shift + delete or rm command, it doesn't actually permanently and securely delete the file from the hard disk. When you delete a file with the rm command, the file system just frees up the appropriate inode but the contents of the old file are still in that space until it is overwritten
3 min read
Ways to Permanently and Securely Delete ‘Files and Directories’ in Linux
Linux is an open-source operating system that is loved by tech geeks. It is a versatile operating system that can be used on thousands of different hardware configurations. The most interesting feature of Linux is its command line interface. This feature allows you to perform multiple operations within the operating system with just a few commands.
8 min read
How to Delete Files in Linux?
Linux comes with several tools that can assist us in removing Directories and files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial. Use caution when using the commands below, particularly those that use regular expressi
6 min read
How to automatically delete/remove files older than x days in Linux
Linux operating systems are widely used for web servers and other data-heavy tasks such as penetration testing etc. In these applications, there can be an accumulation of files that are not useful after a certain period. Now Linux, being an operating system used mostly for its automation functionalities, provides many ways to remove files older tha
9 min read
How to Exclude Certain Paths With the find Command in Linux
The find command in Linux is a very useful command line utility that helps us in finding files and directories. In our daily life, there are many tasks in which we require files that may be located deep inside the system, finding them manually would be a tedious task. Thus, the find command comes in handy. We can improve the performance of this com
3 min read
How to restrict a user's login to certain times of the day or week in linux?
In this article, we explore the significance of time-based user login restrictions in Linux as an important security measure. By leveraging Pluggable Authentication Modules (PAM) and time configuration files, administrators can define specific periods when users are allowed or denied access. This detailed article outlines the step-by-step process,
4 min read
How to Display and Set Date and Time in Linux | date Command
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.  Syntax of Date Command in Linux date [OPTION]... [+F
5 min read
Whitespaces Before Any Command in Linux
By default, the history command does not show the commands in which you have inserted a space at the start. So, to overcome this, you need to make changes to .bashrc file. For Example, Suppose you run rm -rf /test* command and you have inserted a blank space at the start of the command. If you then run history command, you will end up with the list
2 min read
Renaming All Files and Directory Names to Lowercase in Linux
Here we will see how to rename all files and directory names to lowercase in Linux. We can easily do it with some basic commands in Linux. The below command can be used to view the contents of your directory. $ find [directory_name] -depth Here 'priyanshu' directory which has some sub-files/folders, has all files starting from uppercase letters: $
1 min read
How to Recursively Find all Files in Current and Subfolders Based on Wildcard Matching in Linux
Traversing through a directory tree to locate files is a common operation performed by most filesystem management software. This utility is in the form of command-line commands in most Operating Systems. In this article, you will learn how to find files using Wildcard Matching in Linux Operating System. Recursively finding files in a directory tree
2 min read