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.
2. sync Command Syntax and Options
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:
- -f or –file: syncs the file system containing a specified file or directory
- -d or –directory: syncs a target directory and its contents
- –help: shows the help message for sync
- –version: shows the version of the sync command
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.
3. Syncing Entire File Systems
Generally, to sync the current file system, we run the sync command without any option:
$ sudo sync
Further, sync ensures that all pending write operations are flushed to the underlying storage devices.
4. Syncing Only Specific Files
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.
5. Syncing File Data Only
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.
6. Syncing a Specific Directory
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.
7. Syncing a File System With a Specific File
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.