sync

system administrationLinux/Unix
The sync command is one of the most frequently used commands in Linux/Unix-like operating systems. sync Synchronize cached writes to persistent storage

Quick Reference

Command Name:

sync

Category:

system administration

Platform:

Linux/Unix

Basic Usage:

sync [options] [arguments]

Common Use Cases

    Syntax

    sync [options] [file...]

    Options

    Option Description
    -d, --data Sync only file data, no unneeded metadata
    -f, --file-system Sync the file systems that contain the files
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the sync command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sync
    Force all pending disk writes to be committed to stable storage.
    sync /home/user/important_file.txt
    Synchronize only the specific file to disk.
    sync -d /mnt/data
    Synchronize only the directory's metadata, not its contents. # Advanced Examples Advanced # Use sync before unmounting external storage sync umount /media/usb # Sync before shutting down system (although shutdown will do this anyway) sync shutdown -h now # Sync after large file operations cp -r /large/directory /backup sync # Sync specific mount points before backup sync /mnt/data rsync -av /mnt/data /backup # Use with other utilities dd if=/dev/zero of=/mnt/file bs=1M count=100 && sync # Sync before ejecting removable media sync eject /dev/cdrom # Script for safely writing data to USB drives
    cat > file.txt << EOF
    Important data that must be saved EOF sync echo "Data has been safely written" # Sync before fsck sync fsck -f /dev/sdb1 # Use in system backup scripts tar -czf backup.tar.gz /important/data sync echo "Backup completed and synced to disk"

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `sync` command is a fundamental utility in Unix and Linux systems that forces all pending disk writes to be written to persistent storage. In modern operating systems, disk writes are often cached in memory for performance reasons and are flushed to disk at intervals determined by the system. The `sync` command allows users to manually trigger this flush operation, ensuring that all modified data has been physically written to the storage device. This command serves several critical purposes in system administration and data integrity: 1. **Data Integrity**: By forcing cached data to be written to disk, `sync` helps ensure that data isn't lost in the event of a system crash or power failure. This is particularly important before operations like system shutdown or removing storage devices. 2. **Storage Device Safety**: Before physically disconnecting storage devices like USB drives or memory cards, running `sync` ensures that all pending writes are completed, preventing data corruption. 3. **Filesystem Consistency**: By synchronizing cached filesystem metadata, `sync` helps maintain filesystem consistency, which is essential for proper system operation. In its simplest form, running `sync` without arguments will synchronize all filesystems on the system. However, newer versions of the command also support synchronizing specific files or directories, providing more granular control over the synchronization process. The command offers several options: - `-d` or `--data`: Synchronizes only file data, not metadata. This can be faster when metadata synchronization isn't necessary. - `-f` or `--file-system`: Synchronizes the entire filesystems that contain the specified files, rather than just the files themselves. It's important to understand that while `sync` forces pending writes to disk, it doesn't guarantee that the data will survive a catastrophic hardware failure. For critical data, additional measures like redundancy, backups, and journaling filesystems should also be employed. Historically, `sync` was often used in the "sync; sync; sync" pattern (running it three times) to ensure complete synchronization, but modern systems generally don't require this repetition as a single invocation is typically sufficient. In daily use, most users rarely need to run `sync` explicitly, as the system automatically synchronizes data periodically, and operations like proper system shutdown include synchronization. However, it remains an essential tool in scenarios requiring explicit control over when data is committed to persistent storage.

    Related Commands

    These commands are frequently used alongside sync or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sync command works in different scenarios.

    $ sync
    View All Commands