Skip to content
Related Articles

Related Articles

Improve Article
How to Find Files Modified in Last N Number of Days in Linux?
  • Last Updated : 28 Apr, 2021

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

  • to list files whose timestamp has been changed more than n days ago – mtime +n
  • to list files whose timestamp has been changed less than n days ago – mtime – n
  • to list files whose timestamp has been changed exactly n days ago – mtime n

Syntax :-

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

where:



  • directory is the directory name in which the files are to be searched
  • -iname is the name of files and it could be a regex expression as well.
  • -mtime is to specify the number of days 

Example:

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

Linux Find Files Modified in Last N Number of Days

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.

  • to list files whose timestamp has been changed less than n days ago atime – n
  • to list files whose timestamp has been changed exactly n days ago atime n

Syntax:-

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

where:

  • directory is the directory name in which the files are to be searched
  • -iname is the name of files and it could be a regex expression as well.
  • -atime is to specify the number of days.

Example:

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

Linux Find Files Modified in Last N Number of Days



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 "

Linux Find Files Modified in Last N Number of Days

Method 4: The -newer option

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

Syntax:-

find directory -newer directory

Linux Find Files Modified in Last N Number of Days

My Personal Notes arrow_drop_up
Recommended Articles
Page :