This page covers commands for archiving and compressing files and directories, essential for backups and efficient storage.
41. tar
- The Tape Archiver (Archiving Utility)
Purpose: The tar
command (tape archiver) is widely used to combine multiple files and directories into a single archive file (a "tarball"). It can also compress these archives using various compression algorithms (e.g., gzip, bzip2, xz).
Syntax (Common Usage): tar [OPTIONS] ARCHIVE_FILE [FILE/DIR...]
Key Options:
-c
: (create) Create a new archive.-x
: (extract) Extract files from an archive.-v
: (verbose) List the files processed.-f ARCHIVE_FILE
: Specify the archive file name.-z
: (gzip) Compress/decompress with gzip (for.tar.gz
or.tgz
files).-j
: (bzip2) Compress/decompress with bzip2 (for.tar.bz2
or.tbz
files).-J
: (xz) Compress/decompress with xz (for.tar.xz
or.txz
files).-C DIRECTORY
: Change to a different directory before performing the operation.
Examples:
# Create a gzipped tar archive of a folder tar -czvf my_project.tar.gz my_project/ # Extract a gzipped tar archive tar -xzvf my_project.tar.gz # Create an archive of specific files tar -cvf backup.tar file1.txt file2.pdf # Extract archive to a specific directory tar -xvf backup.tar -C /tmp/restored/
42. gzip
- Compress or Expand Files
Purpose: The gzip
command compresses individual files using the Lempel-Ziv coding (LZ77) algorithm. It typically appends a .gz
extension to the compressed file and removes the original.
Syntax: gzip [OPTIONS] FILE...
Key Options:
-d
: (decompress) Decompress a gzipped file (same asgunzip
).-r
: (recursive) Compress files in subdirectories.-k
: (keep) Keep (don't delete) input files during compression/decompress.
Examples:
gzip my_log_file.txt # Creates my_log_file.txt.gz, deletes original gzip -k document.pdf # Creates document.pdf.gz, keeps original
43. gunzip
- Decompress gzipped files
Purpose: The gunzip
command is used to decompress files compressed with gzip
. It's essentially the same as running gzip -d
.
Syntax: gunzip [OPTIONS] FILE...
Examples:
gunzip my_log_file.txt.gz # Decompresses to my_log_file.txt, deletes .gz file gunzip -k document.pdf.gz # Decompresses, keeps original .gz file
44. zip
- Package and Compress (Archive) Files
Purpose: The zip
command is used to create compressed archive files, typically with a .zip
extension. It's often used for cross-platform compatibility, especially with Windows systems.
Syntax: zip [OPTIONS] ARCHIVE_NAME FILE...
Key Options:
-r
: (recursive) Recurse into directories.-e
: Encrypt the archive (prompts for password).-P PASSWORD
: Use password for encryption.
Examples:
zip my_archive.zip file1.txt file2.pdf zip -r project_backup.zip my_project_folder/ zip -e sensitive_data.zip confidential.doc
45. unzip
- Extract Files from Zip Archives
Purpose: The unzip
command is used to extract files from .zip
archives.
Syntax: unzip [OPTIONS] ARCHIVE_NAME [FILE...]
Key Options:
-d DIRECTORY
: Extract files into a specified directory.-l
: List archive contents without extracting.-P PASSWORD
: Use password for decryption.
Examples:
unzip my_archive.zip unzip -d /tmp/extracted_files project_backup.zip unzip -l report.zip