sync
Synchronize file system buffers with disk
TLDR
Flush all pending write operations on all disks
Flush all pending write operations on a single file to disk
Flush writes and drop file system caches (Linux only)
Flush disk writes and attempts to clear inactive memory and filesystem caches (macOS only)
SYNOPSIS
sync [OPTION]...
PARAMETERS
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The sync command is used to ensure that all data held in the operating system's memory buffers, which represents modifications to files, is written out to the physical storage devices (disks).
Modern Unix-like operating systems employ sophisticated caching mechanisms to improve performance. When an application writes data to a file, it's often initially written to an in-memory buffer (known as a 'dirty page') rather than directly to disk. This allows the system to bundle writes, reorder them, and avoid frequent, slow disk I/O operations. While this improves responsiveness, it also means that in the event of an unexpected system crash, power loss, or ungraceful shutdown, data residing only in memory buffers would be lost.
sync forces the kernel to write these 'dirty' buffers to their respective disk blocks, thus ensuring data integrity and consistency. It's particularly crucial before critical operations like unmounting a filesystem or performing a system reboot or shutdown, though most modern utilities (like umount, reboot, shutdown) automatically invoke sync beforehand.
CAVEATS
While sync flushes buffers to the device driver, it does not guarantee that the data has been physically written to the platter or flash memory before the command returns. Modern disk drives often have their own internal caches. For absolute assurance, a storage device might need to implement a 'flush cache' command (e.g., via SCSI 'SYNCHRONIZE CACHE' or ATA 'FLUSH CACHE' commands), which the kernel generally handles. However, sync itself doesn't wait for this deeper level of write completion.
On some older or BSD-derived systems, sync might offer options like -d (sync data only) or -f (sync filesystems). However, the common GNU Coreutils version on Linux typically does not support these options and simply flushes all dirty buffers.
COMMAND VS. SYSTEM CALL
The sync command is a user-space utility that typically invokes the sync() system call. The system call instructs the kernel to write all buffered filesystem data to disk. While the command provides a convenient way for users to trigger this action, the real work is performed by the kernel's internal mechanisms in response to the system call.
KERNEL BUFFERS AND DIRTY PAGES
The data that sync flushes is often referred to as 'dirty' data or 'dirty pages' in the kernel's page cache. When an application writes to a file, the kernel marks the corresponding memory pages as 'dirty'. These dirty pages are then periodically written to disk by a kernel background process (e.g., 'pdflush' or newer writeback threads), but sync allows for an explicit, on-demand flush.
HISTORY
The sync command, along with its underlying sync(2) system call, has been a fundamental part of Unix-like operating systems since their early days. It emerged as a necessary utility to manage the trade-off between performance (via buffered I/O) and data integrity. Its basic function has remained consistent for decades, providing a reliable way for users and system scripts to ensure that volatile buffered data is committed to non-volatile storage.