How to Archive in Linux [Archiving, Extraction, Compression]

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.Archive means compacting things into a package.

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

Insatalling tar in Ubuntu.

Syntax of “tar” Archive Command

tar  [OPTIONS]...   archive_file.tar  files_to_be_archived

Options of “tar” Archive Command

Options Description
-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.

Create Uncompressed Archive in Linux

To create an uncompressed archive, use the tar command with two options -cf. Here, -c is used for creating the archive, and -f is used for specifying the file names to be archived.

Archiving 3 files using the tar command.

Display Contents of an Archive File 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.

Listing the contents of an archive.

Append to an Existing Archive in Linux

To append to an existing archive file, use  the tar command with the -rf option followed by the existing tar file and the files to be appended.

Appending contents into an existing archive.

Delete Contents from an Archive in Linux

To delete one or multiple contents of an archive use the tar command with --delete -f options followed by the archive name and files to be deleted,

Removing contents from an archive

Archive Directories in Linux

The tar command and the -cf option can archive directories in Linux.

Creating an archive of a directory using the tar command.

Archive Compression Tools in Linux

There are few tools available in Linux to compress archives. Here, I will use two of the most used compression tools gzip & bzip2. Here’s how to use these:

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.Compressing an archive file using the gzip tool.

B. Using the bzip2 Compression tool

The bzip2 compression tool can similarly be utilized like the gzip tool. You just need to add an option -j in this case.Compressing an archive file using the bzip2 tool.

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.Archive files requires same disk space as original but compression decreases that.

Extracting 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

To extract the contents of an archive file, use the tar command with the xf option followed by the archive file name.Extracting the contents of an archive on the current directory.

B. Extracting into a Specific Directory

To extract the contents of an archive file into a specific directory rather than the current directory, mention the target directory after -C option. The syntax is:

tar [options] <archive name> -C <directory>  Extracting the contents of an archive on a specific directory.

C. Extracting Compressed Archive File in Linux

To extract a compressed archive file, use gzip -z for gzipped files and option -j for bzipped files.Extracting the contents of a gzipped archive file.Extracting the contents of a bzipped archive file.

Conclusion

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 you wish and also if want to explore more you can look for more on the tar man page.

People Also Ask

What data is stored in archive?

The contents of an archive depend on the type of data that is being archived. For example, a data archive may contain historical records, financial data, or other types of information that are no longer in active use but may be needed for future reference.

Does archiving cause data loss?

No, archiving data does not cause data loss. In fact, data archiving is a method of storing and organizing significant, but currently not in need, information for future reference. It is an essential part of data management and can help prevent data loss by providing a secure location for data storage.

How to view archive in Linux?

To view the contents of a .tar archive file, you can use the tar command to extract the contents of the archive to the terminal. Here is an example of how to use the tar command to extract the contents of an archive file named my_archive.tar:

tar -tvf my_archive.tar

This command will print the names of all the files in the archive to the terminal.

What is the archive command used for?

The archive is a command that is used to create an archive of one or more files. The archive can be compressed or uncompressed, and it can be stored locally or remotely. The tar command is a popular archiving utility that is used to create and manipulate tar archives. The tar program is used to create, modify, and extract from archives.

What is a GNU archive?

A GNU archive is a collection of files that are stored together in a single file. It is used to store and distribute software programs and data. The GNU Project is a free software, mass collaboration project that aims to create a complete Unix-like operating system composed entirely of free software. The GNU Project provides a wide range of software tools and utilities, including the GNU Compiler Collection (GCC), GNU Debugger (GDB), GNU Binutils, and GNU Coreutils.


Similar Readings

5/5 - (6 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Md. Ashakul Islam Sowad

Hi, I am Md. Ashakul Islam Sowad from Dhaka, Bangladesh. I have completed my undergraduate degree in Biomedical Engineering from the Bangladesh University of Engineering and Technology (BUET). I love to watch football and play video games in my free time. Here, I am working as a Linux Content Developer Executive. Furthermore, as a Linux enthusiast, I am always learning new things about Linux-based systems and I’ll be sharing them here. Read Full Bio

Leave a Comment