8/06/2020

The TAR command | Bash Basics


If you are familiar with ZIP, then you will be familiar with TAR. Basically, this is used to archive files into a single, hopefully, smaller, file. Sometimes you will hear these referred to as a “tarball”.

Figuring out what is in a TAR Archive

So normally when you are provided with a “tarball”, you go get it and then you extract it onto your system. This raises the questions, where will the files be created and what will be created? We can inspect the .tar file to determine what is in it. Remember that when we are working with TAR, we want it to be Verbose and so we add the v flag so we can see what it is doing. In the case of listing the contents of the archive, adding verbose or v is the same as asking for a long listing in the ls command by adding -l.
tar --list --verbose --file 
Ok, let’s get real, no one does this; this is what it would look like if you read the man page and decided that you wanted to type all of the things. I am lazy and I like to get things done with the least amount of typing on my part. What you see here is the same as this:
tar -tvf 
The only odd thing here is that --list is replaced by -t and then we can add all the flags to the same - statement. This means that for the rest of the examples I will show you the shorthand and you can decide how you want to do it. So now let’s look at some actual output.
$ tar -tvf ./example.tar 
drwxr-xr-x  0 mmcclaren staff       0 May  2 14:37 ./example/ 
-rw-r--r--  0 mmcclaren staff     210 May  2 14:23 ./example/._examplefile1 
-rw-r--r--  0 mmcclaren staff     899 May  2 14:23 ./example/examplefile1 
-rw-r--r--  0 mmcclaren staff     210 May  2 14:23 ./example/._examplefile2 
-rw-r--r--  0 mmcclaren staff   15033 May  2 14:23 ./example/examplefile2 
-rw-r--r--  0 mmcclaren staff     210 May  2 14:23 ./example/._examplefile3 
-rw-r--r--  0 mmcclaren staff   14957 May  2 14:23 ./example/examplefile3 
In this case, we can see that all of the example files were included in an example directory. This means that when we extract this, we will get that directory in the output. If the path is only the files and there is not a directory included, then the files will end up in the directory that you are in, unless you specify an extraction point.

Extracting a TAR Archive

So now that we know what is in there, we want to get it out of there. This is a matter of running the eXtract flag and the File flag.  Remember that we can add the Verbose flag so that we can see what it is working on.
 $ tar -xvf ./example.tar  
 x ./example/ 
 x ./example/._examplefile1 
 x ./example/examplefile1 
 x ./example/._examplefile2 
 x ./example/examplefile2 
 x ./example/._examplefile3 
 x ./example/examplefile3 
In this example, we extract the files into the example directory.
 $ ls -l ./example 
 total 72 
 -rw-r--r--@ 1 mmcclaren  staff    899 May  2 14:23 examplefile1 
 -rw-r--r--@ 1 mmcclaren  staff  15033 May  2 14:23 examplefile2 
 -rw-r--r--@ 1 mmcclaren  staff  14957 May  2 14:23 examplefile3 
If we wanted to indicate where we wanted the archive extracted, this would require the addition of the -C flag and a location as an argument. Think of this as “copy” the extracted files to the location indicated.
 $ tar -xvf ./example.tar -C ./location 
 x ./example/ 
 x ./example/._examplefile1 
 x ./example/examplefile1 
 x ./example/._examplefile2 
 x ./example/examplefile2 
 x ./example/._examplefile3 
 x ./example/examplefile3 
 $ ls -l ./location 
 total 0 
 drwxr-xr-x  5 mmcclaren  staff  160 May  2 15:08 example 
And the last thing that I want to talk about is extracting only one file from the archive. This takes the form of tar -xf ( archive name ) ( file name ) as in this example.
 $ tar -xvf ./example.tar ./example/examplefile2 
 x ./example/examplefile2 
 $ ls 
 example example.tar location 
 $ ls ./example 
 examplefile2 

Creating an Archive

Whew, this is a long one! We cannot leave, though, without knowing how to create an archive. The simplest way is to point TAR at a directory and use the -c flag for Create in the form of:
tar -cf  
This is how the example archive that we have been using was created:
 $ tar -cvf example.tar example 
 a example 
 a example/examplefile1 
 a example/examplefile2 
 a example/examplefile3 
 $ tar -tf ./example.tar  
 example/ 
 example/._examplefile1 
 example/examplefile1 
 example/._examplefile2 
 example/examplefile2 
 example/._examplefile3 
 example/examplefile3 
One last thing: let’s add a file to an existing archive. The -r flag is used to append to the directory and it takes the same syntax as “create”; the name of the archive and then the file.
$ tar -rf example.tar examplefile4 
$ tar -tf example.tar
example/
example/._examplefile1
example/examplefile1
example/._examplefile2
example/examplefile2
example/._examplefile3
example/examplefile3
examplefile4
As you can see, the examplefile4 was added to the previous archive that we have been using.

No comments:

Post a Comment

Popular Posts