A Step-By-Step Guide to Using the Linux Sync Command

Use the Linux Sync command if you anticipate a power outage

The Sync command writes files to persistent storage.
The Sync command supports two switches.

Managing the Linux operating system isn't particularly clear-cut, but learning the commands that instruct the system to perform basic operations is a big step in the right direction. The sync command writes any data that is buffered in the computer's memory to disk. 

Why Use the Sync Command

To improve performance, a computer often keeps data in its memory rather than write it to disk because the RAM is much faster than the hard disk. This approach is fine until there is a computer crash. When a Linux machine experiences an unplanned shutdown, all the data that was held in memory is lost, or the file system is corrupted. The sync command forces everything in temporary memory storage to be written to a persistent file storage (like a disk) so none of the data is lost. 

When to Use the Sync Command

Usually, computers are shut down in an organized manner.  If the computer is going to be shut down or the processor stopped in an unusual manner, such as when you're debugging kernel code or in the event of a possible power outage, the sync command forces an immediate transfer of the data in memory to disk. Because modern computers have potentially large caches, when you use the sync command, wait until all the LEDs that indicate activity stop blinking before turning off the power on the computer.

Sync Syntax

sync [option] [file]  

Options for the sync Command

Options for the Sync Command are:

  • --help displays any available help and then exits
  • --version displays version information and exits
  • --data (or -d) syncs only file data without the metadata that supports it
  • --file-system (or -f) also syncs the file systems that contain the referenced files

Considerations

It's not common to manually invoke sync. Most often, this command is run before you execute some other command that you suspect could destabilize the Linux kernel, or if you believe that something bad is about to happen (e.g., you're about to run out of battery on your Linux-powered laptop) and you don't have time to execute a full system shutdown.

When you halt or restart the system, the operating system automatically syncs data in memory with persistent storage, as needed.