Cyberithub

20+ Practical tar command examples for Linux Administrators

Advertisements

In this article, we will see 20+ practical tar command examples which can be used by linux administrators. The tar command in Linux is a widely used tool for archiving multiple files into a single archive file, often referred to as a tarball. The name "tar" stands for tape archive, a throwback to when files were backed up to magnetic tape. This command is essential for file compression and archiving in Linux environments. While tar itself does not compress files, it is commonly used in conjunction with compression tools like gzip, bzip2, and xz to create compressed archives.

 

20+ Practical tar command examples for Linux Administrators

20+ Practical tar command examples for Linux Administrators

Also Read: How to Install libxml2-2.X.X on Linux [Simple Steps]

Example 1: Create an archive

To create a new tar archive named archive.tar containing the files itsfosslinux.go and hello.c, use tar -cvf archive.tar itsfosslinux.go hello.c command as shown below. However this archive won't be compressed as no compression is applied.

cyberithub@ubuntu:~$ tar -cvf archive.tar itsfosslinux.go hello.c
itsfosslinux.go
hello.c

Here are the different options used in above command:-

  • -c: This option stands for "create". It tells tar to create a new archive.
  • -v: This option stands for "verbose". When this option is included, tar will list the files it's adding to the archive. This provides feedback to the user about what the command is doing.
  • -f: This option stands for "file". It is followed by the name of the archive file you're creating. In this case, the archive file is named archive.tar.

 

 

Example 2: Extract an archive

To extract all the files and directories contained in the archive.tar file into the current working directory, run tar -xvf archive.tar command as shown below. The verbose output will list each file and directory as it is extracted. This command does not require specifying individual file names within the archive; it will extract all contents by default.

cyberithub@ubuntu:~$ tar -xvf archive.tar
itsfosslinux.go
hello.c

Here are the different options used in above command:-

  • -x: This option stands for "extract". It tells tar to extract files from the specified archive.
  • -v: This option stands for "verbose". When this option is included, tar will list the files it's extracting. This provides feedback to the user about what the command is doing.
  • -f: This option stands for "file". It is used to specify the name of the archive file from which to extract the files.

 

 

Example 3: List contents of an archive

To view detailed information about the contents of archive.tar, including file and directory names, sizes, permissions, and modification dates, without actually extracting the files, run tar -tvf archive.tar command as shown below. This can be particularly useful for verifying the contents of an archive before extracting it.

cyberithub@ubuntu:~$ tar -tvf archive.tar
-rw-rw-r-- cyberithub/cyberithub 95 2023-12-17 15:30 itsfosslinux.go
-rw-rw-r-- cyberithub/cyberithub 96 2023-12-28 22:20 hello.c

Here are the different options used in above command:-

  • -t: This option stands for "list". It instructs tar to display a list of files and directories contained within the archive, rather than extracting them.
  • -v: This option stands for "verbose". When used with the -t option, it provides detailed information about each file in the archive, such as file size, owner, file permissions, and timestamp, in addition to the file names.
  • -f: This option stands for "file". It is used to specify the name of the archive file whose contents you want to view.

 

 

Example 4: Create a Gzip compressed archive

To create a new gzip-compressed tar archive named archive.tar.gz containing the files itsfosslinux.go and hello.c, run tar -czvf archive.tar.gz itsfosslinux.go hello.c command as shown below.

cyberithub@ubuntu:~$ tar -czvf archive.tar.gz itsfosslinux.go hello.c
itsfosslinux.go
hello.c

Here are the different options used in above command:-

  • -c: This option stands for "create". It tells tar to create a new archive.
  • -z: This option enables gzip compression. It tells tar to compress the archive file, resulting in a smaller file size. The compressed file will have a .gz extension.
  • -v: This option stands for "verbose". It makes tar provide detailed output, showing the files being added to the archive. This is useful for tracking the progress of the archive creation.
  • -f: This option stands for "file". It is followed by the name of the archive file you want to create. In this case, the archive file is named archive.tar.gz.

 

 

Example 5: Create a Bzip2 compressed archive

If you are looking to create a new bzip2-compressed tar archive named archive.tar.bz2 containing the files itsfosslinux.go and hello.c then run tar -cjvf archive.tar.bz2 itsfosslinux.go hello.c command as shown below. The verbose output will list these files as they are added to the archive. This type of compression is often used when a smaller archive size is desired, and the time taken for compression is less of a concern.

cyberithub@ubuntu:~$ tar -cjvf archive.tar.bz2 itsfosslinux.go hello.c
itsfosslinux.go
hello.c

Here are the different options used with above command:-

  • -c: This option stands for "create". It instructs tar to create a new archive.
  • -j: This option enables bzip2 compression. It tells tar to compress the archive file using the bzip2 algorithm, which generally provides better compression than gzip (as denoted by the -z option) but can be slower in compression and decompression. The resulting file will have a .bz2 extension.
  • -v: This option stands for "verbose". It makes tar provide detailed output, showing the files being added to the archive. This is useful for monitoring the progress of the archive creation.
  • -f: This option stands for "file". It is followed by the name of the archive file you want to create. In this case, the archive file is named archive.tar.bz2.

 

 

Example 6: Extract a Bzip2 Compressed archive

If you are looking to extract all the files and directories contained in the archive.tar.bz2 file then run tar -xjvf archive.tar.bz2 command as shown below. The -j option is critical for handling the bzip2 compression. The verbose output will list each file and directory as it is extracted, providing clear feedback on the process.

cyberithub@ubuntu:~$ tar -xjvf archive.tar.bz2
itsfosslinux.go
hello.c

 

 

Example 7: Add files to an existing archive

If you are looking to add a file named Student.java to the end of the existing archive.tar file then use tar -rvf archive.tar Student.java command as shown below. The verbose output will confirm the addition of this file. It's important to remember that this command can only be used with uncompressed tar archives, it won't work with archives compressed with tools like gzip or bzip2.

cyberithub@ubuntu:~$ tar -rvf archive.tar Student.java
Student.java

Here are the options used with above command:-

  • -r : This option stands for "append". It instructs tar to append files to the end of an existing archive. It's important to note that this only works on uncompressed tar archives.
  • -v : This option stands for "verbose". When included, tar will provide detailed output, showing the file being added to the archive. This is useful for confirming that the correct file is being appended.
  • -f : This option stands for "file". It is followed by the name of the archive to which the file will be appended.

 

 

Example 8: Extract specific files from an archive

If you are looking to extract a file called hello.c from archive.tar into the current directory then run tar -xvf archive.tar hello.c command as shown below. The verbose output will list hello.c as it is extracted, providing immediate feedback that the extraction is taking place.

cyberithub@ubuntu:~$ tar -xvf archive.tar hello.c
hello.c

 

 

Example 9: Create an archive with excluded directories

To create an archive named archive.tar containing the file window.js, while explicitly excluding any files or directories named go from being archived, use tar -cvf archive.tar --exclude='go' window.js command as shown below.

cyberithub@ubuntu:~$ tar -cvf archive.tar --exclude='go' window.js
window.js

Here is an important option used with above command:-

  • --exclude='go': This is an option to exclude files or directories. In this instance, it tells tar to exclude any files or directories named 'go' from the archive. This is useful for omitting specific files or directories that you don't want to include.

 

 

Example 10: Create an archive from files matching a pattern

To create an archive file archive.tar that contains all files ending with .json in the current directory, run tar -cvf archive.tar --wildcards *.json command as shown below. The verbose output will list each of these .json files as they are added to the archive. This command is particularly useful for selectively archiving a specific type of file from a directory.

cyberithub@ubuntu:~$ tar -cvf archive.tar --wildcards *.json
package.json
package-lock.json
sample.json

Here is an important option used with above command:-

  • --wildcards: This option tells tar to interpret glob patterns (like *.json) as wildcards. This is particularly useful when you are specifying patterns for file selection.

 

 

Example 11: Extract an archive to a specific directory

If you are looking to extract all files and directories from archive.tar into the /tmp directory then run tar -xvf archive.tar -C /tmp command as shown below.

cyberithub@ubuntu:~$ tar -xvf archive.tar -C /tmp
package.json
package-lock.json
sample.json

Below is one of the important option used in above command:-

  • -C /tmp: This option changes the directory to /tmp before extracting the files. -C stands for "change to directory", so the command will extract the contents of archive.tar into the /tmp directory instead of the current working directory.

 

 

Example 12: Create a compressed archive using XZ compression

If you are looking to create a compressed tar archive named archive.tar.xz containing all .json files from the current directory then run tar -cJvf archive.tar.xz *.json command as shown below.

cyberithub@ubuntu:~$ tar -cJvf archive.tar.xz *.json
package.json
package-lock.json
sample.json

Below is one of the important option used with above command:-

  • -J: This option enables xz compression. It instructs tar to compress the archive using the xz compression algorithm, which is known for its high compression ratio. The resulting archive file will have a .xz extension.

 

 

Example 13: Preserve permission while creating an archive

If you have to create a gzip-compressed tar archive named archive.tar.gz containing all .json files from the current directory with all the file permissions preserved then run tar -pczvf archive.tar.gz *.json command as shown below.

cyberithub@ubuntu:~$ tar -pczvf archive.tar.gz *.json
package.json
package-lock.json
sample.json

Here are some of the important options used with above command:-

  • -p: This option tells tar to preserve the permissions of the files when creating the archive. It ensures that the original permissions are maintained when the files are extracted from the archive.
  • -c: This option stands for "create", indicating that a new archive is to be created.
  • -z: This option enables gzip compression. It instructs tar to compress the archive using the gzip algorithm, resulting in a smaller file size. The compressed file will have a .gz extension.

 

 

Example 14: Create an incremental archive

If you are looking to create an incremental tar archive named archive.tar containing all .json files from the current directory that have changed since the last time the archive was created or updated then run tar -cvf archive.tar --listed-incremental=/tmp/archive.file *.json command as shown below. The snapshot file at /tmp/archive.file is used to track the state of the files between runs, making this command particularly useful for creating efficient backups that only include changed files.

cyberithub@ubuntu:~$ tar -cvf archive.tar --listed-incremental=/tmp/archive.file *.json
package-lock.json
package.json
sample.json

Below is an important option used with above command:-

  • --listed-incremental=/tmp/archive.file: This option specifies the snapshot file used for incremental archiving. The snapshot file (/tmp/archive.file) records information about the files included in the archive, such as their modification times and names. When the command is run subsequently, tar will only archive the files that have changed since the last time the archive was created, based on the information in this snapshot file.

 

 

Example 15: Extract an incremental archive

If you are looking to extract files from a tar archive file which is part of an incremental backup series then you have to run tar -xvf archive.tar --listed-incremental=/tmp/archive.file command as shown below. The snapshot file at /tmp/archive.file guides the extraction process by providing information on file changes, ensuring that the current state of the extracted files aligns with the latest incremental backup.

cyberithub@ubuntu:~$ tar -xvf archive.tar --listed-incremental=/tmp/archive.file
package-lock.json
package.json
sample.json

 

 

Example 16: Compare archive contents with file system

If you are looking to perform a detailed comparison between the files and directories contained within archive.tar and those in the current directory then tar -dvf archive.tar command as shown below. It reports differences such as file modifications, additions, or deletions. This can be particularly useful for verifying the integrity of an archive's contents or understanding what changes have occurred since the archive was created.

cyberithub@ubuntu:~$ tar -dvf archive.tar
package-lock.json
package.json
sample.json

Here is an important option used with above command:-

  • -d: This option stands for "diff" or "compare". It instructs tar to compare the contents of the specified archive with the files present in the current working directory.

 

 

Example 17: Create archive with file size limit

If you are looking to create a tar archive named archive.tar containing all .json files from the current directory with size limited to 100 megabytes then run tar -cvf archive.tar --tape-length=102400 *.json command as shown below. If the total size of the .json files exceeds this limit, tar might create additional volumes (or prompt the user to do so, depending on the environment and version of tar).

cyberithub@ubuntu:~$ tar -cvf archive.tar --tape-length=102400 *.json
package.json
package-lock.json
sample.json

Here is an important option used with above command:-

  • --tape-length=102400: This option sets the maximum size of the archive to 102400 kilobytes (which is equivalent to 100 megabytes). The --tape-length option is traditionally used when writing archives to tapes, to split the archive into multiple volumes when a certain size is reached. However, it can also be used with regular files to limit their size.

 

 

Example 18: Create an archive with different owner

If you are looking to create a gzip-compressed tar archive named archive.tar.gz containing all .json files from the current directory and would like to set the ownership of these files within the archive to the user itsfosslinux then run tar --owner=itsfosslinux -cvzf archive.tar.gz *.json command as shown below.

cyberithub@ubuntu:~$ tar --owner=itsfosslinux -cvzf archive.tar.gz *.json
package.json
package-lock.json
sample.json

Here is an important option used with above command:-

  • --owner=itsfosslinux: This option sets the owner of all files in the archive to itsfosslinux. This can be useful for ensuring consistent file ownership when the archive is extracted, regardless of the user performing the extraction. It's important to note that changing the file owner might require superuser privileges, and this option might not be effective when extracting files as a non-root user unless additional actions are taken.

 

 

Example 19: Extract files older than a specific date

If you are looking to extract files from the tar archive named archive.tar, but only those files that have been modified after the specified date, in this case, January 10, 2024, at 00:00:00 then run tar --after-date='2024-01-10 00:00:00' -xvf archive.tar command as shown below. If your version of tar does not support --after-date, you might need to use --newer or an equivalent option based on your tar version.

cyberithub@ubuntu:~$ tar --after-date='2024-01-10 00:00:00' -xvf archive.tar

Here is an important option used with above command:-

  • --after-date='2024-01-10 00:00:00': This option tells tar to only consider files that were modified after the specified date and time. It's a way to filter the files to be extracted based on their modification timestamp. However, it's important to note that the --after-date option might not be supported in all versions of tar, and the --newer option is often used as a more widely supported alternative.

 

 

Example 20: Check all other available options

If you are looking to check all the options available with tar utility then run tar --help command as shown below.

cyberithub@ubuntu:~$ tar --help
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
........................................................

 

 

Example 21: Check Man Page 

If you are looking to check the man page of tar utility then run man tar command as shown below.

cyberithub@ubuntu:~$ man tar
TAR(1) GNU TAR Manual TAR(1)

NAME
       tar - an archiving utility

SYNOPSIS
   Traditional usage
       tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG...]

   UNIX-style usage
       tar -A [OPTIONS] ARCHIVE ARCHIVE

       tar -c [-f ARCHIVE] [OPTIONS] [FILE...]

       tar -d [-f ARCHIVE] [OPTIONS] [FILE...]

       tar -t [-f ARCHIVE] [OPTIONS] [MEMBER...]

       tar -r [-f ARCHIVE] [OPTIONS] [FILE...]
..................................................

Leave a Comment