> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/linux-commands/compression-and-archiving.md).

# Compression and Archiving

## **`bzip2`** :- Compress Files Using Bzip2

**Examples:**

* Compress a single file:

  ```bash
  bzip2 filename.txt
  ```
* Compress multiple files:

  ```bash
  bzip2 file1.txt file2.txt file3.txt
  ```
* Keep the original file while compressing:

  ```bash
  bzip2 -k filename.txt
  ```
* Compress with maximum compression level:

  ```bash
  bzip2 -9 filename.txt
  ```

***

### **`bunzip2`** :- Decompress Bzip2 Files

**Examples:**

* Decompress a `.bz2` file:

  ```bash
  bunzip2 filename.txt.bz2
  ```
* Decompress multiple files:

  ```bash
  bunzip2 file1.txt.bz2 file2.txt.bz2
  ```
* Keep the compressed file while decompressing:

  ```bash
  bunzip2 -k filename.txt.bz2
  ```

***

## **`gzip`** :- Compress Files Using Gzip

**Examples:**

* Compress a file:

  ```bash
  gzip filename.txt
  ```
* Compress multiple files:

  ```bash
  gzip file1.txt file2.txt
  ```
* Compress with maximum compression level:

  ```bash
  gzip -9 filename.txt
  ```
* Keep the original file while compressing:

  ```bash
  gzip -k filename.txt
  ```

***

### **`gunzip`** :- Decompress Gzip Files

**Examples:**

* Decompress a `.gz` file:

  ```bash
  gunzip filename.txt.gz
  ```
* Decompress multiple files:

  ```bash
  gunzip file1.txt.gz file2.txt.gz
  ```
* Keep the compressed file while decompressing:

  ```bash
  gunzip -k filename.txt.gz
  ```

***

## **`zip`** :- Compress Files into a ZIP Archive

**Examples:**

* Create a ZIP archive:

  ```bash
  zip archive.zip file1.txt file2.txt
  ```
* Compress a directory recursively:

  ```bash
  zip -r archive.zip my_directory/
  ```
* Create a password-protected ZIP file:

  ```bash
  zip -e secure.zip file.txt
  ```
* Exclude specific files while zipping:

  ```bash
  zip -r archive.zip my_directory/ -x "*.log"
  ```

***

### **`unzip`** :- Extract ZIP Files

**Examples:**

* Extract a ZIP archive:

  ```bash
  unzip archive.zip
  ```
* Extract to a specific directory:

  ```bash
  unzip archive.zip -d /destination/path
  ```
* List the contents of a ZIP file without extracting:

  ```bash
  unzip -l archive.zip
  ```
* Extract only specific files:

  ```bash
  unzip archive.zip file1.txt file2.txt
  ```

***

## **`7z`** :- Compress and Extract 7z Archives

**Examples:**

* Create a 7z archive:

  ```bash
  7z a archive.7z file1.txt file2.txt
  ```
* Compress a directory into a 7z file:

  ```bash
  7z a archive.7z my_directory/
  ```
* Extract a 7z file:

  ```bash
  7z x archive.7z
  ```
* List contents of a 7z archive:

  ```bash
  7z l archive.7z
  ```
* Extract to a specific directory:

  ```bash
  7z x archive.7z -o/destination/path
  ```

***

## **`tar`** :- Archive and Extract Tar Files

**Examples:**

* Create a `.tar` archive:

  ```bash
  tar -cvf archive.tar file1.txt file2.txt
  ```
* Create a compressed `.tar.gz` archive:

  ```bash
  tar -czvf archive.tar.gz my_directory/
  ```
* Extract an `.tar` archive:

  ```bash
  tar -xvf archive.tar
  ```
* Extract a `.tar.gz` archive:

  ```bash
  tar -xzvf archive.tar.gz
  ```
* Extract to a specific directory:

  ```bash
  tar -xzvf archive.tar.gz -C /destination/path
  ```
* List the contents of a tar archive:

  ```bash
  tar -tvf archive.tar
  ```
