Linux sync Command with Examples

January 12, 2023

Introduction

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.

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
sync terminal output

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.

OptionDescription
-d, --dataSynchronizes only the file data and the essential metadata needed to maintain the system's consistency.
-f, --file-systemSynchronizes the file system linked to the provided file.
--helpPrints a help message and exits.
--versionShows 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
sudo sync terminal output

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
sync two specific files terminal output

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
sync-twpo-specific-files-in-differen-directories-terminal-output

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
sync -d terminal output

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
sync a directory terminal output

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
sync -f terminal output

6. Check sync Version

To verify the sync version, run the following:

sync --version
sync-version-terminal-output

7. Print a Help Message

To get additional info on the sync command and the complete list of options, run the following:

sync --help
sync --help terminal output

Conclusion

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

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Using the Linux free Command
December 29, 2021

The free command in Linux is a utility that helps monitor RAM usage on a system. This guide shows how to use the free command and determine if there are enough resources for running new apps...
Read more
How to Use the top Command in Linux
December 2, 2021

The top command is a built-in Linux utility that contains useful information about your system's running processes and resource usage. Learn how to use the top command effectively...
Read more
How To Use shred Linux Command
March 31, 2021

Deleting a file in Linux or any other OS does not actually remove the data from the drive. The best practice is to use the shred command to permanently destroy sensitive data so that it cannot be...
Read more
How to Copy Files and Directories in Linux
December 28, 2023

Want to learn how to copy files in Linux OS? This guide will show you how to use the Linux commands to copy files and directories from the command line...
Read more