How to use the "tar"command?

Ask a question
In Unix, tar command is useful as it can combine several files into a single uncompressed file. Tar command, also called tape archiving, can bundle several files together and can create an archive folder and this is one of the primary uses of this command. With the help of this command, the individual files in a directory can be unpacked and extracted from the archive file. Not only that, but the command is useful to extract specific files from the archive. For Linux based operating systems, the command is used generally as it plays an important role in storage and distribution.


Archiving files with "tar"


The "tar" command allows you to regroup several files to store them in a single file, still remaining uncompressed.

Archiving folder (or directory) with "tar"

  • tar -vcf archive_name.tar name_of_file
  • tar: the command
  • vcf: the options
  • v :provides a description of archived content (optional)
  • c: to create an archive
  • f: to specify a name for the archive(in parameter)
  • archive_name.tar: name given to the archive (the argument)
  • name_of_file: name of the folder (or directory) to be archived

Unpacking file with tar


The tar command is also used to unpack the files, ie to extract all the individual files contained in an archive file.

Unpack the file with tar


tar -vxf my_file.tar
  • tar: the command
  • vxf: the options
  • v:provides a description of the unpacked files (optional)
  • x:to extract the files
  • f:to designate the archive containing the files (given parameter)
  • my_file.tar: the name of the archive to retrieve
  • Note that:
    • A file having the same name as the "original" folder (and not from the archive) will be created in the current directory (unless you specify another path), overwriting any file of the same name.

Unpacking specific files with "tar"


Example:

You can also unpack only part of a tar archive and extract specific files.
tar -xvf my_archive.tar "*.jpg"


Extract in the file "my_archive" all files with the .jpg extension ".

For a list of all files in the archive, use the "-t".

tar -tf my_file.tar
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Related