Linux sync command

Updated: 03/13/2021 by Computer Hope
sync command

On Unix-like operating systems, the sync command synchronizes corresponding file data in volatile memory and permanent storage. Cached writes are immediately written to disk.

This page describes the GNU/Linux version of sync.

Description

By default, the Linux kernel writes data to disk asynchronously. Writes are buffered (cached) in memory, and written to the storage device at the optimal time. The sync command forces an immediate write of all cached data to disk.

Run sync if you anticipate the system to be unstable, or the storage device to become suddenly unavailable, and you want to ensure all data is written to disk.

Individual files may be synced, or the entire filesystem containing the specified files. If no arguments are provided, all mounted file systems are synced.

This page refers to GNU sync, which is distributed with most Linux operating systems.

Syntax

sync [[-d | --data] | [-f | --file-system]] [file …]
sync [--help | --version]

Options

file &hellp; The file names of the files to sync. A single dash ("-") represents the standard input file descriptor, and is treated as a file name. If no file names are specified, all mounted file systems are synced.
-d | --data Use the fdatasync system call to sync only the file data, and the minimum metadata required to maintain file system consistency.
-f | --file-system Use the syncfs system call to sync all pending I/O with the file system containing the specified files. Note, you should not use this option if specifying a device file, such as /dev/sdb. If you do, the file system containing the device file will be synced (e.g., /dev/sda, contains the root file system), when you probably intended to sync the referenced device.
--help Display a help message, and exit.
--version Display version information, and exit.
-- Two dashes indicate the end of options. Any subsequent arguments, including arguments that start with a dash, will be treated as file names.

Examples

sync

Sync all cached file data of the current user.

sudo sync

Sync all mounted file systems.

sync $HOME/.bashrc $HOME/my/important/file

Sync only those two files.

sync -d $HOME/file1 $HOME/file2 $HOME/file3

Sync only the file data and minimal metadata of those three files.

sudo sync /dev/sdc1

Sync the file system on mounted partition /dev/sdc1.

sudo sync /dev/sdc

Sync all mounted file systems on device /dev/sdc.

sudo sync /var/lib/mysql

Assuming /var/lib/mysql is a directory, sync it and all the files and subdirectories it contains.

sudo sync /var/log/syslog

Sync the file /var/log/syslog.

sudo sync -f /var/log/syslog

Sync the entire file system which contains /var/log/syslog.

sudo sync -f /dev/sdb

Sync the entire file system which contains the device file /dev/sdb, which may not be /dev/sdb.