Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❀ đŸ’»

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Bash - compress files with tar command in Linux

0 contributions
4 points

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/.

    0 contributions

    Checkout latest Findings & News:

    Checkout latest questions:

    Checkout latest wiki articles:

    Hey 👋
    Would you like to know what we do?
    • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
    • We welcome everyone,
      no matter what the experience,
      no matter how basic the question is,
      this community will help you.
    Read more