In this Linux tutorial we will learn how to archive files. Is there any difference between archiving and compression?
In this tutorial we will learn how to archive files in Linux. Is there any difference between archiving and compression? Some of you may often think that these terms have the same meaning. However, the two are completely different processes.
Introduction Linux Archiving & Compression
Archiving is the process of combining multiple files and directories (same or different sizes) into one file. On the other hand, compression is the process of reducing the size of a file or directory. Archiving is usually used as part of a system backup or when moving data from one system to another.
Hope you understand the difference between archiving and compression. Now let's get into the topic.
Archives and Directories
The most common programs for archiving files and directories are:
- Tar
- Zip
This is a big topic, so I will publish this article in two parts. In the first part, we will see how to use the tar command to archive files and directories.
Archive files and directories using the tar command
Tar is a Unix command representative TAPE Archive (tape archive). It is used to combine or store multiple files (same or different
Sub-title here
sizes) into one file. There are four main modes of operation in the tar utility.
- c – Create an archive from a file or directory
- x – Extract archive
- r – Append file to archive
- t - List the contents of the archive
Create a new archive
For this guide, I will use the name ostechnix of the folder, which contains three different types of files.
$ ls ostechnix/
file.odt image.png song.mp3
Now, let us ostechnix create a new tar archive directory.
$ tar cf ostechnix.tar ostechnix/
Here, the c flag refers to the creation of a new archive, where f is the specified archive file.
Similarly, to create an archive of a set of files in the current working directory, use the following command:
$ tar cf archive.tar file1 file2 file 3
Extract archive
To extract the archive in the current directory, just do the following:
$ tar xf ostechnix.tar
We can also use the C logo (capital letter C) to extract the archive to a different directory.
For example, the following command will extract the archive to a Downloads directory.
$ tar xf ostechnix.tar -C Downloads/
Or, go to the Downloads folder and something like the following extract the archive.
$ cd Downloads/
$ tar xf ../ostechnix.tar
Sometimes you may want to extract a particular type of file.
For example, the following command extracts a file of type ".png".
$ tar xf ostechnix.tar --wildcards "*.png"
Create compressed archives in gzip and bzip format
By default, tar creates an archive file to .tar the end. Further, tar the command may be compression utility gzip and bzip combination.
The end of the file to .tar use ordinary tar extension to archive files tar.gz or .tgz end use gzip archived and compressed files, files tar.bz2 or .tbz end use bzip archiving and compression.
First, let's create a gzip archive:
$ tar czf ostechnix.tar.gz ostechnix/
or:
$ tar czf ostechnix.tgz ostechnix/
Here, we use the z flag to use gzip compression method archive.
You can use v to see the progress when creating the archive flag.
$ tar czvf ostechnix.tar.gz ostechnix/
ostechnix/
ostechnix/file.odt
ostechnix/image.png
ostechnix/song.mp3
Here, it v refers to the progress of the display.
Create a gzip archive from a list of files:
$ tar czf archive.tgz file1 file2 file3
To extract the gzip archive in the current directory, use:
$ tar xzf ostechnix.tgz
To extract to a different folder, use the -C logo:
$ tar xzf ostechnix.tgz -C Downloads/
Now let's create a bzip archive . To do this, use the following j logo.
Create an archive of the directory:
$ tar cjf ostechnix.tar.bz2 ostechnix/
or
$ tar cjf ostechnix.tbz ostechnix/
Create an archive from a list file:
$ tar cjf archive.tar.bz2 file1 file2 file3
or
$ tar cjf archive.tbz file1 file2 file3
In order to show progress, the use of v signs.
Now, in the current directory, let's extract a bzip archive. this way:
$ tar xjf ostechnix.tar.bz2
Or, extract the archive to another directory:
$ tar xjf ostechnix.tar.bz2 -C Downloads
Create archives of multiple directories and/or files at one time
This is tar another of the coolest features command. To create a gzip archive of multiple directories or files at once, use the following files:
$ tar czvf ostechnix.tgz Downloads/ Documents/ ostechnix/file.odt
The above command to create Downloads, Documents catalog and ostechnix directory file.odt archives, and archived in the current working directory.
Skip directories and/or files when creating an archive
This is very useful when backing up your data. You can exclude unimportant files or directories in the backup, this is the –exclude option that can help. For example, you want to create /homean archive directory, but do not want to include Downloads, Documents, Pictures, Music these directories.
This is our approach:
$ tar czvf ostechnix.tgz /home/sk --exclude=/home/sk/Downloads --exclude=/home/sk/Documents --exclude=/home/sk/Pictures --exclude=/home/sk/Music
The above command will be my $HOME directory create a gzip archive, which does not include Downloads, Documents, Pictures and Music directories. To create a bzip archive, it will be z replaced j, and the use of extensions in the above example .bz2.
List archives without extracting them
To list the contents of the archive, we use the t symbol.
$ tar tf ostechnix.tar
ostechnix/
ostechnix/file.odt
ostechnix/image.png
ostechnix/song.mp3
To see the detailed output, use the v flag.
$ tar tvf ostechnix.tar
drwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/
-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt
-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png
-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3
Add files to archive
Files or directories can use r logo Add / update to an existing archive. Take a look at the following command:
$ tar rf ostechnix.tar ostechnix/ sk/ example.txt
The above command will be named sk directory and named example.txt added to the ostechnix.tar archive.
You can use the following command to verify that the file has been added:
$ tar tvf ostechnix.tar
drwxr-xr-x sk/users 0 2018-03-26 19:52 ostechnix/
-rw-r--r-- sk/users 9942 2018-03-24 13:49 ostechnix/file.odt
-rw-r--r-- sk/users 36013 2015-09-30 11:52 ostechnix/image.png
-rw-r--r-- sk/users 112383 2018-02-22 14:35 ostechnix/song.mp3
drwxr-xr-x sk/users 0 2018-03-26 19:52 sk/
-rw-r--r-- sk/users 0 2018-03-26 19:39 sk/linux.txt
-rw-r--r-- sk/users 0 2018-03-26 19:56 example.txt
TL;DR
Create a tar archive:
- Common tar archives: tar -cf archive.tar file1 file2 file3
- Gzip tar archive: tar -czf archive.tgz file1 file2 file3
- Bzip tar archive: tar -cjf archive.tbz file1 file2 file3
Extract tar archives:
- Common tar archives: tar -xf archive.tar
- Gzip tar archive: tar -xzf archive.tgz
- Bzip tar archive: tar -xjf archive.tbz
Well, here in this article we learned about how to archive files and directories in Linux. If you have any questions regarding this archiving files tutorial and learning Linux, please drop your comment in below comment section. We will get back to you as soon as possible. Happy learning.