{"id":10797,"date":"2024-05-01T09:53:34","date_gmt":"2024-05-01T09:53:34","guid":{"rendered":"https:\/\/youstable.com\/blog\/?p=10797"},"modified":"2024-05-01T09:53:35","modified_gmt":"2024-05-01T09:53:35","slug":"tar-command-50","status":"publish","type":"post","link":"https:\/\/www.youstable.com\/blog\/tar-command-50","title":{"rendered":"Mastering the TAR Command: 50 Practical Use Cases for Linux"},"content":{"rendered":"\n<p>As a Linux System Administrator at YouStable,  <code><strong>tar<\/strong><\/code> <strong>command<\/strong> is essential for efficiently managing files and directories. <code>tar<\/code>, short for tape archive, is a powerful utility used to create, maintain, manipulate, and extract files stored in a <a href=\"https:\/\/youstable.com\/blog\/ssd-dedicated-server-vs-hdd-dedicated\/\" data-type=\"post\" data-id=\"9746\" target=\"_blank\" rel=\"noopener\">tape or disk<\/a> archive.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/youstable.com\/blog\/wp-content\/uploads\/2024\/02\/Linux-tar-Comand.png\" alt=\"tar commands for linux\" class=\"wp-image-10799\" style=\"width:492px;height:auto\" srcset=\"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2024\/02\/Linux-tar-Comand.png 1024w, https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2024\/02\/Linux-tar-Comand-768x576.png 768w, https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2024\/02\/Linux-tar-Comand-700x525.png 700w, https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2024\/02\/Linux-tar-Comand-150x113.png 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"basic-operations\">Basic Operations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Creating a New Archive:<\/strong> Create an <a href=\"https:\/\/youstable.com\/blog\/knowledgebase\/extract-tar-gz\/\" data-type=\"manual_kb\" data-id=\"10777\" target=\"_blank\" rel=\"noopener\">archive<\/a> file from files or directories.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar file1 directory1\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Viewing an Archive Content:<\/strong> List the contents of an archive without extracting it. <\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -tvf archive.tar\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Extracting an Archive:<\/strong> Extract the contents of an archive to the current directory.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -xvf archive.tar\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Appending Files to Archive:<\/strong> Add new files to an existing archive.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -rvf archive.tar newfile\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Deleting Files from Archive:<\/strong> Remove files from an archive (GNU tar).arduino<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar --delete -f archive.tar file_to_delete\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"advanced-archiving\">Advanced Archiving<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"6\">\n<li><strong>Creating a Compressed Archive:<\/strong> Use compression algorithms like <strong>gzip<\/strong> or <strong>bzip2.<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -czvf archive.tar.gz directory1 # gzip \ntar -cjvf archive.tar.bz2 directory1 # bzip2\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"6\">\n<li><strong>Extracting a Compressed Archive:<\/strong> Extract files from a compressed archive<strong>.<\/strong><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -xzvf archive.tar.gz # gzip \ntar -xjvf archive.tar.bz2 # bzip2\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"6\">\n<li><strong>Preserving Permissions:<\/strong> Maintain file permissions while archiving.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cpvf archive.tar directory1\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"6\">\n<li><strong>Incremental Backups:<\/strong> Create <strong><a href=\"https:\/\/youstable.com\/blog\/knowledgebase\/how-to-configure-ssh-backup-via-jetbackup-in-directadmin\/\" data-type=\"manual_kb\" data-id=\"9276\" target=\"_blank\" rel=\"noopener\">backups<\/a><\/strong> only of changed files since the last backup.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf backup.tar --listed-incremental=\/path\/to\/snapshot.file directory1\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"6\">\n<li><strong>Creating Split Archives:<\/strong> Split an archive into multiple parts.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf - directory1 | split -b 100M - archive.part\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"file-selection\">File Selection<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"11\">\n<li><strong>Excluding Files:<\/strong> Create an archive excluding specific files.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar --exclude=&#039;*.txt&#039; directory1\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"11\">\n<li><strong>Including Files:<\/strong> Archive only files matching a pattern.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar --include=&#039;*.php&#039; directory1\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"11\">\n<li><strong>Archiving Using a File List:<\/strong> Create an archive based on a list of files.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar -T filelist.txt\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"11\">\n<li><strong>Wildcards in File Selection:<\/strong> Use wildcards to specify files or directories.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar directory1\/*.txt\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"security-and-integrity\">Security and Integrity<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"15\">\n<li><strong>Encrypting Archives:<\/strong> <a href=\"https:\/\/youstable.com\/blog\/tag\/wordpress-password-encryption\/\" data-type=\"post_tag\" data-id=\"876\" target=\"_blank\" rel=\"noopener\">Encrypt tar archive<\/a> using <code>gpg<\/code>.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf - directory1 | gpg -c &gt; archive.tar.gpg\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"15\">\n<li><strong>Verifying Archive Integrity:<\/strong> Check the integrity of an archive.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar directory1 --verify\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"networking-and-remote-operations\">Networking and Remote Operations<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"17\">\n<li><strong>Creating Archives from Remote Directories:<\/strong> Use SSH to create an archive of a remote directory.sql<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>ssh user@remote 'tar -cvf - \/path\/to\/directory' > local_archive.tar<\/code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" start=\"17\">\n<li><strong>Extracting Archives to Remote Directories:<\/strong> Extract an archive directly to a remote directory.sql<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>tar -xvf archive.tar | ssh user@remote 'cd \/destination &amp;&amp; tar -xvf -'<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"archiving-strategies\">Archiving Strategies<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"19\">\n<li><strong>Full System Backup:<\/strong> Create a complete backup of the filesystem.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvpzf full_backup.tar.gz \/\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"19\">\n<li><strong>Home Directory Backup:<\/strong> Backup the home directory.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvzf home_backup.tar.gz \/home\/user\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"19\">\n<li><strong>Database Backup:<\/strong> Archive a database dump file.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>mysqldump -u user -p database_name | gzip > database.sql.gz tar -cvzf database_backup.tar.gz database.sql.gz<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading has-text-align-center\" class=\"wp-block-heading has-text-align-center\" id=\"miscellaneous\">Miscellaneous<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"22\">\n<li><strong>Archiving Special Files:<\/strong> Archive device files and special files.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -cvf archive.tar \/dev\/sdx\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"22\">\n<li>Comparing Archive Content with the Filesystem: Check differences between the archive and filesystem.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar -dvf archive.tar\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\" start=\"22\">\n<li><strong>Preserving SELinux Contexts:<\/strong> Archive files with their SELinux context.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntar --selinux -cvf archive.tar\n<\/pre><\/div>\n\n\n<p>These basic TAR commands are essentials we frequently use. If you have a particular scenario in mind, feel free to comment below. We&#8217;ll strive to provide a tailored command response as swiftly as possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a Linux System Administrator at YouStable, tar command is essential for efficiently managing files and directories. tar, short for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10995,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1134],"tags":[],"class_list":["post-10797","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"acf":[],"featured_image_src":"https:\/\/www.youstable.com\/blog\/wp-content\/uploads\/2024\/02\/What-is-TAR-Command-Use-Cases.jpg","author_info":{"display_name":"YouStable","author_link":"https:\/\/www.youstable.com\/blog\/author\/youstable"},"_links":{"self":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/10797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/comments?post=10797"}],"version-history":[{"count":2,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/10797\/revisions"}],"predecessor-version":[{"id":13893,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/posts\/10797\/revisions\/13893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media\/10995"}],"wp:attachment":[{"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/media?parent=10797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/categories?post=10797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.youstable.com\/blog\/wp-json\/wp\/v2\/tags?post=10797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}