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.
Also Read: How to Install libxml2-2.X.X on Linux [Simple Steps]
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:-
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:-
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:-
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:-
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:-
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
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:-
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
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:-
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:-
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:-
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:-
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.
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:-
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:-
/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.
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
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:-
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:-
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.
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:-
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
option might not be supported in all versions of tar, and the --newer
option is often used as a more widely supported alternative.
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
........................................................
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...]
..................................................
Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…
Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…
The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…
I don’t like my prompt, i want to change it. it has my username and…
Introduction: A Fragile Trust The Ruby ecosystem relies heavily on RubyGems.org as the central platform…
Asset management is the process of managing and maintaining a company’s assets to maximize their…