Open In App

How to Find Files Modified in Last N Number of Days in Linux?

Sometimes, we want to find the files which we created or modified in the last N number of days. Sorting the files on the basis of date helps in this case, but that’s the traditional way of doing the task and is not efficient. This article is all about searching such files using the find command.

Method 1: Using -mtime(modification time) attribute of find command



It is a modified timestamp it tells us when a file was last modified either by a program or a user and mtime also changes when the file’s contents are changed or modified. We can see the timestamps of the file within a certain range

Syntax :-



find directory -iname "*.txt " -mtime  -n -print

where:

Example:

find /home/priyanshu -iname "*.txt " -mtime  -2 -print

Method 2: Using atime (File access) attribute of the find command.

It is an access timestamp, and it tells us the last time a file was read or accessed which means an application was used to open the file and read its contents.

Syntax:-

find directory -iname "*.txt " -atime  - n -type f

where:

Example:

find /home/priyanshu -iname "*.txt " -atime  -1 -type f

Method 3: The daystart option

It measures the time from the start of the current day instead of 24 hours ago.

Syntax:-

To list the files edited between n1-n2 days ago for pdf files.

find directory -mtime n1 -mtime -n2 -daystart -iname "*.pdf "

Method 4: The -newer option

It compares the files from two directories and displays the files that are newer

Syntax:-

find directory -newer directory

Article Tags :