Linux sync Command: Flush Buffered Writes to Storage

sync requests the kernel to reflect in the storage device the file and file system write data remaining in memory. It can be used to wait for write completion before disconnecting a storage device immediately after copying files.

What is the sync command?

Linux may keep writes in a buffer for performance. sync uses a system call to synchronize this to permanent storage. When run without arguments, it targets all pending writes in the system, and in the GNU implementation, specifying a file allows synchronizing the writes of that particular file.

Basic syntax

sync [option] [file...]

Installed implementations and options may vary depending on the distribution. Check the current system description with man sync.

Examples

Synchronizing pending writes

Requests that the system reflect current write data to the storage device.

sync

Synchronizing a specific file

In the GNU implementation, synchronizes the contents of a single file.

sync report.txt

Synchronize the file system a file belongs to

Targets pending writes of the file system that the file path belongs to.

sync -f /mnt/data/report.txt

Meaning when there is no output

The sync command requests the kernel to send any pending filesystem writes to the storage device. When successful, it usually does not display anything on the screen.

sync
echo $?

Example output:

0

0 is the shell exit status indicating that the most recent sync command succeeded. This alone does not guarantee that all device hardware caches have been permanently stored or that backups are complete. When disconnecting an external disk, wait for writes to complete and safely unmount it with umount.

Main Options and Format

Options/Format Description
No arguments Synchronizes all pending writes in the entire system.
file Synchronizes the specified file in the GNU implementation.
-d Synchronizes the file's data and the metadata needed for consistency.
-f Synchronizes the file system that the specified file belongs to.

Precautions when using

Even if sync succeeds, you should properly unmount the file system using umount rather than immediately removing external storage devices. It should not be interpreted as an absolute guarantee of data, including hardware caches and device errors. For sync -f, specifying a file path within the file system instead of a block device node matches the intended use.

Frequently Asked Questions

Is it enough to just run sync before removing a USB drive?

No. Sync requests write synchronization but does not unmount the drive. After finishing your work, properly run umount.

Official Documentation

You can check the exact behavior of the options and implementation differences in the official documentation related to sync.

More in This Category
Linux rsync Command: Synchronize Files and Directories

Linux rsync Command: Synchronize Files and Directories

Learn how to synchronize Files and Directories with the Linux rsync command, including practical examples, key options, and important precautions.

Linux basename Command: Extract the Final Component of a Path

Linux basename Command: Extract the Final Component of a Path

Learn how to use the Linux basename command to remove directory prefixes and optional suffixes from path strings, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux sftp Command: Transfer Files Securely over SSH

Linux sftp Command: Transfer Files Securely over SSH

Learn how to use the Linux sftp command to transfer and manage files securely through an SSH connection, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux unxz Command: Decompress xz Files

Linux unxz Command: Decompress xz Files

Learn how to decompress xz Files with the Linux unxz command, including practical examples, key options, and important precautions.

Linux locate Command: Search Indexed File Paths Quickly

Linux locate Command: Search Indexed File Paths Quickly

Learn how Linux locate searches a filename database, how updatedb affects freshness, how to limit and filter matches, and when find is the better tool.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

Linux tar Command: Create and Extract Archives

Linux tar Command: Create and Extract Archives

Learn how to create and Extract Archives with the Linux tar command, including practical examples, key options, and important precautions.

Linux du Command: Check File and Directory Disk Usage

Linux du Command: Check File and Directory Disk Usage

Learn how to use the Linux du command to measure and summarize disk space used by files and directories, with essential options, practical examples, output interpretation, and common troubleshooting tips.