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/

Comments are closed.