{"id":14579,"date":"2026-11-05T19:15:12","date_gmt":"2026-11-05T19:15:12","guid":{"rendered":"https:\/\/commandlinux.com\/?p=14579"},"modified":"2026-11-05T19:41:27","modified_gmt":"2026-11-05T19:41:27","slug":"fsync","status":"publish","type":"post","link":"https:\/\/commandlinux.com\/man-page\/man2\/fsync\/","title":{"rendered":"FSYNC"},"content":{"rendered":"\n<link rel=\"stylesheet\" href=\"https:\/\/wordpress-541078-6199923.cloudwaysapps.com\/wp-content\/themes\/smart-mag\/man-style.css\"><script>document.title = \"FSYNC linux command man page\";<\/script><div class='man-page' style='max-width:900px;margin:40px auto;'>\n\n\n<A NAME=\"lbAB\">&nbsp;<\/A>\n<H2>NAME<\/H2>\n\nfsync, fdatasync &#8211; synchronize a file&#8217;s in-core state with storage device\n<A NAME=\"lbAC\">&nbsp;<\/A>\n<H2>SYNOPSIS<\/H2>\n\n<B>#include &lt;unistd.h&gt;<\/B>\n\n<P>\n<B>int fsync(int <\/B><I>fd<\/I><B>);<\/B>\n\n<P>\n<B>int fdatasync(int <\/B><I>fd<\/I><B>);<\/B>\n\n<P>\n\nFeature Test Macro Requirements for glibc (see\n<B>feature_test_macros<\/B>(7)):\n\n\n<P>\n<B>fsync<\/B>():\n\n_BSD_SOURCE || _XOPEN_SOURCE\n<BR>\n\n<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;\/*&nbsp;since&nbsp;glibc&nbsp;2.8:&nbsp;*\/&nbsp;_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L\n\n<BR>\n\n<B>fdatasync<\/B>():\n\n_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;199309L || _XOPEN_SOURCE&nbsp;&gt;=&nbsp;500\n<A NAME=\"lbAD\">&nbsp;<\/A>\n<H2>DESCRIPTION<\/H2>\n\n<B>fsync<\/B>()\n\ntransfers (&quot;flushes&quot;) all modified in-core data of\n(i.e., modified buffer cache pages for) the\nfile referred to by the file descriptor\n<I>fd<\/I>\n\nto the disk device (or other permanent storage device) so that all\nchanged information can be retrieved even after the system crashed or\nwas rebooted.\nThis includes writing through or flushing a disk cache if present.\nThe call blocks until the device reports that the transfer has completed.\nIt also flushes metadata information associated with the file (see\n<B>stat<\/B>(2)).\n\n<P>\nCalling\n<B>fsync<\/B>()\n\ndoes not necessarily ensure\nthat the entry in the directory containing the file has also reached disk.\nFor that an explicit\n<B>fsync<\/B>()\n\non a file descriptor for the directory is also needed.\n<P>\n<B>fdatasync<\/B>()\n\nis similar to\n<B>fsync<\/B>(),\n\nbut does not flush modified metadata unless that metadata\nis needed in order to allow a subsequent data retrieval to be\ncorrectly handled.\nFor example, changes to\n<I>st_atime<\/I>\n\nor\n<I>st_mtime<\/I>\n\n(respectively, time of last access and\ntime of last modification; see\n<B>stat<\/B>(2))\n\ndo not require flushing because they are not necessary for\na subsequent data read to be handled correctly.\nOn the other hand, a change to the file size\n(<I>st_size<\/I>,\n\nas made by say\n<B>ftruncate<\/B>(2)),\n\nwould require a metadata flush.\n<P>\nThe aim of\n<B>fdatasync<\/B>()\n\nis to reduce disk activity for applications that do not\nrequire all metadata to be synchronized with the disk.\n<A NAME=\"lbAE\">&nbsp;<\/A>\n<H2>RETURN VALUE<\/H2>\n\nOn success, these system calls return zero.\nOn error, -1 is returned, and\n<I>errno<\/I>\n\nis set appropriately.\n<A NAME=\"lbAF\">&nbsp;<\/A>\n<H2>ERRORS<\/H2>\n\n<DL COMPACT>\n<DT><B>EBADF<\/B>\n\n<DD>\n<I>fd<\/I>\n\nis not a valid open file descriptor.\n<DT><B>EIO<\/B>\n\n<DD>\nAn error occurred during synchronization.\n<DT><B>EROFS<\/B>, <B>EINVAL<\/B>\n\n<DD>\n<I>fd<\/I>\n\nis bound to a special file which does not support synchronization.\n<\/DL>\n<A NAME=\"lbAG\">&nbsp;<\/A>\n<H2>CONFORMING TO<\/H2>\n\n4.3BSD, POSIX.1-2001.\n<A NAME=\"lbAH\">&nbsp;<\/A>\n<H2>AVAILABILITY<\/H2>\n\nOn POSIX systems on which\n<B>fdatasync<\/B>()\n\nis available,\n<B>_POSIX_SYNCHRONIZED_IO<\/B>\n\nis defined in\n<I>&lt;unistd.h&gt;<\/I>\n\nto a value greater than 0.\n(See also\n<B>sysconf<\/B>(3).)\n\n\n\n\n<A NAME=\"lbAI\">&nbsp;<\/A>\n<H2>NOTES<\/H2>\n\nOn some UNIX systems (but not Linux),\n<I>fd<\/I>\n\nmust be a\n<I>writable<\/I>\n\nfile descriptor.\n<P>\nIn Linux 2.2 and earlier,\n<B>fdatasync<\/B>()\n\nis equivalent to\n<B>fsync<\/B>(),\n\nand so has no performance advantage.\n<P>\nThe\n<B>fsync<\/B>()\n\nimplementations in older kernels and lesser used filesystems\ndoes not know how to flush disk caches.\nIn these cases disk caches need to be disabled using\n<B>hdparm<\/B>(8)\n\nor\n<B>sdparm<\/B>(8)\n\nto guarantee safe operation.\n<A NAME=\"lbAJ\">&nbsp;<\/A>\n","protected":false},"excerpt":{"rendered":"<p>synchronize a file&#8217;s in-core state with storage device<\/p>\n","protected":false},"author":1,"featured_media":14594,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-14579","post","type-post","status-publish","format-standard","has-post-thumbnail","category-man-page"],"_links":{"self":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/14579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/comments?post=14579"}],"version-history":[{"count":1,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/14579\/revisions"}],"predecessor-version":[{"id":14584,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/14579\/revisions\/14584"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media\/14594"}],"wp:attachment":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media?parent=14579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/categories?post=14579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/tags?post=14579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}