In UNIX and UNIX-like operating systems like Linux, the sync
command is used to synchronize file data in the main memory with the persistent storage.
Since writing to a file is done in an asynchronous manner, the writes are cached in the memory until it is the appropriate time to write them to the disk. This frees up the CPU to do other tasks while files are written to the disk.
The sync
command forces the file writes in the main memory cache and the disk to synchronize.
sync [[-d | --data] | [-f | --file-system]] [file …]
-d or --data
: This is used to only sync file data and the minimum metadata needed to maintain filesystem consistency.
-f or --file-system
: This is used when you want to sync up all the pending I/O and all the referenced files inside the filesystem.
file...
: The names of one or more files specified. If no file is specified, all the mounted files are synchronized.
It is uncommon to manually use the sync
command. Generally speaking, the sync
command is only used prior to performing tasks that may make the kernel unstable or are known to cause crashes.
To sync up all cached files of the current user:
sync
To sync all mounted systems:
sudo sync
To sync just two files:
sync $HOME/file1 $HOME/file2
To sync up only the data and minimal metadata of two files:
sync -d $HOME/file1 $HOME/file2
Free Resources