Unix tar command

About tar

Create tape archives and add or extract files.

Examples

Note: A “.tar” file is not a compressed files, it is actually a collection of files within a single file uncompressed. If the file is a .tar.gz (“tarball“) or “.tgz” file it is a collection of files that is compressed. If you are looking to compress a file you would create the tar file then gzip the file.

Creating a tar file:

tar -cvvf file.tar myfile.txt

In the above example the system would create a tar named file.tar in the directory you currently are in. Wildcards could also be used in this command, for example: tar -cvvf file.tar *.txt would compress all txt files in the current directory.

tar -cvvf home.tar home/

In the above example command the system would create a tar file named home.tar in the directory you currently are in of the home directory.

Extracting the files from a tar file:

tar -xvvf myfile.tar

In the above example command the system would uncompress (untar) the myfile.tar file in the current directory.

tar -xvvzf myfile.tar.gz

In the above example command the system would uncompress (untar) the myfile.tar.gz file in the current directory.

Note: There is no “untar” linux / unix command.

Creating a tarred file that is compressed with bzip

tar -cjvf test.tbz home/

Adding the j option to the tar command enables tar to compress files and/or directories using bzip. In the above example the home directory and all its subdirectories are added to the compressed test.tbz file.

2 comments on “Unix tar command

Leave a comment