Thursday, 10 August 2017


Last week I have written a short intro for my NDG Linux Essential course - module 7 - and now I can proudly announce that week 7 of my NDG Linux education is successfully completed  and here are some key notes and summary of my NDG Linux Essential - week 7 -. As noted previously in my short intro of module 7, this entire module was very short but cover some skills working with compressing, decompressing, creating .tar archive, working with gzip, bzip and so on. So here are some important notes on working with tarball, creating an tar archive, listing it's content, updating the archive and so on. I should also note that this article includes the final exam for module 7 at the end which you can see down bellow. Ok let's start. . . 

Written by: Amar Tufo
9. August, 2017

The tar utility is widely used by all Linux distributions. Along with being a useful tool for backup, tarball files are also a convenient way to distribute files. They allow a single file download and because compression can be handled with tar they also creates smaller files for download. Tarball files do not have to end with .tar extension, but it's a good practice to do so. Files that are joined and compressed often use the .tar.gz extension. In the following sample I will demonstrate how to create tarball file, how to extract it, see it's content and extract a single file out of tarball.

1# Sample: Creating archive.tar.gz file


                                                             
Image 1: Creating archive.tar.gz for final directory
Image source: /home/amar/Desktop

In this first sample I have created archive.tar.gz tarball as backup for my final directory. In the image1 above, archive contents were listed upon compressing. Short note for the command used above is following:



tar -zcvf archive.tar.gz final/

Once typed this command will compress final directory with all it's content into single tarball in this case archive.tar.gz. If you wonder what -zcvf stands for than here what they are:


tar -zcvf archive.tar.gz final/
*z - tells the tar to use gzip tool
*c - creates an archive
*v - tell me what you are doing
*f - file name

2#Sample: Listing the archive.tar.gz content and extracting it to another directory

Let's assume that you want to view the content of archive.tar.gz before extracting it. Here's the command that will do just that:


tar -tf archive.tar.gz 

Once typed into terminal this command will generate the following output:


Image 2: Listing the content of archive.tar.gz tarball
Image source: /home/amar/Desktop


Now as we have listed our content, let's extract our archive.tar.gz in /tmp directory of my home folder. Here's the command that will accomplish such a task.


tar -xvzf archive.tar.gz -C /home/amar/tmp

Note: -C in this command allow you to give the tar command a path to destination directory in which you are extracting your tarball in this case archive.tar.gz > /home/amar/tmp.

Once you typed this command this is the output you should see. Image 3:


Image 3: Extracting archive.tar.gz into /home/amar/tmp
Image source: home/amar/Desktop

Note that I have extracted the archive.tar.gz content into /home/amar/tmp directory from Desktop as my pwd directory. Keep this in mind that before each extraction of the tarball move the archive into empty directory or the directory/files will be replaced and overwritten. 


Image 4: Successfully extracted archive.tar.gz tarball file into /tmp directory
Image source: /home/amar/tmp

3#Sample: Adding log.txt file into archive.tar.gz and extracting a single file out of archive

Let's assume that you want to add a file into existing archive in this case log.txt file into archive.tar.gz. For this purpose I have created four files on my Desktop namely file1.txt, file2.txt, file3.txt and log.txt. I will now create an archive.tar.gz which will contain only file1.txt, file2.txt and file3.txt using the following command:


tar -cvf archive.tar.gz file1.txt file2.txt file3.txt


Image 5: Creating a new archive.tar.gz 
Image source: /home/amar/Desktop

Now let's update existing archive.tar.gz by adding log.txt into our archive. We can do that using the following command:


tar -rf archive.tar.gz log.txt

Your output should be the same as main if you have type it correctly. See the image 6 down bellow.


Image 6: Updating the existing archive.tar.gz with log.txt file
Image source: /home/amar/Desktop

4#Sample: Extracting log.txt into different directory

Sometimes tarball can be very big in size and you don't need an entire archive. You need only a specific file as Linux System Administrator into let's say /tmp directory. In this sample I'm about to show you how to extract log.txt file from archive.tar.gz file into /tmp directory which is in my home directory.


tar -xvf archive.tar -C /home/amar/tmp log.txt

Note when extracting a single file out of tarball, file is added at the end of command. In other case, the file will be extracted into present working directory. Here are the final results of this command.


Image 7: Extracting log.txt file from archive.tar into /tmp directory
Image source: /home/amar/Desktop

Here's the terminal output for our /tmp directory which holds log.txt file which we extracted from archive.tarball.


Image 8: /tmp directory with log.txt file
Image source: /home/amar/Desktop


This all folks for today. I hope that this article has helped you in understanding the way tar works, how to create an tarball, how to list it's content and how to extract the single tarball file into another directory. Make sure to commend down bellow and share this article. Stay on my site because I lot's of other useful and educational content for you all. We see you soon at NDG Linux Essential week 8, till then have a nice stay at my website.


Reakcije:

0 komentari:

Post a Comment

Weather

Advertisement

Follow me on Facebook:

Popular Posts