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

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

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Apr, 2021
Like Article
Save Article