1. Introduction

In this tutorial, we’ll learn three different ways to copy Linux files based on their modification date.

Let’s start with some directories and files:

$ ls -lR
total 92
drwxr-xr-x. 2 root root    21 Jan  2 07:44 dir1
drwxr-xr-x. 2 root root    21 Jan  3 07:44 dir2
drwxr-xr-x. 2 root root    21 Jan  4 07:43 dir3
-rw-r--r--. 1 root root 56559 Dec 30 11:04 file1
-rw-r--r--. 1 root root 11379 Jan  1 11:05 file2
-rw-r--r--. 1 root root 21793 Jan  2 07:44 file3

total 0
-rw-r--r--. 1 root root 0 Jan  4 07:42 inside1

total 0
-rw-r--r--. 1 root root 0 Jan  4 07:43 inside2

total 0
-rw-r--r--. 1 root root 0 Jan  4 07:43 inside3

We have three directories and three files here. All directories have their own files (inside1, inside2, inside3)

2. Using find

We want to use the find command to copy all the files that have changed in the last three days to a specific path /root/desFolder. So first, we need to take a quick look at the output of find:

$ find . -type f

Here the ‘.’ refers to the current directory. We can see that all folders are also considered. Therefore we should limit the result to consider only files by using -type.

We can easily specify files that are modified from 3 days ago by using -mtime:

$ find . -type f -mtime -3

Eventually, if the result is what we expected, we use the cp command to copy the files to the destination:

$ find . -type f -mtime -3 -exec cp {} /root/desFolder \;

We should close -exec by using ‘\;‘ at the end. Moreover, we need to pay attention to a significant point. All files will be copied into one folder, so if we have duplicate names in different folders, only one will be copied.

3. Using for

Instead of our previous problem, let’s say we want to copy all files modified on a specific day. So we can use the for loop as the main command, which gets files from ls and copies those files in each step of the loop with the cp command.

Firstly, we filter the files that were changed on a specific day by using ls and grep:

$ ls -ltr | grep "Jan  2"
-rw-r--r--. 1 root root 21793 Jan  2 07:44 file3
drwxr-xr-x. 2 root root    21 Jan  2 07:44 dir1
We used -ltr to sort our list by the last modification time. Also, we used grep to specify the modification date. We should note that here we have two spaces on “Jan  2”.
Secondly, we can use for loop to copy files:
$ for i in `ls -ltr | grep "Jan  2" | awk '{print $9}' `; do cp $i* /root/desFolder; done

If we need to copy the files inside the subfolders, we can use cp -r.

We can also copy files that are modified between ‘07:00‘ and ‘07:59‘:

$ for i in `ls -ltr | grep "Jan 2 07:" | awk '{print $9}' `; do cp $i* /root/desFolder; done

Here we used ’07:‘ because grep considers details as text, so it cannot recognize the time.

4. Using cp

Lastly, let’s use cp as the main command and use find to get our desired files from the specified date until now.

We can easily use the cp command to copy files, and also we use find to filter specific files:

$ cp `find . -type f -newermt '1 Jan'` /root/desFolder
The above copies all the modified files in the current directory after 1 January.
We can also specify the time:
$ cp `find . -type f -newermt '1 Jan 2023 08:00'` /root/desFolder

If we want to specify the time, we must use date-time in the full format (e.g., DD-MM-YYY HH:MM)

5. Conclusion

There are many ways to copy files based on modification date, and we’ve looked at just three ways. We used the cp command in all of them for copying files. In sum, the usage of these ways is different in ‘how to filter files to copy’ or ‘how to specify the time’.

We may need various arguments of cp in different situations.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.