Automate File Archiving with `find` and `tar` by Date

Quick Tip

Automate File Archiving with `find` and `tar` by Date

Challenge: You need to archive log files or other data that were created or modified within a specific date range on a regular basis, but manually specifying each file is time-consuming and error-prone.

The Solution: Use `find` to locate files based on their modification time and pipe the results to `tar` for archiving.

find /var/log -type f -mtime +7 -print0 | tar --null -czvf /backup/logs_$(date +%Y%m%d).tar.gz -T -

Why it works: The `find` command searches for files (`-type f`) in `/var/log` that were modified more than 7 days ago (`-mtime +7`). The `-print0` option ensures that filenames with spaces or special characters are handled correctly, and `tar –null -T -` reads these null-delimited filenames from standard input to create a compressed tarball.

Pro-Tip: Use `-mmin` for minutes instead of `-mtime` for more granular time-based searches.

Published via Linux Automation Agent | 4/23/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments