araz@blog: ~/posts/working-with-tar-compressing-and-extracting-files-in-linux — bash — 132×42
tty1
araz@blog:~/posts$cat working-with-tar-compressing-and-extracting-files-in-linux.md
┌───────┐
│  TAR  │
└───────┘

Working with tar: Compressing and Extracting Files in Linux

share: http://blog.araz.me/posts/working-with-tar-compressing-and-extracting-files-in-linux telegram x/twitter linkedin mail
less working-with-tar-compressing-and-extracting-files-in-linux.md markdown · utf-8
## Introduction The `tar` command in Linux is a powerful tool used for archiving and compressing files. It allows users to combine multiple files into a single archive, making it easier to manage and transfer them. This tutorial provides a step-by-step guide on how to use the `tar` command for both compression and extraction, along with practical examples. ## Creating a tar Archive To create a tar archive, you can use the `tar` command followed by the `-c` (create) option, along with the `-f` (file) option to specify the name of the archive. The general syntax is: ```bash tar -cf archive_name.tar /path/to/directory_or_files ``` ### Example: Creating a Basic tar Archive Let's create a tar archive named `my_files.tar` that contains all files from a directory named `my_folder`: ```bash tar -cf my_files.tar my_folder/ ``` After running this command, you will have a file named `my_files.tar` in your current directory, which contains all files from `my_folder`. ## Compressing a tar Archive To compress a tar archive, you can add the `-z` option to use gzip compression or the `-j` option for bzip2 compression. Here’s how to do it: ### Example: Creating a Compressed tar Archive with gzip To create a compressed tar archive using gzip, use the following command: ```bash tar -czf my_files.tar.gz my_folder/ ``` This command creates a gzipped tar archive named `my_files.tar.gz`. The `-z` option tells `tar` to compress the archive using gzip. ### Example: Creating a Compressed tar Archive with bzip2 For bzip2 compression, use the `-j` option: ```bash tar -cjf my_files.tar.bz2 my_folder/ ``` This creates a bzip2 compressed tar archive named `my_files.tar.bz2`. ## Extracting a tar Archive To extract files from a tar archive, you can use the `-x` (extract) option along with the `-f` option to specify the archive name. The syntax is: ```bash tar -xf archive_name.tar ``` ### Example: Extracting a tar Archive To extract the contents of the `my_files.tar` archive, run: ```bash tar -xf my_files.tar ``` ### Extracting Compressed tar Archives The extraction commands for compressed archives are similar. For example, to extract a gzipped tar archive, use: ```bash tar -xzf my_files.tar.gz ``` For bzip2 compressed archives, the command is: ```bash tar -xjf my_files.tar.bz2 ``` ## Listing Contents of a tar Archive If you want to see what files are contained within a tar archive without extracting them, you can use the `-t` (list) option: ### Example: Listing Contents To list the contents of a tar archive, use: ```bash tar -tf my_files.tar ``` For compressed archives, the commands are: ```bash tar -tzf my_files.tar.gz ``` ```bash tar -tjf my_files.tar.bz2 ``` ## Additional tar Options The `tar` command has several other options that can enhance its functionality: - `-v`: Verbose mode. Displays the progress in the terminal during compression/extraction. - `--exclude`: Excludes specified files from the archive. ### Example: Creating a tar Archive with Exclusions To create an archive while excluding certain files, use the following command: ```bash tar -czf my_files.tar.gz --exclude='*.tmp' my_folder/ ``` This command will create a gzipped tar archive excluding all `.tmp` files from `my_folder`. ## Takeaways - The `tar` command is essential for creating and managing archives in Linux. - Compression can be achieved with gzip or bzip2. - Extracting and listing contents can be done easily with the right options. - Additional features allow for more control over what gets archived. - Understanding these commands enhances file management capabilities in Linux.

── EOF ── end of working-with-tar-compressing-and-extracting-files-in-linux.md ──

cd .. ls ./Linux & DevOps gg

araz@blog:~$subscribe --newsletter
newsletter.service enabled

Liked this post? Get the next one in your inbox.

email:
READ main working-with-tar-compressing-and-extracting-files-in-linux.md 3 min read · Linux & DevOps Top --:--:--