{"id":687,"date":"2010-05-12T00:26:28","date_gmt":"2010-05-11T22:26:28","guid":{"rendered":"http:\/\/raftaman.net\/?p=687"},"modified":"2021-05-15T11:46:40","modified_gmt":"2021-05-15T09:46:40","slug":"extract-single-file-from-tar-archive","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=687","title":{"rendered":"Extract single file from tar archive"},"content":{"rendered":"<p>Sometimes, extracting a whole tar archive can be a waste of time (and temporarily disk space).<\/p>\n<p>To extract just a single file, you can use<\/p>\n<pre class=\"brush: bash; first-line: 0; title: ; notranslate\" title=\"\">\r\ntar -x file -zf archive.tar.gz -C \/tmp\/\r\n<\/pre>\n<p>This extracts the file <strong><code>file<\/code><\/strong> to <strong><code>\/tmp\/<\/code><\/strong><\/p>\n<p><\/p>\n<p>Of course, most of the time the relative path to the file is not as simple as in the example above:<\/p>\n<pre class=\"brush: bash; first-line: 0; title: ; notranslate\" title=\"\">\r\ntar -x path\/to\/file -zf archive.tar.gz -C \/tmp\/\r\n<\/pre>\n<p>This creates the file <strong><code>\/tmp\/path\/to\/file<\/code><\/strong>.<\/p>\n<p><\/p>\n<p>To avoid the creation of the file&#8217;s relative path (or parts of it) in the target directory, use the switch <strong><code>--strip-components<\/code><\/strong><\/p>\n<pre class=\"brush: bash; first-line: 0; title: ; notranslate\" title=\"\">\r\ntar -x path\/to\/file -zf archive.tar.gz -C \/tmp\/ --strip-components=2\r\n<\/pre>\n<p>This creates the file <strong><code>\/tmp\/file<\/code><\/strong><\/p>\n<p><\/p>\n<p>And finally, you can use stdout redirection to achieve the same result<\/p>\n<pre class=\"brush: bash; first-line: 0; title: ; notranslate\" title=\"\">\r\ntar -x path\/to\/file -zf archive.tar.gz -O &gt;\/tmp\/file\r\n<\/pre>\n<p>or even shorter<\/p>\n<pre class=\"brush: bash; first-line: 0; title: ; notranslate\" title=\"\">\r\ntar xfz archive.tar.gz path\/to\/file -O &gt;\/tmp\/file\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, extracting a whole tar archive can be a waste of time (and temporarily disk space). To extract just a single file, you can use tar -x file -zf archive.tar.gz -C \/tmp\/ This extracts the file file to \/tmp\/ Of course, most of the time the relative path to the file is not as simple as in the example above:&#8230; <a href=\"https:\/\/possiblelossofprecision.net\/?p=687\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[28],"class_list":["post-687","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bash"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=687"}],"version-history":[{"count":14,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":2686,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/687\/revisions\/2686"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}