The tar utility is used to create and manipulate tar archives. An archive is a file containing the contents of many files. In this article we will look at the basic commands for working with tar on the command line.

What is a tar.gz File?

Initially, tar archives were used to compactly store files on a tape — hence the term tar (tape archiver). Regardless of the name of the utility, tar can send its output to any available device, and also store it in a file or send it to another program through a channel. tar may also have access to remote devices and files.

In addition, the archive identifies the file names, their owners, etc. (archives also record access permissions, user and group, size in bytes, and time of last modification. Some archives also contain file names in each archived directory and information about directories).

Before we start, here are some options used with the tar Terminal command:

  • -c – create a new archive;
  • -f – in combination with the -c option is used to create a tar file from the specified file, in conjunction with the option -x is used to unzip the specified file;
  • -t – show a list of files in the tar file;
  • -v – show the progress of the archiving process;
  • -x – extract files from the archive;
  • -z – compress the tar file with the gzip program;
  • -j – compress the tar file with the bzip2 program.

tar.gz Commands

The command syntax for creating and unpacking the archive is almost the same (including with the bzip2 or gzip compression utilities).

Create a tar.gz file, type in the command line:

tar -cvf filename.tar path/to/directory/or/file

In this example, the file_name.tar is the archive file being created, the directory/file is the directory or file to be placed in the archive file.

You can make a tarball file from several files and directories at the same time by listing them separated by spaces:

tar -cvf filename.tar path/to/directory1/or/file1 path/to/directory2/or/file2

This command will place all files from the directory1 and directory2 directories into a new file called filename.tar in the current directory.

Read more: How to Zip Files in macOS?

Create an archive backups.tar.gz from the path/to/directory/you/want/backup with all files and folders inside, but Exclude from archiving directories with names EXCLUDE1, EXCLUDE2:

tar -cvzf backups.tar.gz --exclude=EXCLUDE1 --exclude=EXCLUDE2 directory/backup

View the contents of a file, type in the command line:

tar -tvf filename.tar

Extract the contents of a tar file, type:

tar -xvf filename.tar

This command creates an archive file filename.tar and compresses it into file filename.tgz (file filename.tar is not saved). If you decompress file_name.tgz with the gunzip command, file_name.tgz is deleted and replaced with file_name.tar.

You can extract the tarball a gzip tar file with one command:

tar -xzvf filename.tgz

Conclusion

This article examined the tar command, which is used to archive files and is supplied by default not only in MacOS but also in Linux systems. Its capabilities include creating and unpacking an archive of files without compressing them. The utility is used for compression in conjunction with the popular bzip2 and gzip compressors. All of the above can be safely applied in Linux.

It’s the best time to ask questions and give us your feedback in comments.

Author

Hi there! My Name is Vincent Lago! My goal is to share insightful reviews, guides, and manuals for people looking to know more about current solutions for Apple hardware and OS. I have years of technological background knowledge including owning a custom iMacs & PCs which was using for different types of business. And now, I want to share my experience with you.

Write A Comment