Archiving allows us to consolidate multiple files or directories into a single archived file. Two of the popular archive commands on Linux,
tar, have the ability to compress and archive files. This means that unlike the compression commands which compress a single file at a time, the files’ size will be reduced and packaged into a single archive file in one command.
Zip files are very popular across multiple operating systems. We can create a
.zip archive like so:
zip <archive_name>.zip <file1> <file2> …
Note: On some distributions of Linux,
zip must first be installed using the command
sudo apt install zip unzip.
Directories can be easily archived with the
-r option. Archived files can be extracted and decompressed using the
unzip command and providing paths to one or more
tar, which stands for tape archive or tarball, is a very important archiving utility for Linux systems. While a
zip archive is more popular across platforms, it is recommended to use
tar when distributing archives among Linux-based systems. This is because
tar archives store Unix file attributes, retaining file permissions and other metadata.
tar -cf <archive_name>.tar <files or directories>
creates a uncompressed
.tar archive. A
.tar file can be referred to as a tarball.
tar -cd example.tar index.html script.js style.css
will create a
.tar archive file with three files:
To extract the files in a
.tar archive, we can use the
tar -xf <archive_name>.tar
In the next exercise, we will learn how to further compress
Run the command
ls -R to see all the files in the current directory and subdirectories.
Extract the archive named
.zip archive of the
riddles directory. At the end, there should be one file called
Create a tarball named
text_files.tar of the files
Run the command
ls -R again to see the resulting files after using the commands we just learned.