The Linux sync command allows users to synchronize cache data to permanent system memory to save important files and prevent data loss.
This tutorial shows you how to use the Linux sync command with examples.

Prerequisites
- A Linux system (this tutorial uses Ubuntu 22.04).
- A user account with sudo privileges (for some options).
- Access to the terminal window.
Linux sync Syntax
The basic syntax for the sync command is:
sync [options] [file]
The file flag and options are not required, but they enable users to customize the synchronization process. Specifying the file argument allows the sysadmin to choose a specific file to synchronize.
Without any arguments, the sync command synchronizes all cached data for the current user to the permanent memory:
sync

Note: The sync command prints no output if run correctly.
Linux sync Options
The sync command works with several options. A table of all available options is below.
| Option | Description |
|---|---|
-d, --data | Synchronizes only the file data and the essential metadata needed to maintain the system's consistency. |
-f, --file-system | Synchronizes the file system linked to the provided file. |
--help | Prints a help message and exits. |
--version | Shows version info and exits. |
-- | Indicates the end of options. Any subsequent arguments are to be treated as file names. |
Linux sync Command Examples
The sync command ensures all cached data is written to permanent memory. The command is helpful for syncing individual files on the entire file system.
The following section provides examples of common ways to use the Linux sync command.
1. Sync all File Systems
While using sync without arguments synchronizes the current file system, executing the command with sudo syncs all mounted file systems.
sudo sync

2. Sync Specific Files
To synchronize two or more files with sync, include the file path in the command. For example, to sync myfile1 and myfile2 (both in the Home directory), run:
sync ./myfile1 ./myfile2

The same argument works if files are in different directories. For example, sync myfile1 from Home and anotherfile from ./mydirectory with:
sync ./myfile1 ./mydirectory/anotherfile

3. Sync File Data Only
To sync only file data and the minimum metadata necessary to maintain file system consistency, use sync with the -d option. For instance, synchronize only data from myfile1, myfile2, and myfile3 with:
sync -d ./myfile1 ./myfile2 ./myfile3

4. Sync a Directory
Use sync with a path to a directory to sync it and all the files and directories it contains.
For instance, sync mydirectory with:
sync ./mydirectory

5. Sync a File System Containing a File
To sync a file system linked to a specific file, use sync with the -f argument. For example, synchronize the entire file system containing myfile1 with:
sync -f myfile1

6. Check sync Version
To verify the sync version, run the following:
sync --version

7. Print a Help Message
To get additional info on the sync command and the complete list of options, run the following:
sync --help

Conclusion
After reading this article, you know how to use the Linux sync command. Next, learn how to back up your files with rsync.



