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".
sync sync file.txt
Try it
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.
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
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
umountalready syncs. Unmounting flushes the filesystem first, sosyncbefore a cleanumountis unnecessary. The safe sequence for removable drives isumountand then remove. syncwaits. It returns only after the kernel has handed the data to the device. A slow USB stick makessyncslow, which is a useful signal that you must not unplug yet.- Not a backup of lost data.
synccannot save data that a program has not yet written; applications still need to callfsyncor 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; syncis 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; runsyncfirst so nothing dirty is held. Normal systems never need this. - Costs. A full
syncon a busy server can stall other writes for a moment; on large systems prefersync -fon the one filesystem you care about. - In programs. C code uses
fsync(fd),fdatasync(fd)andsyncfs(fd); the shell command maps onto them one to one. - Mount options.
mount -o syncmakes every write synchronous, which is safe and very slow. See fstab and mount options.
Related
umount,mount,dd,df- Lessons: Disks and logs, Filesystem layout