CIS120Linux Fundementals
Linux Archiving and Zipping
The gzip
, zip
, and tar
commands are essential tools in Linux for compressing and archiving files. Each tool has unique features and is used for different purposes.
The gzip Command
gzip
is a compression tool used to reduce the size of files. It does not create archives (collections of multiple files); instead, it compresses single files.
Common gzip
Options:
Option | Description |
---|---|
-d |
Decompress |
-k |
Keep the original file |
-l |
List compression information |
-r |
Recursively compress directories |
-v |
Verbose output |
-1 to -9 |
Set compression level (1 = fastest, 9 = slowest/maximum) |
Examples:
To compress a file:
gzip file.txt
To decompress a file:
gzip -d file.txt.gz
To keep the original file while compressing:
gzip -k file.txt
To recursively compress all files in a directory:
gzip -r directory/
The zip Command
zip
is a tool used for both compressing and archiving files. It can create zip archives containing multiple files and directories.
Common zip
Options:
Option | Description |
---|---|
-r |
Recursively add directories |
-d |
Delete files from the archive |
-u |
Update files in the archive |
-l |
List files in the archive |
-v |
Verbose output |
-e |
Encrypt the archive |
Examples:
To create a zip archive:
zip archive.zip file1.txt file2.txt
To unzip an archive:
unzip archive.zip
To add files to an existing archive:
zip archive.zip file3.txt
To recursively add directories to an archive:
zip -r archive.zip directory/
The tar Command
tar
is used to create archive files that can contain multiple files and directories. While tar
itself does not compress files, it is often used in combination with gzip
or bzip2
to create compressed archives (.tar.gz
or .tar.bz2
).
Common tar
Options:
Option | Description |
---|---|
-c |
Create a new archive |
-x |
Extract files from an archive |
-t |
List files in an archive |
-v |
Verbose output |
-f |
Specify the filename of the archive |
-z |
Filter the archive through gzip |
-j |
Filter the archive through bzip2 |
Examples:
To create a tar archive:
tar -cvf archive.tar file1.txt file2.txt
To extract a tar archive:
tar -xvf archive.tar
To create a compressed tar archive:
tar -czvf archive.tar.gz file1.txt file2.txt
To list the contents of a tar archive:
tar -tvf archive.tar
Differences Between gzip
and zip
gzip
compresses individual files, whilezip
compresses and archives multiple files and directories.gzip
is often used withtar
to compress tar archives, resulting in.tar.gz
files.zip
creates a single archive that contains compressed files and directories.
Differences Between gzip
, zip
, and tar
gzip
: Compresses single files. Commonly used withtar
to create compressed archives.zip
: Compresses and archives multiple files and directories in a single archive.tar
: Archives multiple files and directories but does not compress them by itself. Often used withgzip
orbzip2
for compression.
Viewing Contents of a Tar Archive Without Extracting
You can use the -t
option with tar
to list the contents of an archive without extracting it.
Example:
tar -tvf archive.tar
This command lists all files and directories in the archive.tar
file.
Summary
The gzip
, zip
, and tar
commands are essential for file compression and archiving in Linux. gzip
is used for compressing single files, zip
is used for creating compressed archives of multiple files and directories, and tar
is used for archiving multiple files and directories without compression (but can be combined with gzip
for compressed archives). Understanding the differences and common options for these commands allows you to efficiently manage and organize files in Linux.