2024-09-12
tar
tar
(short for “tape archiver”) is a powerful command-line utility used for creating archive files. An archive combines multiple files and directories into a single file, often compressed for smaller storage size and easier transfer. tar
itself doesn’t perform compression; it relies on external compression tools like gzip
, bzip2
, or xz
.
tar
OperationsThe basic syntax of tar
is:
tar [options] [archive-file] [file-list]
Let’s look at some common options:
-c
(create): Creates a new archive.-x
(extract): Extracts files from an archive.-t
(list): Lists the contents of an archive without extracting.-v
(verbose): Displays detailed information during the process.-f
(file): Specifies the archive file name.-z
(gzip): Compresses the archive using gzip.-j
(bzip2): Compresses the archive using bzip2.-J
(xz): Compresses the archive using xz.Let’s create an archive named my_archive.tar.gz
containing the files file1.txt
, file2.txt
, and the directory my_directory
:
mkdir my_directory
touch my_directory/file3.txt
touch file1.txt
touch file2.txt
tar -czvf my_archive.tar.gz file1.txt file2.txt my_directory
This command uses:
-c
: Creates a new archive.-z
: Compresses the archive using gzip.-v
: Displays verbose output (showing files being added).-f
: Specifies the archive filename my_archive.tar.gz
.To extract the contents of my_archive.tar.gz
:
tar -xzvf my_archive.tar.gz
This command uses:
-x
: Extracts the archive.-z
: Indicates that the archive is compressed with gzip.-v
: Provides verbose output.-f
: Specifies the archive filename.To list the files within my_archive.tar.gz
without extracting:
tar -tvf my_archive.tar.gz
This command uses:
-t
: Lists the archive contents.-v
: Shows verbose output.-f
: Specifies the archive filename.You can use different compression algorithms by changing the options. For example, to create an archive compressed with bzip2:
tar -cjvf my_archive.tar.bz2 file1.txt file2.txt my_directory
And to use xz compression:
tar -cJvf my_archive.tar.xz file1.txt file2.txt my_directory
While tar
doesn’t directly support adding files to an existing archive, you can achieve this by extracting the archive, adding the new files, and then creating a new archive. Alternatively, some specialized tools can append to certain archive types, but this is beyond the scope of basic tar
usage.
tar
supports wildcards (*
, ?
, [...]
) for selecting files and the -r
option for recursively adding directories. For instance, to archive all .txt
files in the current directory and its subdirectories:
tar -czvf my_archive.tar.gz **/*.txt
This utilizes the **
wildcard for recursive directory traversal. Remember that this requires Bash’s extended globbing to be enabled. You can enable this by running shopt -s globstar
before using this command.
To include or exclude specific files or directories during archiving, you can use the --exclude
option along with the -I
option which includes files matching a specific pattern. For example, to archive everything except file2.txt
:
tar -czvf my_archive.tar.gz --exclude=file2.txt *
These examples demonstrate the fundamental usage of tar
. Exploring its numerous options and combining them effectively unlocks its full potential for efficient file management in Linux. Remember to consult the man tar
page for a complete reference.