How to Use the tar command to Create and Extract Tar.gz Files on Ubuntu 16.04 | 18.04

For new users and students learning how to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS… Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices…

The same way one uses a mouse and keyboard to manage files and folders or perform others simple tasks on Microsoft Windows OS, Ubuntu also makes that easy for you… If you think Windows is easy to use, then Ubuntu might be just what you need and for other beginners.

However, when you’re learning to use and understand Ubuntu Linux, you’ll also want to learn the commands behind the GUI interface and mouse-clicking — and how to use them…  This tutorial is going to show you how…..

This post shows new users and students what the tar command is used for and how to use it…

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, which is the most commonly used tape drive backup command used by the Linux/Unix system…  It allows you to quickly gather files and save them into a highly compressed archive file commonly called tarball.

       Gzip has become the most popular algorithm for compressing tar files… In short, a file that ends in .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…

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 determined 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 of the tar command:

   FILES… Replace FILES….. with the files you wish to archive using the tar command..
-c, –create
Use the -c or –create 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

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

To create a new archive file using the tar.gz algorithm from the /documents/public/students  directory, you run the commands.. You’ll need to use the command with option -C and specified 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 just 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 it it…

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 PATTERN
      --exclude-backups      exclude backup and lock files
      --exclude-caches       exclude contents of directories containing
                             CACHEDIR.TAG, except for the tag file itself
      --exclude-caches-all   exclude directories containing CACHEDIR.TAG
      --exclude-caches-under exclude everything under directories containing
                             CACHEDIR.TAG
      --recursion            recurse into directories (default)
  -T, --files-from=FILE      get names to extract or create from FILE
      --unquote              unquote input file or member names (default)
      --verbatim-files-from  -T reads file names verbatim (no option handling)
  -X, --exclude-from=FILE    exclude patterns listed in FILE

 File name matching options (affect both exclude and include patterns):

      --anchored             patterns match file name start
      --ignore-case          ignore case
      --no-anchored          patterns match after any '/' (default for
                             exclusion)
      --no-ignore-case       case sensitive matching (default)
      --no-wildcards         verbatim string matching
      --no-wildcards-match-slash   wildcards do not match '/'
      --wildcards            use wildcards (default for exclusion)
      --wildcards-match-slash   wildcards match '/' (default for exclusion)

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
      --test-label           test the archive volume label and exit
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive

 Operation modifiers:

      --check-device         check device numbers when creating incremental
                             archives (default)
  -g, --listed-incremental=FILE   handle new GNU-format incremental backup
  -G, --incremental          handle old GNU-format incremental backup
      --hole-detection=TYPE  technique to detect holes
      --ignore-failed-read   do not exit with nonzero on unreadable files
      --level=NUMBER         dump level for created listed-incremental archive
  -n, --seek                 archive is seekable
      --no-check-device      do not check device numbers when creating
                             incremental archives
      --no-seek              archive is not seekable
      --occurrence[=NUMBER]  process only the NUMBERth occurrence of each file
                             in the archive; this option is valid only in
                             conjunction with one of the subcommands --delete,
                             --diff, --extract or --list and when a list of
                             files is given either on the command line or via
                             the -T option; NUMBER defaults to 1
      --sparse-version=MAJOR[.MINOR]
                             set version of the sparse format to use (implies
                             --sparse)
  -S, --sparse               handle sparse files efficiently

 Overwrite control:

  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --keep-directory-symlink   preserve existing symlinks to directories when
                             extracting
      --keep-newer-files     don't replace existing files that are newer than
                             their archive copies
      --no-overwrite-dir     preserve metadata of existing directories
      --one-top-level[=DIR]  create a subdirectory to avoid having loose files
                             extracted
      --overwrite            overwrite existing files when extracting
      --overwrite-dir        overwrite metadata of existing directories when
                             extracting (default)
      --recursive-unlink     empty hierarchies prior to extracting directory
      --remove-files         remove files after adding them to the archive
      --skip-old-files       don't replace existing files when extracting,
                             silently skip over them
  -U, --unlink-first         remove each file prior to extracting over it
  -W, --verify               attempt to verify the archive after writing it

 Select output stream:

      --ignore-command-error ignore exit codes of children
      --no-ignore-command-error   treat non-zero exit codes of children as
                             error
  -O, --to-stdout            extract files to standard output
      --to-command=COMMAND   pipe extracted files to another program

 Handling of file attributes:

      --atime-preserve[=METHOD]   preserve access times on dumped files, either
                             by restoring the times after reading
                             (METHOD='replace'; default) or by not setting the
                             times in the first place (METHOD='system')
      --clamp-mtime          only set time when the file is more recent than
                             what was given with --mtime
      --delay-directory-restore   delay setting modification times and
                             permissions of extracted directories until the end
                             of extraction
      --group=NAME           force NAME as group for added files
      --group-map=FILE       use FILE to map file owner GIDs and names
      --mode=CHANGES         force (symbolic) mode CHANGES for added files
      --mtime=DATE-OR-FILE   set mtime for added files from DATE-OR-FILE
  -m, --touch                don't extract file modified time
      --no-delay-directory-restore
                             cancel the effect of --delay-directory-restore
                             option
      --no-same-owner        extract files as yourself (default for ordinary
                             users)
      --no-same-permissions  apply the user's umask when extracting permissions
                             from the archive (default for ordinary users)
      --numeric-owner        always use numbers for user/group names
      --owner=NAME           force NAME as owner for added files
      --owner-map=FILE       use FILE to map file owner UIDs and names
  -p, --preserve-permissions, --same-permissions
                             extract information about file permissions
                             (default for superuser)
      --same-owner           try extracting files with the same ownership as
                             exists in the archive (default for superuser)
  -s, --preserve-order, --same-order
                             member arguments are listed in the same order as
                             the files in the archive
      --sort=ORDER           directory sorting order: none (default), name or

That’s it!

Congratulations! You’ve learned how to use the tar command to create and extract archives files…

You may also like the commands below:

Install Zoia CMS (Node.js) Framework on Ubuntu 16.04 / 18.04

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.