How to Use the sync Command in Linux?

In Linux, the sync command utility synchronizes the data from the cache to the permanent memory. The temporary data on Cache can corrupt or be lost in case of a sudden shutdown of the system. To avoid losing the important cache data, it’s better to sync it with the hard disk. Considering the importance of the sync command, this post will illustrate the working and usage of the sync command in Linux. The outline of this post is provided below:

Let’s start!

What is a sync Command in Linux?

Before using the sync command in Linux, let’s check the syntax and options/flags for the sync command.

The sync command syntax is given below:

$ sync [option] [file]...

Options Available in Sync Command

The following options are available with the “sync” command, which can be checked with the “sync –help” command:

-d –dataThis option syncs the file’s data only.
-f –file-systemIt allows to sync all the system data files (including metadata and other system files ) of a file.
–versionTo get the installed version.

The options and the syntax-related support of the sync command can be retrieved using the following command:

$ sync --help

Let’s get into the usage of the sync command.

How to Use the sync Command in Linux?

This section provides a detailed usage of the sync command with various examples:

Example 1: Sync User Cache Data

This command syncs the cache data to the hard disk. To sync all the data of the current user, use the following command:

$ sync

Example 2: Sync User File Sytems

When you give the “sudo” privileges to the sync command in Linux, it will sync all the mounted file systems on the hard disk:

$ sudo sync

To get the file systems of a user, execute the “$ df” command:

$ df

The above figure shows “57%” of data is mounted on the “/” root directory, and the partition is “/dev/sda3”.

Example 3: Sync File Data Only

The sync command provides the “d” or “–data” options to sync/copy the cache data of the file (excluding other data files, i.e., metadata) to the hard disk. To copy the data files of a particular file named “testfile1.txt”, the below command is exercised:

$ sync -d /home/itslinuxfoss/testfile1.txt

Example 4: Sync Multiple Files Data

To sync cache data of multiple files including a text file“/home/itslinuxfoss/testfile1.txt” and an mp4 file “~/Desktop/”, you can use the sync command as follows:

$ sync -d /home/itslinuxfoss/testfile1.txt ~/Desktop/Samplevideo.mp4

Example 5: Sync File System

The “f” or “–file-system” options allows the user to sync the data along with the file systems.  To sync the file systems of the “/home/itslinuxfoss/Documents”, we used the command:

$ sync -f /home/itslinuxfoss/Downloads/testfile2.txt

Example 6: Sync Multiple File Systems

The sync command can sync/copy multiple file systems with a single command. To sync the file systems for “/home/itslinuxfoss/test1.txt” and “~/Desktop/Samplevideo.mp4”, use the command as shown below:

$ sync -f /home/itslinuxfoss/file1.txt ~/Desktop/Samplevideo.mp4

Both the file systems are sync to the hard disk.

Example 7: Sync File System of a Specific Partition

We can sync the file system of a specific partition of the system. For instance, to sync the file system of the “/dev/sda3”, we executed this command:

$ sudo sync /dev/sda3

Example 8: Sync Directory and Sub-Directories

To sync a directory named “/var/log/syslog” and its sub-directories, the following command is used:

$ sudo sync /var/log/syslog

That’s all from this post!

Conclusion

The sync command is used to sync the cache data and file systems of a user. To sync all the cache data for the user, execute the “sync” command, and to sync all the file systems, use the “sudo sync” command. To sync the data of a single file, run the “sync -d <filepath>” and for file systems, use the “sync -f <filepath>” commands. Apart from these commands, you have learned a detailed usage of the sync command in Linux.