Rocky Linux Core Commands: Page 9

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:

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:

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:

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:

Examples:

unzip my_archive.zip
unzip -d /tmp/extracted_files project_backup.zip
unzip -l report.zip