# Cheat Sheet #day56 - tar

# `tar` Command Cheatsheet

The `tar` command is used to create, manipulate, and extract archive files. It is commonly used for bundling multiple files into a single file for easy distribution and backup.

## Syntax

```bash
tar [OPTION]... [FILE]...
```

## Common Options

* `-c`: Create a new archive.
    
* `-x`: Extract an archive.
    
* `-t`: List the contents of an archive.
    
* `-f FILE`: Specify the archive file name.
    
* `-v`: Verbose mode, display the progress.
    
* `-z`: Filter the archive through gzip (use with `.tar.gz`).
    
* `-j`: Filter the archive through bzip2 (use with `.`[`tar.bz`](http://tar.bz)`2`).
    
* `-J`: Filter the archive through xz (use with `.tar.xz`).
    
* `--delete`: Delete files from the archive.
    

## Examples

### Create a Tar Archive

```bash
tar -cf archive.tar file1.txt file2.txt
```

* Creates `archive.tar` containing `file1.txt` and `file2.txt`.
    

### Create a Tar.gz Archive

```bash
tar -czf archive.tar.gz file1.txt file2.txt
```

* Creates `archive.tar.gz` with gzip compression.
    

### Create a [Tar.bz](http://Tar.bz)2 Archive

```bash
tar -cjf archive.tar.bz2 file1.txt file2.txt
```

* Creates [`archive.tar.bz`](http://archive.tar.bz)`2` with bzip2 compression.
    

### Create a Tar.xz Archive

```bash
tar -cJf archive.tar.xz file1.txt file2.txt
```

* Creates `archive.tar.xz` with xz compression.
    

### Extract a Tar Archive

```bash
tar -xf archive.tar
```

* Extracts `archive.tar`.
    

### Extract a Tar.gz Archive

```bash
tar -xzf archive.tar.gz
```

* Extracts `archive.tar.gz`.
    

### Extract a [Tar.bz](http://Tar.bz)2 Archive

```bash
tar -xjf archive.tar.bz2
```

* Extracts [`archive.tar.bz`](http://archive.tar.bz)`2`.
    

### Extract a Tar.xz Archive

```bash
tar -xJf archive.tar.xz
```

* Extracts `archive.tar.xz`.
    

### List Contents of an Archive

```bash
tar -tf archive.tar
```

* Lists contents of `archive.tar`.
    

### Verbose Listing

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

* Lists contents of `archive.tar` with details.
    

### Extract to a Specific Directory

```bash
tar -xf archive.tar -C /path/to/directory
```

* Extracts `archive.tar` to `/path/to/directory`.
    

### Add Files to an Existing Archive

```bash
tar -rf archive.tar newfile.txt
```

* Adds `newfile.txt` to `archive.tar`.
    

### Delete Files from an Archive

```bash
tar --delete -f archive.tar file_to_delete.txt
```

* Deletes `file_to_delete.txt` from `archive.tar`.
    

## Combining Options

You can combine options for more concise commands. For example:

```bash
tar -czvf archive.tar.gz file1.txt file2.txt
```

* Creates `archive.tar.gz` with gzip compression, verbose mode.
    

---

This cheatsheet covers the most common usages and options for the `tar` command. For more detailed information, refer to the `tar` man page by typing `man tar` in your terminal.
