Intro To 'sync' Command In Linux
2023-06-05 - By Robert Elder
I use the 'sync' command to ensure that any cached writes are committed to persistent storage:
sync
Here, I have an important piece of data that I want to back up to an external hard drive:

I use the 'cp' command to copy this data onto the external hard drive:
cp my-important-data.zip /external-drive/my-important-data.zip
As the 'cp' command runs, I no longer have control over the command prompt, but I can see that the destination file size is growing larger:

Eventually, the command prompt comes back and the destination file size stops increasing. 13 seconds later, I decide to unplug my external hard drive believing that the data has finished copying:

When I plug the drive back in, the file size is now smaller and the checksum doesn't match, indicating that the backup is corrupt:

That's why I always use the 'sync' command even if the drive activity light has stopped flashing:
sync
After using the 'sync' command, the data will finish copying for a few seconds, and then return control to the prompt. Now, the file sizes are correct and the checksums match!
And that's why the 'sync' command is my favourite Linux command.
![]() Intro To 'stty' Command In Linux
Published 2023-10-04 |
![]() $1.00 CAD |
![]() Intro To 'nproc' Command In Linux
Published 2023-07-15 |
![]() Intro To 'comm' Command In Linux
Published 2023-09-06 |
![]() How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
![]() A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
![]() A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
![]() Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|