Find latest modified files in a directory in Linux (Recursively)

In this article, we will learn about different ways to find the latest modified files in a folder and its subfolders in Linux.

Table Of Contents

We often get a requirement to find all the files that got modified at a specific time. Even though linux operating system provides an option to Sort the files using modified date, that option may not work if you have modified multiple files within the directory and it’s sub-directories. Here we will look at different options that will help us to find all the files that got modified in a directory and it’s sub-directory.

Option 1 : Using ‘-mtime’ along with find command

Syntax for finding the latest modified files in a directory and sub-directories,

find [directory-path] -mtime [+/-]N -ls 
  • find : This linux command will be used to find files
  • [directory-path] : Specify the directory in which you want to search files
  • [+/-]N : This will be used to specify number of days
  • -ls : This will list all files

Examples

Let’s find all files that got modified in last 1 day.

find /home/opc -mtime -1 -ls

Output:

100663431      0 drwx------   3  opc      opc            74 Aug  7 19:54 /home/opc
8547           0 drwx------   2  opc      opc            29 Aug  7 19:54 /home/opc/.ssh
8548           4 -rw-------   1  opc      opc           400 Aug  7 19:54 /home/opc/.ssh/authorized_keys

It looked into the folder “/home/opc” and all its subfolders for the files which are modified in last 1 day. Then printed the list of those files.

Option 2 : Using ‘-newermt’ along with find command

Syntax for finding the latest modified files in a directory and sub-directories,

find [directory-path] -newermt [Date Options] -ls
  • find : This linux command will be used to find files
  • [directory-path] : Specify the directory in which you want to search files
  • [Date Options] : Specify date options like 24 hours or 1 day ago
  • -ls : This will list all files

Examples

Let’s find all files that got modified in last 1 min.

find /home/opc -newermt "-1 minute" -ls

Output:

100663431      0 drwx------   4  opc      opc           121 Aug  7 20:14 /home/opc
100690180      0 -rw-rw-r--   1  opc      opc             0 Aug  7 20:14 /home/opc/tutorial.sh

It looked into the folder “/home/opc” and all its subfolders for the files, which are modified in last 1 minute. Then printed the list of those files.

Option 3 : Using ‘-mmin’ along with find command

Syntax for finding the latest modified files in a directory and sub-directories

find [directory-path] -mmin [+/-]N -ls
  • find : This linux command will be used to find files
  • [directory-path] : Specify the directory in which you want to search files
  • [+/-]N : This will be used to specify number of days
  • -ls : This will list all files

Examples

Let’s find all files that got modified in last 1 day.

find /home/opc -mmin -1 -ls

Output:

100663431      0 drwx------   3  opc      opc            74 Aug  7 19:54 /home/opc
8547           0 drwx------   2  opc      opc            29 Aug  7 19:54 /home/opc/.ssh
8548           4 -rw-------   1  opc      opc           400 Aug  7 19:54 /home/opc/.ssh/authorized_keys

It looked into the folder “/home/opc” and all its subfolders for the files, which are modified in last 1 day. Then printed the list of those files.

Summary:

In this guide, we learnt to find latest modified files in a directory and sub-directories in Linux.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top