- Creating a .tar archive:
In Linux, we can create an archive file using the tar program and use gzip to compress the archive. This is similar to the WinZip program in Windows where you can compress multiple files or directories into a compressed .zip archive.
For example, first just create a directory called “testdir”
# mkdir testdir
Now, change your working directory to testdir and create files “test1.txt”, “test2.txt”, “test3.txt“.
# cd testdir
# touch test1.txt
# touch test2.txt
# touch test3.txt
Now, create another directory called “testdir2”
# mkdir testdir2
Now, change your working directory to testdir2 and create files “test4.txt”, “test5.txt”, “test6.txt“.
# cd testdir2
# touch test4.txt
# touch test5.txt
# touch test6.txt
Now, go back your initial directory
# cd ../..
To see the list of the created directories and files:
# tree testdir
Now, to create an tar archive using the “c” (create) and “f” (file) arguments with the tar command as follows:
# tar cf testdir.tar testdir
Now, to seeif the archive was created. You can type the following command:
# ls -l *.tar
If you want to see if all the files are inside the archive, or if you want to see the contents of the archive, you use the following command:
# tar tf testdir.tar
If you want to see the progress in the archiving process, you can add the “v” argument. For example, the command would then would be:
# tar cvf testdir.tar testdir
You can also make an tar archive in “Interactive Mode” to select which files needed to be added to the archive and which ones you need 2 exclude. This can be done by using the ‘w‘ argument . You can add a file by typing ‘y’ when prompted and ‘n’ for the file you do not need to add to the archive.
For example, if you wanted to exclude files “test2.txt” and “test4.txt“ from the archive, all you have to do is , use the following command and type ‘n’ when prompted for “test2.txt” and “test4.txt” and type “y” for the rest of the files.
# tar cwf testdir.tar testdir
- Extracting a .tar archive:
To extract a .tar archive, you can use tar with ‘-x‘ (for extracting) and “-f” and “-v“(to see what is happening or progress of extraction) arguments.
# tar xvf testdir.tar
To select which files to be extracted you need to use the ‘w’ argument. For example:
# tar xvwf testdir.tar
Now, if you want to just want to extract a single file from the archive, for example, “testdir/testdir2/test4.txt”, you can use the following command:
# tar xvf testdir.tar testdir/testdir2/test4.txt
- Compressing Files using gzip:
We can compress a .tar archive using gzip and the compressed file will be either a .tgz or .tar.gz file.
For example, if you want to compress “testdir.tar” using gzip, you can do it by using the following command:
# gzip testdir.tar
The resulting compressed file will be “testdir.tar.gz”. So, the compressed file will be the filename with a “.gz” extension by default.
To see the details of the compressed file, you use the following command:
# gzip -l testdir.tar.gz
To check if the compressed “.tar.gz” file is proper, you can use the following command:
# gzip -tv testdir.tar.gz
You can also compress the “.tar” archive to a custom extension using the “-S” argument. For example, if you want to compress the arhive “testdir.tar” to “.gzipped”, by using:
# gzip -S .gzipped testdir.tar
- Decompressing using gzip or gunzip:
We can decompress a compressed file using the “gunzip“. The compressed file should have any of these extensions “.gz”, “.z”, “.Z”, “-z” or “-Z” in order to decompress or it will not work.
For example, if you want to decompress the file “testdir.tar.gz”
# gunzip testdir.tar.gz
Another way to decompress is by using the gzip program itself by using the “-d” argument. For example:
# gzip -d testdir.tar.gz