Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
The Linux sync command synchronizes cached data to permanent storage. This data includes modified superblocks, modified inodes, delayed reads and writes, and others. sync uses several system calls:
For example, the sync command utilizes the sync() system call to write all buffered modifications to file data and metadata to an underlying storage device.
As a Linux systems administrator or developer, understanding the sync command can be crucial for efficient file synchronization. Additionally, sync can be helpful after crashes or when the file system becomes corrupted.
In this tutorial, we’ll explore the various aspects of the sync command. Also, we’ll see how we can use sync in different scenarios.
Before we dive into the details, let’s start with the basic syntax of the sync command:
sync [OPTION] [File] ...
Overall, the sync command requires no arguments. However, there are some options available to control its behavior.
In fact, we can summarize all flags in a short list:
Basically, the first two options combine with any arguments to provide functionality. For example, they can help us to sync specific files, directories, or file systems.
Generally, to sync the current file system, we run the sync command without any option:
$ sync
Also, if we run sync with sudo privileges, it syncs all mounted systems:
$ sudo sync
Further, sync ensures that all pending write operations are flushed to the underlying storage devices.
Often, we only want to sync specific files or directories instead of the whole file system. We can do this by specifying the file or directory as an argument to sync:
$ sync /path/to/file1 /path/to/file2
This operation syncs the specific files only. These files can be in the same or different directories.
By default, the sync command synchronizes both file content and metadata. However, in some cases, we only need to sync content to enhance performance. Further, we can do this using the -d or –data option:
$ sync -d
This operation ensures that only the file data is synchronized. Also, -d avoids any unnecessary metadata updates.
Let’s suppose we want to sync a whole directory along with its contents. For this, we can supply the path of the directory as an argument to sync:
$ sync /path/to/directory
The command above syncs the specified directory. It also checks any changes within it are written to the storage.
In some cases, we may need to sync a file system that contains a specific file. We can do this using the -f or –file-system option:
$ sync -f /path/to/file
As a result, the sync command syncs the file system containing the given file. Thus, we don’t need to first locate the file system object before running sync.
The sync command is a powerful tool for file synchronization in Linux systems.
In this article, we’ve seen its syntax and various options. We also learned how to use sync to synchronize all file systems, specific files, directories, and file systems containing files. On the whole, understanding and effectively using the sync command can be very important.