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.
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
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.
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.
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.
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,
Archive Directories in Linux
The tar
command and the -cf
option can archive directories in Linux.
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.
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.
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.
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.
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>
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.
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
- What is Directory in Linux [A Complete Overview]
- An Overview of Relative Path in Linux [The Complete Guide]
- An Overview of Absolute Path in Linux [The Complete Guide]
- What is Alias in Linux? [Creating, Removing & More]
FUNDAMENTALS A Complete Guide for Beginners