When running tar cfzp home.tar.gz /home
, you might encounter different types of file-related warnings:
1. "File changed as we read it" - Warning about modified files during archiving
2. "File removed before we read it" - Warning about deleted files
The crucial distinction is that tar treats these cases differently:
- The "file changed" warning is just a NOTICE - tar continues processing
- The "file removed" case may or may not abort depending on tar version
For robust archiving of volatile directories, use:
tar --ignore-failed-read -cfzp backup.tar.gz /path/to/files
Key points:
- Must place --ignore-failed-read before mode flags (-c/-x)
- Continues archiving even if files disappear during operation
- Still maintains exit code 0 for successful operations
Here's a bulletproof backup script template:
#!/bin/bash
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
LOG_FILE="/var/log/backup_${TIMESTAMP}.log"
TAR_OPTS="--ignore-failed-read --warning=no-file-changed"
echo "Starting backup at ${TIMESTAMP}" >> ${LOG_FILE}
tar ${TAR_OPTS} -czpf /backups/home_${TIMESTAMP}.tar.gz /home \
2>> ${LOG_FILE}
if [ $? -eq 0 ]; then
echo "Backup completed successfully" >> ${LOG_FILE}
else
echo "Backup completed with warnings" >> ${LOG_FILE}
fi
For massive directories with frequent changes:
find /home -type f -print0 | \
tar --null --ignore-failed-read -czpf backup.tar.gz -T -
Benefits:
- Uses find to snapshot the file list first
- --null handles filenames with spaces
- -T reads files from stdin
After creating the archive:
tar -tzvf backup.tar.gz | wc -l
find /home -type f | wc -l
Compare counts to verify most files were archived (expect small differences for volatile files).
When running tar cfzp archive.tar.gz /path/to/files
, you might encounter:
tar: file.txt: file changed as we read it
tar: Exiting with failure status due to previous errors
This occurs because GNU tar (by default) treats file modifications during archiving as an error condition. The behavior differs between error types:
- File removed: "File removed before we read it" - tar continues
- File changed: "file changed as we read it" - tar exits by default
To force tar to complete the archive despite changes:
tar --ignore-failed-read -cfzp archive.tar.gz /home
Parameter order matters: Always place options before the -f
flag to avoid creating oddly named archives.
Here's a robust production-ready command:
tar \
--ignore-failed-read \
--warning=no-file-changed \
-czf backup-$(date +%Y%m%d).tar.gz \
--exclude="*.tmp" \
/home/user/important_data
Key additions:
--warning=no-file-changed
suppresses the warning messages
- Date-based archive naming
- Exclusion of temporary files
To check if your archive captured everything despite errors:
tar -tf archive.tar.gz | wc -l
find /home -type f | wc -l
For critical systems, consider using rsync
to create a snapshot first:
rsync -a --delete /home/ /tmp/home_snapshot/
tar -czf home_backup.tar.gz -C /tmp/home_snapshot .
For maximum control over file inclusion:
find /home -type f -print0 | \
tar --null -T - \
--ignore-failed-read \
-czf incremental_backup.tar.gz
This method handles spaces in filenames and gives you precise file selection control.
How to Force tar to Continue Archiving Despite “File Changed” Errors in Linux
3 views