{"id":4668,"date":"2023-07-22T10:02:41","date_gmt":"2023-07-22T10:02:41","guid":{"rendered":"https:\/\/arzhost.com\/blogs\/?p=4668"},"modified":"2024-12-03T20:55:51","modified_gmt":"2024-12-03T15:55:51","slug":"tar-command-in-linux","status":"publish","type":"post","link":"https:\/\/arzhost.com\/blogs\/tar-command-in-linux\/","title":{"rendered":"Tar Command in Linux: A Complete Guide with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Importance in File Archiving and Compression<\/h2>\n\n\n\n<p>In Linux systems, the most popular archiving tool is the GNU tar programmer (short for Tape Archiver). The Tar Command in Linux aids in creating, extracting, and listing archive contents and is directly accessible in the terminal.<\/p>\n\n\n\n<p>The tool is straightforward and offers a wide range of advantageous choices for managing backups, compressing data, and extracting a raw installation.<\/p>\n\n\n\n<p>This guide from our <a href=\"https:\/\/arzhost.com\/blogs\/\"><strong>Blog<\/strong><\/a> demonstrates the use of the <span style=\"color: #000000;\"><strong>Tar Command in Linux<\/strong> <\/span>using examples and all of the available options.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A system running Linux.<\/li>\n\n\n\n<li>Access to the command line\/terminal.<\/li>\n\n\n\n<li>A file or files for testing the command.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Tar Command Syntax<\/strong>: How to Structure Tar Command Lines<\/h2>\n\n\n\n<p><u>The general syntax for the tar command is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar 'operation mode' 'options' 'archive' 'file' or 'location'<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>1: Which operation is performed on the files is indicated by the operation mode.<\/u><\/p>\n\n\n\n<p><u>2: (Conception, exploitation, etc.) The command only permits and necessitates one activity.<\/u><\/p>\n\n\n\n<p><u>3: Options are optional and change the operation mode. There is no restriction on the variety of choices.<\/u><\/p>\n\n\n\n<p><u>4: The file name and extension are the archives.<\/u><\/p>\n\n\n\n<p><u>5: A list of file names separated by spaces is used for extraction, compression, or wildcard matching.<\/u><\/p>\n\n\n\n<p><strong><u>The operations and options can be used in one of three syntax types:<\/u><\/strong><\/p>\n\n\n\n<p><u>1: Traditional style, clustered together without any dashes.<\/u><\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cfv &lt;archive> &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>2: UNIX short option style, using a single dash and clustered options:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar -cfv &lt;archive> &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Alternatively, a dash before each option:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar -c -f -v &lt;archive> &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>3: GNU long-option style with a double-dash and a descriptive option name:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar --create --file &lt;archive> --verbose &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p>All three styles can be used in a single Tar Command in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Linux\"><strong>Linux<\/strong><\/a>.<\/p>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-ea34b2e4 wp-block-button uag-blocks-common-selector\" style=\"--z-index-desktop:479;;--z-index-tablet:undefined;;--z-index-mobile:undefined;\"><div class=\"uagb-cta__wrap\"><h2 class=\"uagb-cta__title\"><a href=\"https:\/\/arzhost.com\/vps\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/vps\/\">High-Performance VPS Hosting: Instant Activation. Join Now!<\/a><\/h2><p class=\"uagb-cta__desc\">Dedicated Resources, Affordable Price: Sign Up for VPS Hosting at <strong>$12.50\/month<\/strong> with <a href=\"https:\/\/arzhost.com\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/\">ARZ Host<\/a>!<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/arzhost.com\/vps\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_self\" rel=\"noopener noreferrer\">Read More<\/a><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>List of Frequently Used Tar Options: Common Tar Command Options<\/strong><\/h2>\n\n\n\n<p>The following table outlines the commonly used tar operations and options.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Command<\/strong><\/td><td><strong>Role<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>&#8211;create<br>\n-c<\/td><td>Operation<\/td><td>Creates a new archive.<\/td><\/tr><tr><td>&#8211;list<br>\n-t<\/td><td>Operation<\/td><td>Lists an archive&#8217;s contents.<\/td><\/tr><tr><td>&#8211;extract<br>\n-x<\/td><td>Operation<\/td><td>Extract one or more items from an archive.<\/td><\/tr><tr><td>&#8211;append<br>\n-r<\/td><td>Operation<\/td><td>Appends files to an existing archive.<\/td><\/tr><tr><td>&#8211;concatenate<br>\n-A<\/td><td>Operation<\/td><td>Appends archives to an existing archive.<\/td><\/tr><tr><td>&#8211;compare<br>\n&#8211;diff<br>\n-d<\/td><td>Operation<\/td><td>Compares archive members with files on the system.<\/td><\/tr><tr><td>&#8211;delete<\/td><td>Operation<\/td><td>Deletes a member from the archive.<\/td><\/tr><tr><td>&#8211;update<br>\n-u<\/td><td>Operation<\/td><td>Updates archive with new files only if they are not in the archive and are newer than existing files.<\/td><\/tr><tr><td>&#8211;file=&lt;archive&gt;<br>\n-f &lt;archive&gt;<\/td><td>Option<\/td><td>Specifies the file.<\/td><\/tr><tr><td>-C<\/td><td>Option<\/td><td>Changes the directory.<\/td><\/tr><tr><td>&#8211;verbose<br>\n-v<\/td><td>Option<\/td><td>Shows the file&nbsp;<strong>tar<\/strong>&nbsp;works on while running.<\/td><\/tr><tr><td>&#8211;wildcard<\/td><td>Option<\/td><td>Renders wildcard search options.<\/td><\/tr><tr><td>&#8211;bzip2<br>\n-j<\/td><td>Option<\/td><td>Read or write compressed archives through bzip2 format.<\/td><\/tr><tr><td>&#8211;gzip<br>\n-z<\/td><td>Option<\/td><td>Read or write compressed archives through gzip format.<\/td><\/tr><tr><td>&#8211;xz<br>\n-J<\/td><td>Option<\/td><td>Read or write compressed archives through xz format.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Follow the examples in the next section to learn how to work with tar.<\/p>\n\n\n\n<p><strong>Related Article: <a href=\"https:\/\/arzhost.com\/blogs\/different-between-linux-server-vs-windows-server\/\">Difference between Linux Server and Windows Server<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Practical Examples of Tar Commands: Detailed Instructions<\/strong><\/h2>\n\n\n\n<p>The criteria for the examples below are as follows:<\/p>\n\n\n\n<p><u>1: make a directory called &#8220;tar examples&#8221; and go there:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cd tar examples and then run \"mkdir tar examples\"<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>2: Create the following directory, files in tar examples, and enter it:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cd files and mkdir files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>3: Produce documents to fill the files directory:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>open the file \"0.100.txt\"<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Use: to get back to the parent directory.<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cd.<\/code><\/li>\n<\/ul>\n\n\n\n<p>All the examples below work from the tar examples directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1: Creating an Archive with Tar<\/strong><\/h2>\n\n\n\n<p>Depending on the type of archive, a certain syntax must be used. Use tar with the -c or \u2014create command to generate an archive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Create a Basic Tar Archive<\/u><\/strong>: Step-by-Step Guide<\/h3>\n\n\n\n<p><u>Create a tar archive, often known as a tarball, by using:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf \"archive name\"<\/code><\/li>\n\n\n\n<li><code>tar files or directories<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>As an illustration, backup the files directory:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><u><code>files in tar cf format<\/code><\/u><\/li>\n<\/ul>\n\n\n\n<p><u>Each file added to the archive is listed in the output. To see the newly created file, show<\/u> <u>the directory contents. the tar archive<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>files in tar cf format<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Each file added to the archive is listed in the output. To see the newly created file, show the directory contents. the tar archive<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ls \u2013l<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Creating a tar.gz Compressed Archive<\/u><\/strong><\/h3>\n\n\n\n<p><u>Add the -z option to create a compressed GNU zip (gzip) file:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar czf &lt;archive name>. tar.gz &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar czf files.tar.gz files<\/code><\/li>\n<\/ul>\n\n\n\n<p>The file size is smaller than a regular tarball file and the original directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Creating a tar.bz2 Compressed Archive<\/u><\/strong><\/h3>\n\n\n\n<p>The bzip2 is a file compression program and an alternative to gzip.<\/p>\n\n\n\n<p><u>To create a tar.bz2 file, add the -j tag:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cjf &lt;archive name>. tar.bz2 &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cjf files.tar.bz2 files<\/code><\/li>\n<\/ul>\n\n\n\n<p>The bzip2 has a greater compression rate and takes longer than gzip.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Creating a tar.xz Compressed Archive<\/u><\/strong><\/h3>\n\n\n\n<p><u>Use the -J tag to compress archives in the tar.xz format:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cJf &lt;archive name>.tar. xz &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cJf files.tar. xz files<\/code><\/li>\n<\/ul>\n\n\n\n<p>Wish to Use mkdir Command? Read our Detailed Guide on <a href=\"https:\/\/arzhost.com\/blogs\/use-mkdir-command-to-make\/\"><strong>How to Use the mkdir Command in Linux<\/strong><\/a> to Make or Create a Linux Directory<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2: Eliminating Files Following Archive Creation<\/strong><\/h2>\n\n\n\n<p><u>Use the \u2014remove-files option at the end to delete the files from disc after archiving:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf \"archive\" \"file\" \"location\" \u2014remove-files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>As an illustration, execute the following Tar Command in Linux to build a tar archive of the files directory and delete it from the disc:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>files in tar cf format \u2014remove-files<\/code><\/li>\n<\/ul>\n\n\n\n<p>To ensure the procedure is successful, check the directory&#8217;s contents.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3: Extracting Files from an Archive<\/strong><\/h2>\n\n\n\n<p>The -x or \u2014 extract operation with tar is used to extract from an archive or compressed archive. The extra choices are based on the file type and the location where tar should extract the parts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Extracting from the tar archive<\/u><\/strong>: Basic Extraction Commands<\/h3>\n\n\n\n<p><u>When extracting from a tar archive, the general syntax is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Tar XF. Tar<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>The components are all extracted to the current directory by default when using tar. Add the -C option and the path to the command to specify where to extract the components:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>xfC tar xfC.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Run the following command, for instance, to create the directory extracted tar and extract the files from files.tar.<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xfC files.tar extracted tar and mkdir extracted tar<\/code><\/li>\n<\/ul>\n\n\n\n<p>There is no confirmation message output by the command. Verify the directory&#8217;s contents to ensure that the components were correctly extracted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Extracting from a tar.gz Archive<\/u><\/strong><\/h3>\n\n\n\n<p><u>Use the -z option to extract a tar.gz file:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xzf .tar.gz<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>The command extracts the contents in the current directory. Add the -C option to specify the location:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xzfC .tar.gz<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, create a new directory named extracted_gz and extract the files.tar.gz contents:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mkdir extracted_gz &amp;&amp; tar xzfC files.tar.gz extracted_gz<\/code><\/li>\n<\/ul>\n\n\n\n<p>The tar.gz compressed archives take the least time to extract when compared to other compression formats.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Extracting from a tar.bz2 Archive<\/u><\/strong><\/h3>\n\n\n\n<p><u>To extract files from a tar.bz2 compressed archive into the current directory, use:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xjf .tar.bz2<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Extract tar.bz2 archives into a specific directory with:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xjfC tar.bz2<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, create a directory and extract the contents from files.tar.bz2:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mkdir extracted_bz2 &amp;&amp; tar xjfC files.tar.bz2 extracted_bz2<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong><u>Extracting from a tar.xz Archive<\/u><\/strong><\/h3>\n\n\n\n<p><u>Add the -J option to extract from tar. xz compressed archives. The syntax to extract in the current directory is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xJf .tar.xz<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>To extract the contents to a specific directory, use the -C option and add the path:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xJfC .tar. xz<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>As an example, create a directory and extract the files.tar. xz contents:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>mkdir extracted_gz &amp;&amp; tar xJfC files.tar. xz extracted_gz<\/code><\/li>\n<\/ul>\n\n\n\n<p>The xz compression format is the middle ground between gz and bz2 when it comes to extraction time.<\/p>\n\n\n\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/s7HQjNhzNxI\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4: Commands for Rewriting File Ownership: Adjusting Permissions<\/strong><\/h2>\n\n\n\n<p>When the names of files in the working directory and the archive have the same names, tar overwrite controls handle the situation.<\/p>\n\n\n\n<p><u>1: These are the three potential overwrite actions:<\/u><\/p>\n\n\n\n<p><u>Files in the working directory should be overwritten:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf \"archive\", \"optional file(s), or (locations)\" \u2014overwrite<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>2: Refrain from replacing files in the working directory:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf \"archive\", \"optional file(s), or (locations)\" \u2014keep-old-files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Tar does not extract the files if they already exist.<\/u><\/p>\n\n\n\n<p><u>3: Only extract files if they are more recent than the ones already present.<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf \"archive\", \"optional file(s), or (locations)\" \u2014keep-newer-files<\/code><\/li>\n<\/ul>\n\n\n\n<p>Tar does not extract files if the working directory&#8217;s files are more recent or the same age.<\/p>\n\n\n\n<p>Learn to <a href=\"https:\/\/arzhost.com\/blogs\/create-symbolic-links-in-linux\/\"><strong>Create Symbolic Links in Linux with this Easy to Follow Guide<\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5: Commands for Viewing Archive Contents: Listing Archive Contents<\/strong><\/h2>\n\n\n\n<p><u>Use the following command to list an archive&#8217;s contents:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf &lt;archive><\/code><\/li>\n<\/ul>\n\n\n\n<p>The option works for any file extension containing tar.<\/p>\n\n\n\n<p><u>For example, list the files and directories in the files.tar archive:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar.gz<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output lists all contents stored in the archive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6:\u00a0Finding Files in an Archive<\/strong>: Steps to Search for Specific Files<\/h2>\n\n\n\n<p>There are two ways to locate specific content using tar:<\/p>\n\n\n\n<p><u>1: The -t option to list files in an archive is handy for locating specific files. Add the file name (or names) after the command:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf &lt;archive> &lt;file(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, to locate file50.txt in the files.tar.gz archive, run:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar.gz file\/file50.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The option requires knowing the possible path to the file.<\/p>\n\n\n\n<p><u>2: Use the tar together with the grep command to filter the output:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf &lt;archive> | grep &lt;file(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar.gz | grep file50.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The option doesn&#8217;t require knowing the possible path to the file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7: Find Multiple Files in an Archive<\/strong>: How to Locate Multiple Files Efficiently<\/h2>\n\n\n\n<p><u>Use the &#8211;wildcards option to match multiple file instances. For example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar.gz --wildcards file\/files5*.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>Apply wildcard matching when files have a similar name, or for filtering a certain file type.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8: Exclude Files When Creating an Archive<\/strong>: Best Practices for File Exclusion<\/h2>\n\n\n\n<p><u>To exclude certain files from the archive during creation, add the following option:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf &lt;archive> --exclude='&lt;pattern>' &lt;files(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, create an archive from the files directory and exclude all .txt files:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf files.tar --exclude='*.txt' files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>List the archive contents:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output shows no .txt files, only the files directory in the archive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9: Extracting a Single File from an Archive: Commands for Single File<\/strong><\/h2>\n\n\n\n<p>Avoid extracting the whole archive if you need one or several files.<\/p>\n\n\n\n<p><u>To get a single file from an archive:<\/u><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2022\/12\/Extract-a-Single-File-from-the-Archive.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"364\" height=\"243\" src=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2022\/12\/Extract-a-Single-File-from-the-Archive.png\" alt=\"Extract a Single File from the Archive\" class=\"wp-image-4671\" srcset=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2022\/12\/Extract-a-Single-File-from-the-Archive.png 364w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2022\/12\/Extract-a-Single-File-from-the-Archive-300x200.png 300w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-ea34b2e4 wp-block-button uag-blocks-common-selector\" style=\"--z-index-desktop:479;;--z-index-tablet:undefined;;--z-index-mobile:undefined;\"><div class=\"uagb-cta__wrap\"><h2 class=\"uagb-cta__title\"><a href=\"https:\/\/arzhost.com\/lifetime-web-hosting\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/lifetime-web-hosting\/\">Pay Once, Host for Life &#8211; Lifetime Hosting at Unbeatable Price!<\/a><\/h2><p class=\"uagb-cta__desc\">Host for Life, Pay Just Once &#8211; Grab Lifetime Web Hosting with a Bonus Domain!<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/arzhost.com\/lifetime-web-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_self\" rel=\"noopener noreferrer\">Read More<\/a><\/div><\/div>\n\n\n\n<p><u>1: List the contents and check if the file exists:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar | grep file100.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output prints the path to the file needed for the next step.<\/p>\n\n\n\n<p><u>2: Extract the specific file with:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf files.tar files\/file100.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The Tar Command in Linux only includes the specified file when creating the directory files. By adding the proper tag, repeat these procedures to extract a single file from compressed archives. For instance, to pull from a tar.gz file, use the -z option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10: Using the Verbose Option: Benefits of Using Verbose Output<\/strong><\/h2>\n\n\n\n<p>The verbose option displays additional information after running a tar command. Add -v or &#8211;verbose to any operation to see the result.<\/p>\n\n\n\n<p><u>For example, create a tar.gz file and add -v:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar czfv files.tar.gz files<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output shows each file as it is added to the archive.<\/p>\n\n\n\n<p><u>Some tar commands show additional information when you add the -v tag twice. For example, try to add files to an archive with -vv:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar czfvv files.tar.gz files<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output prints a long listing format and looks similar to running the ls -l command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11: Deleting Files from an Archive<\/strong>: Commands for File Deletion<\/h2>\n\n\n\n<p><u>To delete from the archive, locate the file you want to remove, for example:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tf files.tar | grep file100.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Then, remove the file using the &#8211;delete tag:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar --delete -f files.tar files\/file100.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The delete option does not work on compressed file formats.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12: Appending Files to an Existing Archive: Steps to Add Files<\/strong><\/h2>\n\n\n\n<p><u>Append files to an existing archive using the -r tag. The syntax is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar rf &lt;archive name>.tar &lt;file(s) or location(s)><\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, append the compressed files.tar.gz file to the files.tar archive:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar rf files.tar files.tar.gz<\/code><\/li>\n<\/ul>\n\n\n\n<p>Already compressed archives cannot be updated. So the syntax only works for tarball files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>13: Combining Multiple Archives<\/strong>: Merge Tar Archives<\/h2>\n\n\n\n<p><u>Use the &#8211;concatenate or -A option to combine multiple archives. The basic syntax is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar Af<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>As an example, copy the existing files.tar file using the cp command:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cp files.tar files_copy.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Next, concatenate the two archives:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar Af files.tar files_copy.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p>To confirm the concatenation worked, check the file size. Have file Permission problems? check out How to <a href=\"https:\/\/arzhost.com\/blogs\/change-file-permissions-recursively-chmod-linux\/\"><strong>Change File Permissions with chmod in Linux<\/strong><\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>14: Difference Between Archive and Individual Files<\/strong><\/h2>\n\n\n\n<p><u>To check the difference between an archive and files on disk, use the -d tag:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar df<\/code><\/li>\n<\/ul>\n\n\n\n<p>The command looks for identical items and contrasts them with the contents of the archive. The option ignores newly inserted files and only looks for existing files.<\/p>\n\n\n\n<p><strong><u>The steps below show how to use the -d, &#8211;diff, or &#8211;compare tag with tar:<\/u><\/strong><\/p>\n\n\n\n<p><u>1: Create a tar archive:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf files.tar files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>2: Compare the archive with the existing directory:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar df files.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p>The output does not display anything, meaning there is no difference between the existing files.<\/p>\n\n\n\n<p><u>3: Add text to an existing file in the files directory:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>echo 'Hello' >> files\/file0.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>4: Compare the archive to the existing directory again:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar df files.tar<\/code><\/li>\n<\/ul>\n\n\n\n<p>This time, the report reveals variations in the size and modification time for a particular file. Comparing sheds light on any system modifications performed after the archive was created.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>15: Updating Files within an Archive: Best Practices for File Updates<\/strong><\/h2>\n\n\n\n<p><u>Update the existing files in the archive with a newer version from disk with the -u option:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar uf<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, update the files.tar archive with a changed text file:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar uf files.tar files<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Check the tar contents for the changed file:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar tfv files.tar | grep files0.txt<\/code><\/li>\n<\/ul>\n\n\n\n<p>The command updates the archive with changed files without any overwrites.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>16: Modifying File Timestamps<\/strong>: Commands for Timestamp Adjustment<\/h2>\n\n\n\n<p><u>Tar offers various options to modify the file&#8217;s timestamp. Set a custom date when creating an archive by adding the &#8211;mtime option and providing a date:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf --mtime=YYYY-MM-DD<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, create an archive and set the date to January 1st, 1999:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf files.tar files --mtime=1999-01-01<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Alternatively, extract the files with the current date and time:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf files.tar -m<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>A useful feature when working with time is filtering files modified after a specific date. For example, to extract files created after a date, use the &#8211;newer-mtime option and add the date:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf --newer-mtime=YYYY-MM-DD<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>17: Managing File Permissions in an Archive: Adjusting Permissions<\/strong><\/h2>\n\n\n\n<p>There are two possible ways to control file permissions with tar when extracting an archive:<\/p>\n\n\n\n<p><u>1: Preserve original permissions:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf --preserve-permissions<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>The permissions are as stated in the file before archive creation.<\/u><\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p><u>2: Modify the permissions to the default umask value:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf --no-same-permissions<\/code><\/li>\n<\/ul>\n\n\n\n<p>The files take on the default Linux permissions.<\/p>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-f75fffa7 wp-block-button uag-blocks-common-selector\" style=\"--z-index-desktop:479;;--z-index-tablet:undefined;;--z-index-mobile:undefined;\"><div class=\"uagb-cta__wrap\"><h2 class=\"uagb-cta__title\"><a href=\"https:\/\/arzhost.com\/web-hosting\/\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\/web-hosting\/\">The Clock&#8217;s Ticking! 24 Hours Left to Claim up to 90% Off<\/a><\/h2><p class=\"uagb-cta__desc\">Seize the Savings Now\u2014Lock in Your Discount at <a href=\"https:\/\/arzhost.com\" data-type=\"link\" data-id=\"https:\/\/arzhost.com\">ARZ Host<\/a> Before Time Runs Out!<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/arzhost.com\/web-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_self\" rel=\"noopener noreferrer\">Read More<\/a><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>18: Changing File Ownership in Archives: Modify File Ownership<\/strong><\/h2>\n\n\n\n<p><u>Tar allows file ownership configuration. For example, to set the file owner when creating an archive, add the &#8211;owner and &#8211;group options and provide values for each:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf --owner= --group=<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>The owner value represents the UID (User ID) while the group value is the GID (Group ID). To find these values for a user, run:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>id<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, create an archive and set the ownership to root:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar cf files.tar files --owner=0 --group=0<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>Tar allows preserving the ownership when extracting from an archive. To do so, add the &#8211;same-owner option at the end:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf --same-owner<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>19: Writing Archive Data to External Programs<\/strong><\/h2>\n\n\n\n<p><u>The &#8211;to-command option instructs tar to send each extracted file to the standard output for an external program. The basic syntax is:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf --to-command=''<\/code><\/li>\n<\/ul>\n\n\n\n<p><u>For example, extract the files.tar contents and pipe the file names as directories:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar xf files.tar --to-command='mkdir $TAR_FILENAME'<\/code><\/li>\n<\/ul>\n\n\n\n<p>Each extracted file is given its own directory, which the command creates. Visit the manual page for more details and a list of the available Linux environment variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>20: Creating Daily Backups with Tar: Setting Up Automated Backups<\/strong><\/h2>\n\n\n\n<p><u>To automate daily backups, create a bash script and add the following lines:<\/u><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tar czf backup-$(date +%Y%m%d). tar.gz files<\/code><\/li>\n\n\n\n<li><code>find backup* -mtime +1 -delete<\/code><\/li>\n<\/ul>\n\n\n\n<p>The find command looks for backup files older than one day, whereas the tar tool builds a compressed archive. For weekly or monthly backups, change the +1 parameter to +7 or +31, respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>You are familiar with how to use the Tar Command in Linux after reading this tutorial. There are, however, several additional options that are not covered in this guide. To learn everything there is to know about tar options, use the man command.<\/p>\n\n\n\n<p><strong>Read More:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/how-to-fix-403-forbidden-error-wordpress\/\">How To Fix 403 Forbidden Error WordPress<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/how-to-get-the-most-out-of-claude-ai\/\">How To Get The Most Out Of Claude Ai<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/bad-gateway-error-502-the-ultimate-guide-to-quick-fixes\/\">Bad Gateway Error (502): The Ultimate Guide to Quick Fixes<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/a-deep-dive-into-todays-best-linux-distros\/\">A Deep Dive Into Today\u2019s Best Linux Distros<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/arzhost.com\/blogs\/domain-investor-terms-powerful-strategy\/\">Domain Investor Terms: Expert Insight on Powerful Strategy<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Importance in File Archiving and Compression In Linux systems, the most popular archiving tool is the GNU tar programmer (short for Tape Archiver). The Tar Command in Linux aids in creating, extracting, and listing archive contents and is directly accessible in the terminal. The tool is straightforward and offers a wide range of advantageous choices [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6119,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-4668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/comments?post=4668"}],"version-history":[{"count":6,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4668\/revisions"}],"predecessor-version":[{"id":11047,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/4668\/revisions\/11047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media\/6119"}],"wp:attachment":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media?parent=4668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/categories?post=4668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/tags?post=4668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}