Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to flushes file system buffers in the Linux operating system?
To synchronize cached writes to persistent storage, we use the sync command in the Linux operating system. This command ensures that data stored in memory buffers is written to disk, preventing data loss during unexpected shutdowns or system crashes.
The sync command synchronizes file data between volatile memory (RAM) and persistent storage devices like hard drives or SSDs. When applications write data, it is initially stored in kernel buffers for performance reasons. The sync command forces these buffers to be written to disk immediately.
How File System Buffers Work
Linux uses a write-back caching strategy where data is temporarily stored in memory buffers before being written to disk. This improves performance but creates a risk of data loss if the system crashes before the buffers are flushed.
Syntax
The general syntax of the sync command is as follows −
sync [OPTION] [FILE]...
Available Options
| Option | Description |
|---|---|
| -d, --data | Synchronize only file data, no unneeded metadata |
| -f, --file-system | Synchronize the file systems that contain files |
| --help | Displays a help message and then exits |
| --version | Shows version information and then exits |
Note: If one or more files are specified, sync will synchronize only those files or their containing file systems.
Examples
Synchronize All Cached Files
To synchronize all cached files system-wide, use sudo privilege −
$ sudo sync
The sync command works silently without displaying output on the terminal.
Synchronize Only File Data
To synchronize only file data without metadata −
$ sync -d /home/user/document.txt
Synchronize File Systems
To synchronize only the file systems containing specific files −
$ sync -f /home/user/document.txt
Check Version and Help
To display version information −
$ sync --version
To view help information −
$ sync --help
Common Use Cases
Before system shutdown − Ensure all data is written to disk
After critical file operations − Force immediate disk writes
Before removing external storage − Prevent data corruption
In system scripts − Guarantee data persistence
Conclusion
The sync command is essential for ensuring data integrity by flushing file system buffers to persistent storage. It prevents data loss during unexpected shutdowns and is particularly important before system maintenance or when working with critical files.
