Using the tar command to archive files and directories

This is again a command that I keep forgetting the syntax for, so I will put the most basic syntax here:

Tar command syntax:

tar <options> <output-file> <input-file>

Creating a tar file from wildcard matching:

tar -cvf file.tar *.xml

This will create a tar archive with all files ending with ‘.xml’ in it. The -c option stands for create, -v option for verbose (echo what files being used) and -f option for file (standard is stdin)

Creating a tar file from a directory:

tar -cvf dir.tar dir/

This will create a tar file of all files in directory dir.

Extracting tar files:

tar -xvf file.tar

This will extract the contents of the file.tar file to the current directory. The -x option stands for extract

Extracting a gzip tar archive:

tar -xvzf myfile.tar.gz

The -z option here tells tar to run the archive through gzip

Creating a tar file with gzip compression:

tar -czvf dir.tar.gz dir/

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.