How to Use the sync Command on Linux

Use the sync command on Linux and synchronize your system’s data . The command is one of the most flexible utilities to sync data, whether in the volatile memory or in the physical storage.

When talking about the default scenario, it is important to understand that the Linux kernel writes data without worrying about syncing it. And although it might not be a thing to consider for many, the need for synchronization is significant. That’s when the sync command plays in.

Pre Requisites

Before you learn how to use the sync command in Linux, you must have all the utilities ready. Here is the list of requirements for invoking and employing the sync command:

  • A fully functional Linux system
  • Full access to the Terminal or Command Line utility
  • Knowledge of basic Terminal commands

Syntax

The fundamental structure or the basic syntax of sync command on Linux looks something like this:

$ sync [[-d | --data ] | [-f | --file-system]] [file ...]
sync command linux

How to Use the Sync Command on Linux

To start using the sync command on Linux, you’ll first need to understand the availability of different options and how they work. The following will be helping you with that.

-d | --data option: You can use the -d flag when the need is only syncing the file data without bothering much about the metadata. However, it does sync some metadata, but only limited to the essentials in maintaining the system’s overall consistency.

-f | --file option: The -f flag is invoked alongside the sync command when your want your system call to start syncing all the pending I/O. The sync is usually done with the concerned filesystem that contains the specified file(s).

Remember, you should never use the -f option when attempting to sync a device file like /dev/sdb, for instance because it will sync even the system root file.

--help option: If you’re willing to instruct your system so that it displays the sync command help message and exit, simply use the --help flag.

how to use sync command on linux

--version option: In case you don’t know, the –version flag helps display the concerned code’s version info.

Other Crucial Terminologies:

file: These are the names of the file which you want to sync

-“: A single dash signifies the standard input file descriptor. In most cases, it is treated as the usual file name.

--“: Unlike the single dash, double dashes represent the end of options.

Using the sync Command on Linux

Now that you’ve got enough idea about the general options, now’s the perfect time to learn how to use the sync command on Linux.

Sync All Cached File

One of the basic purposes of the sync command on Linux is to sync all the existing cached file data that corresponds to the current user. To do this, you don’t even need to have sudo privileges. Just run the following command inside the Terminal.

Input:

$ sync

Syncing All the Mounted Filesystem

Invoking the sync command can help sync all the mounted filesystems. But you do need sudo privileges for this.

Input:

$ sudo sync
how to use sync command on linux

Syncing Specific Files

As sync is a flexible command-line utility, it allows users even to specify the demand. To sync specific files, you’ll need to launch the Terminal and invoke the sync command together with the desired file names.

Suppose, you want to sync $HOME/ .bashrc and $HOME/mystorage/crucial/file. In that case, the required command should look something like this:

$ sync $HOME/ .bashrc $HOME/mystorage/crucial/file
how to use sync command on linux

Use the Sync Command on Linux for the File Data of Specific Files

To sync the file data alongside the metadata (in minimum volume), simply use the sync command and the -d flag command. 

For instance, let us consider four files named filename1, filename2, filename3, and filename4 under the $HOME directory. To do the requisites, you should run the following command:

$ sync -d $HOME/filename1 $HOME/filename2 $HOME/filename3 $HOME/filename4
sync specific files

Syncing File on Mounted Partition

For syncing any file on mounted storage, invoke the sync command and the desired directory. Keep in mind that having access to sudo privileges is crucial.

If you want to sync the files within the mounted partition /dev/sdc1, invoke the command as follows:

$ sudo sync /dev/sdc1
syncing file on mounted partition

Sync Command for Syncing Files and Subdirectories

To sync all the existing files along with the subdirectories, use the sync command followed by the desired directory. Let’s say  /var/lib/mycreatedlib is the desired directory, the command that you’ll need to run is:

$ sudo sync /var/lib/mycreatedlib

Syncing All the Files Using the sync Command on Linux

Arriving at this point, you should already know that specifying the filename and running the sync command together with it will sync the concerned file, but what if you need the entire file system to be synced? Well, for that, using the -f flag is necessary. The required command for this method is:

$ sudo sync -f [filename]
syncing all files

And with that, you now know how to use the sync command on Linux. In this article, we meticulously walked through all the appropriate situations where you can employ the sync command in the quickest way possible. And that marks the end of this article.

If this guide helped you, please share it.

Leave a Reply
Related Posts