Crmgo Blog

tar with examples

tar with examples

Creating an archive using the tar command Without compression: tar cvf files.tar dir/ To use gzip compression, add the z flag, and for bzip2 compression, use the j flag. tar cvzf files.tar.gz dir/ gzip and bzip2: bzip2 works slower than gzip,…

tar with examples
    1. Creating an archive using the tar command

Without compression:
tar cvf files.tar dir/
To use gzip compression, add the z flag; to use bzip2 compression, use the j flag.
tar cvzf files.tar.gz dir/

gzip and bzip2: bzip2 is slower than gzip, but it produces smaller archive sizes.

* c – create a new archive
* v – display a list of processed files.
* f – specify that the archive container will be a file with the following name.
* z – compress the archive using gzip.
* j – compress the archive using bzip2.
* x – extract files from the archive.

    1. Extracting files using tar

Extract data from a *.tar file using the xvf options:
tar xvf files.tar

To extract files from archives created with gzip or bzip2, use the z or j flags, respectively.

    1. Listing the contents of an archive

Archive without compression:
tar tvf files.tar
To view the listing of an archive compressed with gzip or bzip2, add the z or j flags, respectively.

    1. Extracting a specific file from a tar archive

Archive without compression:
tar -xvf files.tar /location/of/the/file
Similarly, to use compression, use the same z or j flags.

    1. Extracting a specific directory from an archive

To extract a specific directory (as well as nested subdirectories and files) from a tar archive, you must specify the name of the folder to be extracted when using the tar command.
tar -xvf container.tar /where/the/file/is
Extract several folders from a tar archive:
tar -xvf files.tar /location/of/the/dir/ /location/of/the/dir2/
As always, use the z and j flags to work with compressed archives:

    1. Regular expressions when working with tar

The following command will extract all files with the .doc extension from the archive.
tar -xvf files.tar --wildcards '*.doc'
where * – wildcards *.doc – a pattern to select files with the .doc extension.

    1. Add a file or folder to an existing archive using the -r flag

Add a specific file or folder to an existing archive:
tar rvf files.tar some_our_file
The file some_our_file will be added to the archive named files.tar. Adding a directory is done similarly:
tar rvf files.tar some_our_dir/
You cannot add a file or folder to an archive created with compression.

    1. Verify data in the archive file using the -W flag

After creating an archive, it is useful to verify it using the W flag:
tar cvfW files.tar dir/
However, as in the previous case, verification will fail if you are dealing with an archive created using compression.

Sometimes, when dealing with very large tar archives (.tar, .tar.gz, .tar.bz2), you need to extract not all of the contents, but only a few specific files. In such cases, it is impractical to extract the entire original tar archive, as this would not only consume significant resources (primarily disk I/O) but also take a considerable amount of time.

Of course, the tar developers have provided the ability to extract only specified objects from an archive. It is quite straightforward – simply list the required files as arguments to tar after the archive name. For example:

tar -xf archive.tar myfile1 dir2/myfile2

This command will extract the files myfile1 and dir2/myfile2 from the archive archive.tar. For compressed archives, the process is similar—you just add the appropriate flags: “z” for .gz (gzip) and “j” for .bz2 (bzip2).

The only potential difficulty lies in specifying the exact filename or full path of the file within the archive. This issue can be resolved by using the functionality to view the tree of files/directories stored in the archive. For example:

tar -tf archive.tar

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.

Categories: tar