sync Command
The sync command forces the kernel to flush file system buffers to disk. It ensures that all pending write operations are completed and data is safely stored on persistent storage, which is crucial for data integrity and system reliability.
Syntax
sync [OPTION] [FILE...]
Description
The sync command synchronizes cached writes to persistent storage. Linux uses write caching to improve performance, storing write operations in memory buffers before writing them to disk. The sync command forces these cached writes to be immediately written to disk.
Key features:
- Flush all file system buffers to disk
- Synchronize specific files or file systems
- Ensure data integrity before system operations
- Force completion of pending write operations
- Essential for safe system shutdown and device removal
- Prevent data loss from unexpected power failures
Note: Modern Linux systems automatically sync during shutdown, but manual sync provides additional assurance for critical operations.
Common Options
| Option |
Description |
-d, --data |
Sync only file data, not metadata |
-f, --file-system |
Sync the file systems that contain the files |
--help |
Display help message and exit |
--version |
Display version information and exit |
Examples
Basic sync operation
sync
Flush all file system buffers to disk
Sync specific file
sync /path/to/important/file.txt
Synchronize a specific file to disk
Sync multiple files
sync file1.txt file2.txt file3.txt
Synchronize multiple specific files
Sync file system containing files
sync -f /home/user/documents/
Sync the file system that contains the specified directory
Sync only file data
sync -d /path/to/file
Sync only the file data, not metadata
Safe device removal
# Before unmounting USB drive
sync
umount /media/usb
Ensure all data is written before unmounting
Before system shutdown
sync && sync && sync
shutdown -h now
Traditional triple sync before shutdown (mostly historical)
Understanding File System Buffers
How Write Caching Works
# Check current buffer/cache usage
free -h
# View detailed memory information
cat /proc/meminfo | grep -E "(Buffers|Cached|Dirty|Writeback)"
# Monitor dirty pages (data waiting to be written)
watch -n 1 'cat /proc/meminfo | grep Dirty'
Monitor system buffer and cache usage
Buffer States
| State |
Description |
| Clean Buffers |
Data that matches what's on disk |
| Dirty Buffers |
Modified data waiting to be written to disk |
| Writeback |
Data currently being written to disk |
Checking Dirty Data
# Check amount of dirty data
$ cat /proc/meminfo | grep Dirty
Dirty: 1234 kB
# Check writeback data
$ cat /proc/meminfo | grep Writeback
Writeback: 0 kB
# View per-filesystem dirty data
$ cat /proc/fs/ext4/*/mb_stats
Monitor dirty data waiting to be synchronized
Practical Use Cases
System Administration
Safe system shutdown
# Ensure all data is written before shutdown
sync
# Check for any remaining dirty data
cat /proc/meminfo | grep Dirty
# Proceed with shutdown
shutdown -h now
Ensure data integrity before system shutdown
USB device removal
# Copy files to USB device
cp -r /home/user/documents/ /media/usb/
# Ensure all data is written
sync
# Wait for any pending writes
sleep 2
# Safe to unmount
umount /media/usb
Safely remove USB devices after data transfer
Database and Application Management
Database backup safety
# Stop database service
systemctl stop mysql
# Ensure all data is written
sync
# Create backup
tar -czf database_backup.tar.gz /var/lib/mysql/
# Sync backup file
sync database_backup.tar.gz
# Restart database
systemctl start mysql
Ensure database consistency during backup operations
Log file rotation
# Before rotating large log files
sync /var/log/application.log
# Rotate the log
mv /var/log/application.log /var/log/application.log.old
# Signal application to reopen log file
kill -HUP $(pidof application)
# Ensure new log file is synced
sync /var/log/application.log
Ensure log file integrity during rotation
Development and Testing
Critical file operations
# Write important configuration
echo "critical_setting=value" > /etc/app/config.conf
# Ensure it's written to disk
sync /etc/app/config.conf
# Verify the write
cat /etc/app/config.conf
# Restart service
systemctl restart app
Ensure critical configuration changes are persisted
Performance testing
# Clear caches for consistent testing
sync
echo 3 > /proc/sys/vm/drop_caches
# Run performance test
time dd if=/dev/zero of=testfile bs=1M count=1000
# Ensure test data is written
sync testfile
# Clean up
rm testfile
Ensure consistent conditions for performance testing
Advanced Usage
Monitoring and Automation
Automated sync script
#!/bin/bash
# Function to sync with progress indication
safe_sync() {
echo "Starting sync operation..."
# Check dirty data before sync
DIRTY_BEFORE=$(awk '/^Dirty:/ {print $2}' /proc/meminfo)
echo "Dirty data before sync: ${DIRTY_BEFORE} kB"
# Perform sync
sync
# Wait for completion
while [ $(awk '/^Dirty:/ {print $2}' /proc/meminfo) -gt 100 ]; do
echo "Waiting for sync to complete..."
sleep 1
done
DIRTY_AFTER=$(awk '/^Dirty:/ {print $2}' /proc/meminfo)
echo "Dirty data after sync: ${DIRTY_AFTER} kB"
echo "Sync completed successfully"
}
safe_sync
Script to perform sync with progress monitoring
Periodic sync daemon
#!/bin/bash
# Periodic sync script for critical systems
while true; do
# Check if dirty data exceeds threshold
DIRTY=$(awk '/^Dirty:/ {print $2}' /proc/meminfo)
if [ $DIRTY -gt 10240 ]; then # 10MB threshold
echo "$(date): Dirty data: ${DIRTY}kB - syncing"
sync
fi
sleep 30 # Check every 30 seconds
done
Daemon to automatically sync when dirty data exceeds threshold
System Tuning
Adjust sync behavior
# View current sync settings
cat /proc/sys/vm/dirty_ratio
cat /proc/sys/vm/dirty_background_ratio
cat /proc/sys/vm/dirty_writeback_centisecs
# Adjust sync frequency (temporary)
echo 5 > /proc/sys/vm/dirty_background_ratio
echo 10 > /proc/sys/vm/dirty_ratio
echo 100 > /proc/sys/vm/dirty_writeback_centisecs
# Make permanent in /etc/sysctl.conf
echo "vm.dirty_background_ratio = 5" >> /etc/sysctl.conf
echo "vm.dirty_ratio = 10" >> /etc/sysctl.conf
echo "vm.dirty_writeback_centisecs = 100" >> /etc/sysctl.conf
Tune kernel parameters for sync behavior
Emergency Procedures
Force sync during system issues
# Emergency sync when system is unresponsive
echo s > /proc/sysrq-trigger # Emergency sync
# Alternative: Magic SysRq key sequence
# Alt + SysRq + S
# Check if sync completed
cat /proc/meminfo | grep Dirty
# If system is still responsive, use regular sync
sync
Emergency sync procedures for system recovery
Best Practices
sync Command Best Practices
- Use Before Critical Operations - Always sync before unmounting, shutdown, or device removal
- Monitor Dirty Data - Check /proc/meminfo to understand sync necessity
- Don't Overuse - Excessive syncing can hurt performance; use judiciously
- Combine with Sleep - Allow time for sync to complete in scripts
- Check Return Status - Verify sync completed successfully in critical scripts
- Understand Your Workload - Adjust sync frequency based on data criticality