How to use tar command in Ubuntu Linux with examples

The tar (tape archive) command is a commonly used backup command in the Linux/Unix system that creates highly compressed archive files, also known as tarballs. The command offers various options for creating, extracting, and managing these files. Ubuntu, an open-source Linux operating system, is a suitable platform for beginners learning to use the tar command.…

This article explains how to use the tar command on Ubuntu Linux.

The tar command stands for tape achieve; the most commonly used tape drive backup command used by the Linux/Unix system. 

Using the tar command in Ubuntu Linux is important for anyone working with files and directories in a Linux environment. The tar command is used to archive and compress files and directories into a single file, making it easier to manage and transfer them.

The tar command is an essential tool for system administrators, developers, and anyone who works with Linux regularly. Knowing how to use the tar command can help you save time and effort by automating tasks and managing files more efficiently.

Additionally, learning how to use the tar command in Ubuntu Linux can help you better understand the Linux operating system and its command-line interface.

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 lets you quickly gather and save files into a highly compressed tarball archive file.

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 the 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.

Syntax:

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].

Options:

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, –fileUse the -f or –file to create a file archive
-x, –extractUse the -x or –extract to extract files from an archive
-t, –listUse the -t or –list to displays or lists files in archived file
-u, –updateUse the -u or –update to only append files newer than copy in existing archive
-j, –bzip2Use 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=DIRUse 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
–helpUse the –help option to display this help and exit
 –versionUse the –version option to output version information and exit

Examples:

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

That’s it!

Congratulations! You’ve learned to use the tar command to create and extract archive files.

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version