Quick Tip
Taming `tar` with `–newer-than` for Incremental Backups
Challenge: You need to create a backup of a directory, but only include files that have been modified since your last backup. Manually tracking these changes is tedious and error-prone.
The Solution: Utilize the `–newer-than` option with `tar` to create incremental archives based on a reference file.
# First, create a full backup and a timestamp file tar -czvf /path/to/backup/full_backup_$(date +%Y%m%d).tar.gz /path/to/data touch /path/to/backup/last_backup.timestamp # To create an incremental backup tar -czvf /path/to/backup/incremental_backup_$(date +%Y%m%d).tar.gz --newer-than=/path/to/backup/last_backup.timestamp /path/to/data # After a successful incremental backup, update the timestamp file touch /path/to/backup/last_backup.timestamp
Why it works: The `–newer-than` option tells `tar` to only include files in the archive that have been modified more recently than the specified reference file. By updating the timestamp file after each successful backup, you effectively create a series of incremental archives.
Pro-Tip: For more complex backup strategies, consider using `rsync` which is highly efficient for incremental synchronization and can handle file deletions and permissions more robustly.
Linux Tips & Tricks | © ngelinux.com | 4/29/2026
