Juicy lil' bytes

Find Files by Extension and Modification Time

I came across this file extension x file modification time search capability in linux/unix systems as I was looking into different solutions for a new software project that I am currently working on. It’s a simple shell one-liner.

The First Step : Find by File Extension

The *nix find command allows you to use wildcards for your search in order to identify any file by its filetype. For instance, any c source code file with a .c extension can be identified with the following command:

1
find . -name "*.c"

The first period in the sequence indicates the path for your search (and can be replaced with any path) and the wildcard string argument for the -name switch specifies any filename with a .c extension.

The Next Step : Find by Modification Time

Now let’s extend the command to find any file modified within a specified period of time. The find command has a -mtime (modified time) switch that provides this feature. By default, it takes arguments that are 24 hour time block multipliers. To indicate that you want to find files with a .c extension that were modified within the last 24 hours, you use this command:

1
find . -name "*.c" -mtime -1

For files that were modified within the last 72 hours, use this command:

1
find . -name "*.c" -mtime -3

And if you need more granular control of the time, you can specify the time as the -mtime switch argument. For files modified in the last 1h:

1
find . -name "*.c" -mtime -1h

The command supports s (second), m (minute), h (hour), d (day), and w (week) arguments.

Modify the Command for Any File Extension

To change the file extension, simply change the wildcard string in the search. Here’s how you would find Python source files (with a .py extension) that were modified in the last hour:

1
find . -name "*.py" -mtime -1h

and .png image files that were modified in the last hour:

1
find . -name "*.png" -mtime -1h

and .txt files that were modified in the last hour:

1
find . -name "*.txt" -mtime -1h