UNIX/Linux administration: Which command is used to immediately flush filesystem buffers and force all pending writes to disk (i.e., write cached blocks to storage)?

Difficulty: Easy

Correct Answer: sync

Explanation:


Introduction / Context:
Modern UNIX/Linux systems aggressively cache disk writes in memory to improve performance. At times, an administrator needs to force these buffered writes to be committed to persistent storage, for example before removing power or unmounting a filesystem. The classic utility for this task is the sync command.



Given Data / Assumptions:

  • You are working on a UNIX or Linux system with standard core utilities available.
  • There are pending filesystem buffers (dirty pages) that have not yet been written to disk.
  • You want a user-level command that requests the kernel to flush these buffers.


Concept / Approach:
The kernel defers writes to batch I/O. The sync command asks the kernel to write all dirty filesystem caches (metadata and data) to disk devices. It does not guarantee hardware cache flush on every device, but it ensures the OS-level buffers are issued to storage. Running sync multiple times is harmless and sometimes used out of caution.



Step-by-Step Solution:

Run: syncOptionally run it more than once: sync; syncIf unmounting, first sync, then umount the filesystem to minimize data loss risk.For removable media, run sync before eject to ensure all writes are complete.


Verification / Alternative check:
Use tools like mount or lsof to ensure no open files before unmount. Review system logs (e.g., dmesg) to see any I/O errors. Some filesystems expose statistics (e.g., /proc/meminfo Dirty pages) that decrease after sync completes.



Why Other Options Are Wrong:
save: Not a standard UNIX command for flushing buffers. edbuff: Not a valid core utility. flush: Not a portable command for this purpose. None of the above: Incorrect because sync is the correct command.



Common Pitfalls:
Assuming sync powers off disks; it only requests write-out. Believing sync fixes hardware cache policies; device write caches may still hold data unless the kernel issues barriers. Always pair sync with proper unmount/eject procedures.



Final Answer:
sync

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion