Open In App

How to Find and Remove Files Modified or accessed N days ago in Linux

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Searching for the files which were modified (or accessed) some days ago is a common operation that is performed by Archival or Backup applications. This process is a trivial one for an Operating System that stores all the Metadata about the files in a File Table. Therefore, there exist commands offered by the Command Processor of the Operating System that could be used to obtain this information programmatically. In this article, you will learn how to find files that were modified or accessed N days ago in Linux and remove them. 

For this, we would be using the find command in bash with different attributes. Find command is used for walking a file hierarchy. It is used to traverse through a directory and its branches to find out the presence of a file/folder in it. It supports searching by file, folder, name, creation date, modification date, owner, and permissions. If used along with appropriate attributes we could obtain files/folders that match the specific criteria. For this case, we would be using the attributes -atime and -mtime to get files based on their access and modification timestamps. After obtaining the files that meet the specific criteria, we would be removing them using the rm command.

The man page for the find command is 

 

The man page for the rm command is 

 

Finding the files that match the criteria 

For finding the files that were accessed N days ago we would be using the following command

find -atime N

where N = no. of days. So if we have to find all the files that have been accessed 5 days ago the command would be 

find -atime 5

Now to get files that were modified 5 days ago we would add the -mtime attribute to the before-mentioned command

find -atime 5 -mtime 5

The above command will display all the files which have been either modified or accessed 5 days ago.

Now to remove the files we will be using the pipeline | to pipe the output of the find to the rm command. What this does is it will pass all the files which are matching the criteria, to xargs in which we have defined an rm command. Therefore all the criteria matching files are to be removed/deleted. The final command for which would be

find -atime 5 -mtime 5 | xargs rm

Example:

In the following example, we would be using two files named gobby.txt and privet.txt. Both of these have been created 5 days before the current date. Therefore, their modification and access dates are also of 5 days ago.

Firstly we used the ls -l command to display the creation dates of the two files which are of 15th September, which is 5 days before the current date of 20th September. (The command ls is solely used for displaying the current state of the current working directory, and has no use in the process of finding the files. Therefore, it is only used for demonstration)

 

Now we ran the find -atime 5 -mtime 5 commands to see which files satisfied the aforementioned conditions.

 

Then we passed the output of the previous command into the rm command to remove those files. 

 

In the end, we displayed all the files in the current directory with the ls command. The two files which were meeting the criteria could no longer be found. 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads