Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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
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:
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.
Using the above filters, we’ll see examples of copying files.
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.
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.
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.
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.