In this tutorial, we will show you various Linux and Unix Tar command examples that might be helping you. For those of you who didn’t know, TAR, short for Tape Archive, is a command-line utility in Linux and Unix-like operating systems used for creating and manipulating archive files. The TAR command can compress and decompress files and directories in various formats, such as .tar, .tar.gz, .tgz, .tar.bz2, .tar.xz, and more. It is often used for creating backups, transferring files, and archiving collections of files and directories.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. If you want to learn about tar
commands in Linux with Examples then this post is ideal for you.
Prerequisites
- A server running one of the following operating systems: Ubuntu-based or RHEL-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
TAR Command Syntax
The basic syntax for the TAR command is as follows:
tar [options] [archive file] [files/directories to be archived]
Here’s what each element of the syntax means:
- tar: the name of the TAR command
- [options]: optional parameters that modify the behavior of the TAR command
- [archive file]: the name and location of the archive file to be created
- [files/directories to be archived]: a list of files and directories to be included in the archive
TAR Command Options
The TAR command comes with a range of options that allow you to modify its behavior. Here are some of the most commonly used options:
- -c: create a new archive file
- -x: extract files from an archive file
- -t: list the contents of an archive file
- -v: verbose output that displays the progress of the TAR command
- -f: specify the name and location of the archive file
- -z: compress the archive file using gzip compression
- -j: compress the archive file using bzip2 compression
- -J: compress the archive file using xz compression
- -p: preserve file permissions and ownerships during archiving
- -C: change the directory before archiving or extracting files
TAR Command Examples
Now that we have covered the basic syntax and options of the TAR command, let’s take a look at some examples.
Creating a TAR Archive
To create a new TAR archive of a directory, use the -c
option followed by the name and location of the archive file and the directory to be archived. Here’s an example:
tar -cvf myarchive.tar mydirectory/
In this example, we create a new TAR archive called myarchive.tar
and archive the contents of the mydirectory/
directory.
Extracting Files from a TAR Archive
To extract files from an existing TAR archive, use the -x
option followed by the name and location of the archive file. Here’s an example:
tar -xvf myarchive.tar
In this example, we extract all the files and directories from the myarchive.tar
archive.
Compressing a TAR Archive
To compress a TAR archive using gzip compression, use the -z
option followed by the name and location of the archive file. Here’s an example:
tar -czvf myarchive.tar.gz mydirectory/
In this example, we create a new TAR archive called myarchive.tar.gz
and compress it using gzip compression.
Changing the Directory Before Archiving
To archive files from a different directory than the current one, use the -C
option followed by the path of the directory to be archived. Here’s an example:
tar -cvf myarchive.tar -C /home/user/mydirectory/
Conclusion
In this article, we discussed what the tar command is, how it works, and provided examples of how to use it. A tar command is a powerful tool that can be used to create and manipulate archive files. It is commonly used in Linux and other Unix-based operating systems and is a tool that every Linux user should be familiar with.