Languages
[Edit]
EN

Bash - compress files with tar command in Linux

4 points
Created by:
Nataniel-Barclay
499

In this short article we are going to show how with tar command we are able to pack or compress files in Linux.

Quick solution:

tar -czvf new-archive-name.tar.gz /path/to/existing-directory-or-file

We can select two options:

  • packing files together without compression,
  • packing files together with compression.

Second one option is more useful when we want to move our files quickly between some devices.

 

1. tar command with compression

Below example will create compressed archive (gzip algorithm will be used), printing progress in console:

tar -czvf new-archive-name.tar.gz /path/to/existing-directory-or-file

or as:

tar -c -z -v -f new-archive-name.tar.gz /path/to/existing-directory-or-file

Where:

  • -c creates new archive for indicated files (required),
  • -z uses gzip compression (optional),
  • -v displays in terminal progress, printing file paths - it is verbose mode (optional),
  • -f allows to select output archive file path (optional).

Example console output:

root@debian:~# tar -czvf reports_2020-01.tar.gz reports/2020-01/
reports/2020-01/log.1.txt
reports/2020-01/log.2.txt
reports/2020-01/log.3.txt
reports/2020-01/log.4.txt
reports/2020-01/users/log.1.txt
reports/2020-01/users/log.2.txt
reports/2020-01/users/log.3.txt
#

    When we don't want to print file paths we can remove -v parameter:

    tar -czf new-archive-name.tar.gz /path/to/existing-directory-or-file

    Example console output:

    root@debian:~# tar -czvf reports_2020-01.tar.gz reports/2020-01/
    #

    2. tar command without compression

    Below example will create not compressed archive, printing progress in console:

    tar -cvf new-archive-name.tar /path/to/existing-directory-or-file

    Where -z parameter was removed to turn off compression mode.

    Example console output:

    root@debian:~# tar -cvf reports_2020-01.tar reports/2020-01/
    reports/2020-01/log.1.txt
    reports/2020-01/log.2.txt
    reports/2020-01/log.3.txt
    reports/2020-01/log.4.txt
    reports/2020-01/users/log.1.txt
    reports/2020-01/users/log.2.txt
    reports/2020-01/users/log.3.txt
    #

    Similar to above example when we don't want to print file paths we can remove additionally -v parameter:

    tar -cf new-archive-name.tar /path/to/existing-directory-or-file

    Example console output:

    root@debian:~# tar -cf reports_2020-01.tar reports/2020-01/
    #

    3. tar and many input file paths

    It is possible to indicate many paths to files or directories in following way:

    tar -czvf new-archive-name.tar.gz /path/to/existing-directory /path/to/existing-file-1 /path/to/existing-file-2 /path/to/other/place/*.txt

    That will compress existing-directory, existing-file-1, existing-file-2 and all txt files for located in the /path/to/other/place/.

    See also

    1. Bash - extract *.tar.gz file in Linux
    Donate to Dirask
    Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
    Join to our subscribers to be up to date with content, news and offers.

    Bash

    Bash - compress files with tar command in Linux
    Native Advertising
    🚀
    Get your tech brand or product in front of software developers.
    For more information Contact us
    Dirask - we help you to
    solve coding problems.
    Ask question.

    ❤️💻 🙂

    Join