How to untar and tar files

posted in: Linux-Unix | 0

tar was and is a program that creates Archive files. tar is a abbreviation for “Tape ARchive”.
When tar is used to create a archive file, that file is called a tarball or a .tar file. Tarball file is just like a ZIP file, but does not compress the files inside of it. Tarball files are used primarily in a UNIX and LINUX environments.
tape drive

 

One of the first uses for a tar file was to backup files to a tape media. Tape media is a long strip of plastic film that has a magnetizable coating that can be edited with a use of a tape drive.

Tape is still used, but the main point for tar files is a way to put many files into one, so they can all be downloaded. These files end with a .tar file.

Below is the syntax use to the tar command.
1. Extract files
To extract files from a tar file, or to get the files out of the tarball file:
x is for extract (This switch will extract files from the tar file)
v is for verbose (This switch lists the files processed by the tar command)
f is for file (This switch tells tar what file or device to use)
tar xvf something.tar

This command is used to extract files inside of a compressed tarball file that is using the gzip compression algorithm.
x is for extract (This switch will extract files from the tar file)
v is for verbose (This switch lists the files processed by the tar command)
f is for file (This switch tells tar what file or device to use)
z is for compress (This filters the file through the compression algorithm)
tar xvfz something.tar.gz

This command is used to extract only some directories inside of a compressed tarball file that is using the gzip compression algorithm.
x is for extract (This switch will extract files from the tar file)
v is for verbose (This switch lists the files processed by the tar command)
f is for file (This switch tells tar what file or device to use)
z is for filter to gzip (This filters the file through the gzip compression algorithm)
tar xvzf something.tar.gz */dir.you.want/*

2. Create Files
This command is used to extract files inside of a compressed tarball file that is using the bzip2 compression algorithm.
x is for extract (This switch will extract files from the tar file)
v is for verbose (This switch lists the files processed by the tar command)
f is for file (This switch tells tar what file or device to use)
j is for filter to bzip2 (This filters the file through the bzip2 compression algorithm.)
tar xvjf something.tar.bz2

This command creates a .tar.gz file. (A tarball file using the gzip compression algorithm.)
c is for create (This creates a new file)
f is for file (This switch tells tar what file or device to use)
z is for filter to gzip (This filters the file through the gzip compression algorithm)
tar cfz somthing.tar.gz /directory

This command creates a .tar.gz file. (A tarball file using the bzip2 compression algorithm)
c is for create (This creates a new file)
f is for file (This switch tells tar what file or device to use)
j is for filter to bzip2 (This filters the file through the bzip2 compression algorithm)
tar cfj somthing.tar.gz

3. Listing out the content.
This command lists the files inside a .tar.gz file (A tarball file using the gzip compression algorithm.)
t is for list (This switch tells tar to just list the files)
f is for file (This switch tells tar what file or device to use)

tar -tf somthing.tar

This command lists the files inside a .tar.gz file (A tarball file using the gzip compression algorithm.)
t is for list (This switch tells tar to just list the files)
f is for file (This switch tells tar what file or device to use)
z is for filter to gzip (This filters the file through the gzip compression algorithm)
tar -tfz somthing.tar.gz

This command lists the files inside a .tar.bz2 file (A tarball file using the bzip2 compression algorithm.)
t is for list (This switch tells tar to just list the files)
f is for file (This switch tells tar what file or device to use)
j is for filter to gzip (This filters the file through the bzip2 compression algorithm)
tar -tfj somthing.tar.bz2

Some advanced uses of tar
This command creates a .tar.bz2 file and names it using today’s date for doing daily snapshots of the home folders.
tar cjf homedirs-$(date +%Y%m%d).tar.bz2 /home

Just a interesting thing regarding TAR it does not create fully POSIX-compliant archives this means that it does not reliably reconstructing file access rights. A program that does that is called pax.

More into on the tar file can be found at:

  • http://linuxcommand.org/man_pages/tar1.html

More info on tapes and tape drives can be found at.

  • http://sio.co/blog/audio-format-guide/