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 blkid Command: Find Block-Device UUIDs and Filesystem Types

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Learn how to find Block-Device UUIDs and Filesystem Types with the Linux blkid command, including practical examples, key options, and important precautions.

Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.

Linux join Command: Combine Files by a Common Field

Linux join Command: Combine Files by a Common Field

Learn how to combine Files by a Common Field with the Linux join command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux more Command: View Text One Screen at a Time

Linux more Command: View Text One Screen at a Time

Learn how to use Linux more to read text one screen at a time, move and search interactively, start at a line or pattern, and choose between more and less.

Linux history Command: Review and Reuse Shell Command History

Linux history Command: Review and Reuse Shell Command History

Learn how to use the Linux history command to review, search, reuse, and manage shell command history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux killall Command: Signal Processes by Name

Linux killall Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux killall command, including practical examples, key options, and important precautions.