File Archiving and Compression: Commands tar
, gzip
, zip
1. Concept of Archiving and Compression
What's archiving and why do you need it?
Imagine a closet full of books. To store them compactly, you could pack them in a box (archiving) and tie it with a rope for tighter storage (compression). In the computer world, archiving is the process of combining multiple files into one container (archive), and compression is reducing its size.
But why do you need it? Here are a few examples:
- Data storage: By packing files into an archive, you can easily move or copy them as one file.
- Data transfer: Compression reduces the size of the data, making network transfer faster.
- Backup: Archiving helps protect data and makes recovery simpler.
By the way, have you ever tried sending 1000 files via email? Without archiving, it’s more like torture!
2. Command tar
: creating and working with archives
What is tar?
tar
is a utility for archiving files on Linux. By itself, tar
doesn’t compress files but just combines them into one. If you want to compress the file, you can use it together with compression tools (gzip
, bzip2
, etc.).
General syntax
tar [options] <archive> <files/folders>
Key options
-c
: create a new archive.-v
: verbose output (lets you know about all added files).-f
: specify the archive name.-x
: extract files from the archive.-t
: display the contents of the archive.
Usage examples
1. Creating an archive
tar -cvf archive.tar file1.txt file2.txt
- Creates an archive
archive.tar
from two filesfile1.txt
andfile2.txt
. -c
: creates a new archive.-v
: displays the whole process of adding files in the terminal.-f
: specifies that the archive name isarchive.tar
.
2. Archiving a folder
tar -cvf project.tar /home/user/project
- Creates an archive
project.tar
including the entire folder/home/user/project
.
3. Extracting an archive
tar -xvf archive.tar
- Unpacks the archive
archive.tar
into the current directory.
4. Viewing the contents of an archive
tar -tvf archive.tar
- Shows a list of files and folders in the archive.
3. Compression with gzip
What does gzip
do?
If tar
is a box, then gzip
is a vacuum seal that reduces its size. gzip
minimizes data using compression algorithms, making it an awesome tool for saving space.
General Syntax
gzip [options] <file>
Usage Examples
1. Compressing a file
gzip file1.txt
- After execution, the command will rename the file
file1.txt
tofile1.txt.gz
.
2. Decompressing a file
gunzip file1.txt.gz
- Restores the original file
file1.txt
.
3. Archiving and compressing with tar
and gzip
tar -czvf archive.tar.gz file1.txt file2.txt
- Creates an archive
archive.tar.gz
, which combines and compresses the filesfile1.txt
andfile2.txt
.
4. Extracting a compressed archive
tar -xzvf archive.tar.gz
- Unpacks and extracts the contents of the archive
archive.tar.gz
.
4. The zip
Utility for Creating Compressed Archives
What's the Difference from tar
?
Unlike tar
, zip
creates a compressed archive right away, which by default can be used in Windows and other systems.
General Syntax
zip [options] <archive> <files/folders>
Examples of Use
1. Creating a zip archive
zip archive.zip file1.txt file2.txt
- Creates an archive
archive.zip
, which will include the filesfile1.txt
andfile2.txt
.
2. Archiving a folder
zip -r project.zip /home/user/project
- Creates an archive
project.zip
, including the entire folder/home/user/project
. -r
: recursively adds files and folders.
3. Extracting a zip archive
unzip archive.zip
- Extracts the contents of the archive
archive.zip
.
5. Differences Between tar
, gzip
, and zip
Utility | Archiving | Compressing | Approach | Main Use |
---|---|---|---|---|
tar |
✅ | ❌ | File packaging | Combining files into an archive without compression |
gzip |
❌ | ✅ | Compression | Reducing the size of individual files or archives |
zip |
✅ | ✅ | All-in-one | Creating portable archives |
6. Practical Task
- Create an archive
backup.tar
from the contents of the folder/home/user/documents
. - Compress the archive using
gzip
to getbackup.tar.gz
. - Extract the archive to a different directory, for example,
/tmp/backup
. - Create a zip archive
docs.zip
from the filesdoc1.txt
anddoc2.txt
. - Try to extract the contents of
docs.zip
using theunzip
command.
7. Typical Errors and Features
1. Error "Permission denied" If you're trying to archive files that are restricted, you'll get an error message. Run the command with admin privileges: sudo
.
2. Confused by options? Options in tar
can sometimes be confusing. Just remember that order matters: for example, you need to specify the action like -c
or -x
first, and only then other options.
3. Compressed archive won’t open If you accidentally added compression to an already compressed file (like gzip archive.tar.gz
), don’t panic. Just unpack it twice.
4. Too large an archive When using the zip
command, exclude temporary or cache files to reduce the size of the archive: zip -r archive.zip folder -x "*.tmp"
.
5. Compatibility issues If you need to send a file to a Windows user, go with zip
since it’s better supported on that OS.
That’s how we mastered file archiving and compression in Linux. These tools will come in handy for simplifying work with large amounts of data, transferring them, and protecting them from loss. Move on to the next topic—your knowledge will only keep growing! 🎉
GO TO FULL VERSION