1. Introduction

In system administration, copying files that match certain criteria is a common task for backups or file deletion.

In this tutorial, we’ll discuss how to copy files modified after a specific date, or modified within a given timeframe. We’ll also see examples for copying files based on other criteria like file size and file permissions

2. Filtering Files

Firstly, we’ll explore a method to find files based on a condition. We’ll use the find command to query the file system:

$ find /var/log -name "*.log" -type f -newermt "2023-03-25"

In this case, we’re finding files with .log extensions that are newer than 2023-03-25. There are several options we added to the default find command:

  • -name “*.log”: list of files ending with .log extension
  • -type f: include files and exclude directories
  • -newermt “2023-03-25”: files modified or created after 2023-03-25

As we saw, the find command is quite powerful to list the files based on specific criteria. Naturally, there are other options which we’ll explore here.

3. Copying Files

Using the above filters, we’ll see examples of copying files.

3.1. Time Filters

To demonstrate time-based filters, we’ll copy files that are newer than 2023-03-25 to the directory /tmp/backup:

$ find /var/log -name "*.log" -type f -newermt "2023-03-25" -exec cp -r {} /tmp/backup \;

Here, the find command has an extra option -exec cp -r {} /tmp/backup \;. It executes the cp -r command for the files matching the criteria.

Another example is copying files modified around a given time:

$ find /var/log -name "*.log" -type f -mtime +100 -exec cp -r {} /tmp/backup \;

The option -mtime +100 lists all the files which are modified more than 100 days ago. Again, we use cp on this dataset.

We can also copy files modified within a certain timeframe:

$ find /var/log -name "*.log" -type f -mtime +100 -mtime -200

The options -mtime +100 and -mtime -200 list all the files that are modified more than 100 days ago but less than 200 days ago. Of course, there are related options like -atime for access time instead of modification time. Some file systems even support -ctime for creation times.

3.2. Size Filters

In this example, we’ll copy files that are greater than 2 megabytes to the directory /tmp/backup:

$ find /var/log -name "*.log" -type f -size +2M -exec cp -r {} /tmp/backup \;

The -size +2M addition lists the files with a size greater than 2M. Similar to the example with times, we can also use less than () instead of greater than (+), or simply omit the sign for exact matches.

3.3. Permission Filters

We can match files based on their permissions and copy them to a different directory:

$ find /var/log -name "*.log" -type f -perm -g=w -exec cp -r {} /tmp/backup \;

The -perm -g=w option matches any files with an owning group having the w permission.

4. Conclusion

In this article, we discussed a few examples of combining the find command with the cp command, to copy files to a different location.

Firstly, we explored the various time-based filters available in the find command. Specifically, we saw filters based on file creation date and file modification time. Lastly, we used the size and permission-based filters to copy files.

Comments are closed on this article!