Archiving is the method to merge multiple files and directories into a single bundle. Here, I will discuss how to archive in Linux using the tar command. Then, compressing the tar files using different tools will also be discussed. Moreover, you will learn about the subtle difference between archive and compression.
What is Archiving?
When someone combines multiple contents into a unit package, it is known as archiving. This compact package or archive then can be transferred to another machine or stored in a distant repository. Thus, computers can be organized more efficiently. It is an inevitable part of Linux based OS environment. Because, when anyone installs any software, first he/she downloads it basically as an archive from a distant server.
Difference Between Archive and Compression
Sometimes, people use archive and compression interchangeably but these two are not the same thing. When you archive contents that means you are processing it into a unit bundle and it will consume the same disk space as before. On the contrary, compression will lower the size of the contents.
An Overview of Tar Archive
In Linux, tar is the abbreviation for tape archive. The tar command is used to archive and unarchive files and directories in Linux. Most Linux distributions contain pre-installed tar, if otherwise, you can simply run the below command for ubuntu:
sudo apt-get install tar
tar [OPTIONS]... archive_file.tar files_to_be_archived
- -c: Creates a new archive.
- -x: Extracts an archive.
- -f: Creates an archive with the given file name.
- -r: Appends files to an existing archive.
- -t: Shows the contents of an archive.
- –delete: Deletes from an archive.
Basics of Tar Archive in Linux
In Linux, you can simply create an archive using the tar command. We have to implement the basic options and we’re good to go.
A. Uncompressed Archive Creation in Linux
Here, I provided tar command with two options -cf (one for creating(-c) the archive and another for creating the archive with the given file(-f) name) followed by the archive name and the files to be archived.
B. Displaying Contents of an Archive in Linux
Contents of an existing archive can be printed on the terminal using the tar command provided with the -tf option followed by the archive name.
C. Appending to an Existing Archive in Linux
Appending can be easily done by executing the tar command with -rf option followed by the existing tar file and the files to be archived.
D. Deleting Contents from an Archive in Linux
You can delete one or multiple contents of an archive using the tar command with –delete & -f options followed by the archive name and files to be deleted,
E. Archiving Directories in Linux
Directories can also be archived in a similar fashion to the files using the tar command and the -cf option.
Archive Compression Tools in Linux
In one of the first segments, I discussed about the differences between archive and compression. Now, I will describe the methods to compress archive files. There are few tools available in Linux to do this task. Here, I will use two of the most used compression tools gzip & bzip2.
A. Using the gzip Compression tool
You can simply gzip an archive file by using the -czf(option -z for gzip) option instead of using the -cf option while creating the archive file.
B. Using the bzip2 Compression tool
The bzip2 compression tool can similarly be utilized like the gzip tool. You just need to add option -j in this case.
Extracting Tar Archive in Linux
In Linux, extracting an archive file is as simple as creating one. You have to use the -x option and we are good to go.
A. Extracting in the Current Directory
You can simply extract the contents of an archive file using the tar command with -xf option followed by the archive file name.
B. Extracting into a Specific Directory
Sometimes, you may need to extract the contents of an archive file into a specific directory rather than the current directory. It can be easily done by utilizing the -C(capital c) option followed by the target directory,
C. Extracting Compressed Archive File in Linux
When you’re extracting a compressed archive file you just need to add option -z for gzipped file.And option -j for bzipped files.
Sometimes, you may need to organize the contents of your computer or store & transfer them to a remote location. You can archive the contents and complete the tasks more efficiently. In this article, I tried to provide you with the basic knowledge of archives in Linux using the tar command along with some detailed examples. With this knowledge, you can now play with the tar command as your wish and also if want to explore more you can look for more on the tar man page.