Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Disks and Filesystems

Linux sync command

Reference for the Linux sync command: flush cached writes to disk, sync a single file or filesystem with -d and -f, and know when sync matters before unplugging drives.

When a program writes to a file, Linux normally keeps the data in memory (the page cache) and writes it to the disk a little later. This makes everything fast, but it also means that "write finished" does not mean "data is safe on the disk". sync asks the kernel to write all pending changes (file data, directory entries, inodes) to the storage devices.

You use it before pulling a USB drive that is not unmounted, before a hard power-off, and in scripts that must be sure a file is durable before continuing. It does not free the cache; the data stays cached and is simply no longer "dirty".

Terminal
sync
sync file.txt

Try it

Bash Flush everything, or just one file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Watching dirty memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
sync [OPTION] [FILE]...
Option Description
no arguments Flush all cached writes of all mounted filesystems.
FILE With file arguments, flush only those files (fsync).
-d, --data Flush only file data, not unneeded metadata (fdatasync).
-f, --file-system Flush the whole filesystem that contains each FILE (syncfs).

-d and -f need at least one file argument.

Returns: 0 on success, non-zero when a file does not exist or cannot be opened, or when the device reports an error.

Usage notes

  • A successful umount already syncs. Unmounting flushes the filesystem first, so sync before a clean umount is unnecessary. The safe sequence for removable drives is umount and then remove.
  • sync waits. It returns only after the kernel has handed the data to the device. A slow USB stick makes sync slow, which is a useful signal that you must not unplug yet.
  • Not a backup of lost data. sync cannot save data that a program has not yet written; applications still need to call fsync or close files.
  • Disk caches may lie. A drive with a volatile write cache may report data as written before it truly is. Servers use barriers, battery-backed controllers or disable the drive cache for this reason.
  • sync; sync; sync is superstition. Old advice to repeat it comes from early Unix. One call is enough on Linux.
  • Dropping caches is different. echo 3 > /proc/sys/vm/drop_caches (root) discards clean cached data to free memory; run sync first so nothing dirty is held. Normal systems never need this.
  • Costs. A full sync on a busy server can stall other writes for a moment; on large systems prefer sync -f on the one filesystem you care about.
  • In programs. C code uses fsync(fd), fdatasync(fd) and syncfs(fd); the shell command maps onto them one to one.
  • Mount options. mount -o sync makes every write synchronous, which is safe and very slow. See fstab and mount options.