Create a TAR zip file on Linux command line

By January 10, 2015IT Solutions
tar cf archive.tar file1 file2

Create archive archive.tar containing files file1 and file2. Here, the c tells tar you will be creating an archive; the f tells tar that the next option (here it’s archive.tar) will be the name of the archive it creates. file1 and file2, the final arguments, are the files to be archived.

tar tvf archive.tar

List the files in the archive archive.tar verbosely. Here, the t tells tar to list the contents of an archive;v tells tar to operate verbosely; and f indicates that the next argument will be the name of the archive file to operate on.

tar xf archive.tar

Extract the files from archive archive.tar. x tells tar to extract files from an archive; f tells tar that the next argument will be the name of the archive to operate on.

tar xzvf archive.tar.gz

Extract the files from gzipped archive archive.tar.gz verbosely. Here, the z tells tar that the archive will be compressed with gzip.

tar cf archive.tar mydir/

Creates an archive of the directory mydir.

tar czf archive.tar.gz mydir/

Creates an gzip-compressed archive of the directory mydir.

tar xvf archive.tar documents/work/budget.doc

Extract only the file documents/work/budget.doc from the archive archive.tar. Produce verbose output.

tar xvf archive.tar documents/work/

Extract only the directory documents/work/, and any files it contains, from the archive archive.tar. Produce verbose output.

tar xvf archive.tar --wildcards '*.doc'

Extract only files with the extension .doc from the archive archive.tar. The –wildcards option tells tarto interpret wildcards in the name of the files to be extracted; the filename (*.doc) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

tar rvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the existing archive archive.tar. The r option is the same as the long option –append.

tar uvf archive.tar documents/work/budget.doc

Add the file documents/work/budget.doc to the archive archive.tar only if it is newer than the version already in the archive (or does not yet exist in the archive). Here, u is the same as the long option –update.

tar cf - documents/work/ | wc -c

Estimate the file size of an archive of the directory documents/work, but do not create the file. Here, the archive file is specified as a dash (““), which tells tar to send its archived output to the standard output rather than a file on disk. This output is then piped to the wc command, which reports how many bytes (-c) were in the input it received.

Related commands

ar — Create, modify, and extract files from archives.
basename — Strip directory information and suffixes from filenames.
cd — Change the working directory.
chown — Change the ownership of files or directories.
cpio — Copy files to or from archives.
dirname — Strip the filename from a pathname, leaving only the directory component.
ls — List the contents of a directory or directories.
mt — Control magnetic tapes.
zcat — Print the uncompressed contents of compressed files.

Extract specific file from a tar file:

xample: suppose you want file etc/apt/sources.list from etc.tar:

tar -xf etc.tar etc/apt/sources.list

Will extract sources.list and create directories etc/apt under the current directory.

  • You can use the -t listing option instead of -x, maybe along with grep, to find the path of the file you want
  • You can also extract a single directory
  • tar has other options like --wildcards, etc. for more advanced partial extraction scenarios; see man tar