The tar command stands for tape achieve; the most commonly used tape drive backup command used by the Linux/Unix system.
For new users and students learning to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS.
Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.
When you’re ready to learn how to use the tar commands, follow the guide below:
About tar command:
The tar command stands for tape achieve; the most commonly used tape drive backup command used by the Linux/Unix system. It allows you to quickly gather and save files into a highly compressed archive file commonly called a tarball.
Gzip has become the most popular algorithm for compressing tar files. In short, a file that ends with .tar.gz is an archive compressed with gzip algorithm.
The same way you use your mouse and keyboard to create archived or zipped files and folders. The tar is the way to do it on the command line.
The syntax is the rule and format of how the tar command can be used. These syntax options can be reordered, but a straight format must be followed.,.
Below is an example syntax of how to use the tar command.
tar [OPTION.] [FILE].
The command line options are switches or flags that determine how the commands are executed or controlled. They modify the behavior of the commands. They are separated by spaces and followed after the commands.
Below are some options for the tar command:
|FILES.||Replace FILES.. with the files you wish to archive using the tar command.|
|-c, –create||Use the -c or –create a new archive|
|-f, –file||Use the -f or –file to create a file archive|
|-x, –extract||Use the -x or –extract to extract files from an archive|
|-t, –list||Use the -t or –list to displays or lists files in archived file|
|-u, –update||Use the -u or –update to only append files newer than copy in existing archive|
|-j, –bzip2||Use the -j or –bzip2 to create a new archive through bzip2|
|-z, –gzip, –gunzip||Use the -z or –gzip options to create a new archive through tar.gz|
|-C, –directory=DIR||Use the -C or –directory to create a new archive using the directory specified|
|-v, –verbose||Use the -v or –verbose option output a diagnostic for every directory processed|
|–help||Use the –help option to display this help and exit|
|–version||Use the –version option to output version information and exit|
Below are some examples of how to run and use the tar on Ubuntu Linux.
If you want to create an archived file called confidential.tar from files called private and secrets in the same directory, you will run the commands below.
tar -cf confidential.tar private secrets
You run the commands to create a new archive file using the tar.gz algorithm from the /documents/public/students directory. First, you’ll need to use the command with option -C and specify the directory.
tar -czf confidential.tar.gz -C /documents/public/students
If you want to extract an archived file to a specified directory, you run the commands below:
tar -xf confidential.tar.gz -C /home/students/files
If you want to extract the confidential.tar.gz file to the same directory you’re working in, you run the commands below:
tar -xf confidential.tar.gz
If you’re not logged in as a root account, you may have to use the sudo command.
sudo tar -czf confidential.tar.gz -C /documents/public/students
When you run tar with the –help option, you’ll see the help text below:
Usage: tar [OPTION.] [FILE]. GNU 'tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive. Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf archive.tar # List all files in archive.tar verbosely. tar -xf archive.tar # Extract all files from archive.tar. Local file name selection: --add-file=FILE add given FILE to the archive (useful if its name starts with a dash) -C, --directory=DIR change to directory DIR --exclude=PATTERN exclude files, given as a PATTER
Congratulations! You’ve learned to use the tar command to create and extract archives files.