tar Command in Linux With Examples
Introduction
The GNUtar
(short for Tape ARchiver) command is the most widely used archiving utility in Linux systems. Available directly in the terminal, the tar
command helps create, extract, and list archive contents.
The utility is simple and has many helpful options for compressing files, managing backups, or extracting a raw installation.
This tutorial shows how to use the tar command through examples and the available options.
Prerequisites
- A system running Linux.
- Access to the command line/terminal.
- A file or files for testing the command.
Tar Command Syntax
The tar
command general syntax is:
tar <operation mode> <option(s)> <archive> <file(s) or location(s)>
- Operation mode indicates which operation executes on the files (creation, extraction, etc.). The command allows and requires only one operation.
- Options modify the operation mode and are not necessary. There is no limit on the number of options.
- The archive is the file name and extension.
- The file name(s) is a space-separated list for extraction or compression or wildcard matched name.
There are three possible syntax styles to use the operations and options:
1. Traditional style, clustered together without any dashes.
For example:
tar -cfv <archive> <file(s) or location(s)>
2. UNIX short option style, using a single dash and clustered options:
tar -cfv <archive> <file(s) or location(s)>
Alternatively, a dash before each option:
tar -c -f -v <archive> <file(s) or location(s)>
3. GNU long-option style with a double-dash and a descriptive option name:
tar --create --file <archive> --verbose <file(s) or location(s)>
All three styles can be used in a single tar
command.
Tar Command Options
The following table outlines the commonly used tar
operations and options.
Command | Role | Description |
---|---|---|
--create -c |
Operation | Creates a new archive. |
--list -t |
Operation | Lists an archive's contents. |
--extract -x |
Operation | Extract one or more items from an archive. |
--append -r |
Operation | Appends files to an existing archive. |
--concatenate -A |
Operation | Appends archives to an existing archive. |
--compare --diff -d |
Operation | Compares archive members with files on the system. |
--delete |
Operation | Deletes a member from the archive. |
--update -u |
Operation | Updates archive with new files only if they are not in the archive and are newer than existing files. |
--file=<archive> -f <archive> |
Option | Specifies the file. |
-C |
Option | Changes the directory. |
--verbose -v |
Option | Shows the file tar works on while running. |
--wildcard |
Option | Renders wildcard search options. |
--bzip2 -j |
Option | Read or write compressed archives through bzip2 format. |
--gzip -z |
Option | Read or write compressed archives through gzip format. |
--xz -J |
Option | Read or write compressed archives through xz format. |
Follow the examples in the next section to learn how to work with tar.
Tar Command Examples
The examples below have the following requirements:
1. Create a directory named tar_examples and navigate to the directory:
mkdir tar_examples && cd tar_examples
2. Make another directory called files in tar_examples and enter that directory:
mkdir files && cd files
3. Create files to populate the files directory:
touch file{0..100}.txt
To return to the parent directory, use:
cd ..
All the examples below work from the tar_examples directory.
1. Create an Archive
The syntax for creating an archive depends on the archive type. To make an archive, use tar
with the -c
or --create
operation.
Create a tar Archive
To make a tar archive (also called a tarball), use:
tar -cf <archive name>.tar <file(s) or location(s)>
For example, archive the files directory:
tar -cf files.tar files
The output lists each file added to the archive. Display the directory contents to see the created file.tar archive:
tar cf files.tar files
The output lists each file added to the archive. Display the directory contents to see the created file.tar archive:
ls -l
Create a tar.gz Compressed Archive
Add the -z
option to create a compressed GNUzip (gzip) file:
tar -czf <archive name>.tar.gz <file(s) or location(s)>
For example:
tar -czf files.tar.gz files
The file size is smaller than a regular tarball file and the original directory.
Create tar.bz2 Compressed Archive
The bzip2 is a file compression program and an alternative to gzip.
To create a tar.bz2 file, add the -j
tag:
tar -cjf <archive name>.tar.bz2 <file(s) or location(s)>
For example:
tar -cjf files.tar.bz2 files
The bzip2 has a greater compression rate and takes longer than gzip.
Create tar.xz Archive
Use the -J
tag to compress archives in the tar.xz format:
tar -cJf <archive name>.tar.xz <file(s) or location(s)>
For example:
tar -cJf files.tar.xz files
The xz compression takes the longest when compared to both gzip and bz2. However, with larger files, xz has the highest compression rates.
2. Remove Files After Creation
To remove the files from disk after archiving, use the --remove-files
option at the end:
tar -cf <archive> <file(s) or location(s)> --remove-files
For example, create a tar archive with the files directory and remove it from the disk in one command:
tar -cf files.tar files --remove-files
Check the directory contents to confirm the operation works correctly.
3. Extract from Archive
Extracting from an archive or compressed archive uses the -x
or --extract
operation with tar
. The additional options depend on the file type and where tar should extract the components.
Extract From tar Archive
The general syntax for extracting from a tar archive is:
tar -xf <archive name>.tar
By default, tar extracts all the components to the current directory. To indicate where to extract the components, add the -C
option and specify the path:
tar -xfC <archive name>.tar <path>
For example, to create a directory named extracted_tar and extract the files from files.tar, run:
mkdir extracted_tar && tar -xfC files.tar extracted_tar
The command doesn't output a confirmation message. Check the directory contents to confirm the components extracted successfully.
Extract From tar.gz Archive
Use the -z
option to extract a tar.gz file:
tar -xzf <archive name>.tar.gz
The command extracts the contents in the current directory. Add the -C
option to specify the location:
tar -xzfC <archive name>.tar.gz -C <location>
For example, create a new directory named extracted_gz and extract the files.tar.gz contents:
mkdir extracted_gz && tar -xzfC files.tar.gz -C extracted_gz
The tar.gz compressed archives take the least time to extract when compared to other compression formats.
Extract From tar.bz2 Archive
To extract files from a tar.bz2 compressed archive into the current directory, use:
tar -xjf <archive name>.tar.bz2
Extract tar.bz2 archives into a specific directory with:
tar -xjfC <archive name>tar.bz2 <location>
For example, create a directory and extract the contents from files.tar.bz2:
mkdir extracted_bz2 && tar -xjfC files.tar.bz2 extracted_bz2
Extract From tar.xz Archive
Add the -J
option to extract from tar.xz compressed archives. The syntax to extract in the current directory is:
tar -xJf <archive name>.tar.xz
To extract the contents to a specific directory, use the -C
option and add the path:
tar -xJfC <archive name>.tar.xz -C <location>
As an example, create a directory and extract the files.tar.xz contents:
mkdir extracted_xz && tar -xJfC files.tar.xz extracted_xz
The xz compression format is the middle ground between gz and bz2 when it comes to extraction time.
4. Overwrite Control
Tar overwrite controls handle situations where file names in the archive overlap with files in the working directory.
The three possible overwrite actions are:
1. Overwrite files in the working directory:
tar -xf <archive> <Optional file(s) or location(s)> --overwrite
2. Don't overwrite files in the working directory:
tar -xf <archive> <Optional file(s) or location(s)> --keep-old-files
If the files already exist, tar doesn't perform the extraction.
3. Extract files only if they are newer than the existing files:
tar -xf <archive> <Optional file(s) or location(s)> --keep-newer-files
If the files in the working directory are newer or the same age, tar does not extract the files.
5. List Archive Contents
Use the following command to list an archive's contents:
tar -tf <archive>
The option works for any file extension containing tar.
For example, list the files and directories in the files.tar archive:
tar -tf files.tar.gz
6. Find a File in an Archive
There are two ways to locate specific content using tar:
1. The -t
option to list files in an archive is handy for locating specific files. Add the file name (or names) after the command:
tar -tf <archive> <file(s)>
For example, to locate file50.txt in the files.tar.gz archive, run:
tar -tf files.tar.gz file/file50.txt
The option requires knowing the possible path to the file.
2. Use the tar
together with the grep command to filter the output:
tar tf <archive> | grep <file(s)>
For example:
tar -tf files.tar.gz | grep file50.txt
The option doesn't require knowing the possible path to the file.
7. Find Multiple Files in an Archive
Use the --wildcards
option to match multiple file instances. For example:
tar -tf files.tar.gz --wildcards file/files5*.txt
Apply wildcard matching when files have a similar name, or for filtering a certain file type.
8. Exclude Files when Creating Archive
To exclude certain files from the archive during creation, add the following option:
tar -cf <archive> --exclude='<pattern>' <files(s) or location(s)>
For example, create an archive from the files directory and exclude all .txt files:
tar -cf files.tar --exclude='*.txt' files
List the archive contents:
tar -tf files.tar
The output shows no .txt files, only the files directory in the archive.
9. Extract Single File from Archive
Avoid extracting the whole archive if you need one or several files.
To get a single file from an archive:
1. List the contents and check if the file exists:
tar -tf files.tar | grep file100.txt
The output prints the path to the file needed for the next step.
2. Extract the specific file with:
tar -xf files.tar files/file100.txt
The command creates the directory files with only the indicated file. Follow the same steps to extract a single file from compressed archives by adding the appropriate tag. For example, use the -z
option to pull from a tar.gz file.
10. Verbose Option
The verbose option displays additional information after running a tar command. Add -v
or --verbose
to any operation to see the result.
For example, create a tar.gz file and add -v
:
tar -czfv files.tar.gz files
The output shows each file as it is added to the archive.
Some tar commands show additional information when you add the -v
tag twice. For example, try to add files to an archive with -vv
:
tar -czfvv files.tar.gz files
The output prints a long listing format and looks similar to running the ls -l command.
11. Delete from Archive
To delete from the archive, locate the file you want to remove, for example:
tar -tf files.tar | grep file100.txt
Then, remove the file using the --delete
tag:
tar --delete -f files.tar files/file100.txt
The delete option does not work on compressed file formats.
12. Append Files to Archive
Append files to an existing archive using the -r
tag. The syntax is:
tar -rf <archive name>.tar <file(s) or location(s)>
For example, append the compressed files.tar.gz file to the files.tar archive:
tar -rf files.tar files.tar.gz
Already compressed archives cannot be updated, so the syntax only works for tarball files.
13. Combine Archives
Use the --concatenate
or -A
option to combine multiple archives. The basic syntax is:
tar -Af <archive to extend> <archive to extend with>
As an example, copy the existing files.tar file using the cp command:
cp files.tar files_copy.tar
Next, concatenate the two archives:
tar -Af files.tar files_copy.tar
To confirm the concatenation worked, check the file size.
14. Difference Between Archive and Files
To check the difference between an archive and files on disk, use the -d
tag:
tar -df <archive name>
The command searches for the same contents and compares them to what is in the archive. The option only checks for existing files and ignores any newly added files.
The steps below show how to use the -d
, --diff
, or --compare
tag with tar
:
1. Create a tar archive:
tar -cf files.tar files
2. Compare the archive with the existing directory:
tar -df files.tar
The output does not display anything, meaning there is no difference between the existing files.
3. Add text to an existing file in the files directory:
echo 'Hello' >> files/file0.txt
4. Compare the archive to the existing directory again:
tar -df files.tar
This time, the output shows differences in the modification time and the size for a specific file. Comparing provides insight into any changes made on the system after creating the archive.
15. Update Files in Archive
Update the existing files in the archive with a newer version from disk with the -u
option:
tar -uf <archive> <file(s) or location(s)>
For example, update the files.tar archive with a changed text file:
tar -uf files.tar files
Check the tar contents for the changed file:
tar -tfv files.tar | grep files0.txt
The command updates the archive with changed files without any overwrites.
16. Modified Time
Tar offers various options to modify the file's timestamp. Set a custom date when creating an archive by adding the --mtime
option and providing a date:
tar -cf <archive> <file(s) or location(s)> --mtime=YYYY-MM-DD
For example, create an archive and set the date to January 1st, 1999:
tar -cf files.tar files --mtime=1999-01-01
Alternatively, extract the files with the current date and time:
tar -xf files.tar -m
A useful feature when working with time is filtering files modified after a specific date. For example, to extract files created after a date, use the --newer-mtime
option and add the date:
tar -xf <archive> --newer-mtime=YYYY-MM-DD
17. Permissions
There are two possible ways to control file permissions with tar when extracting an archive:
1. Preserve original permissions:
tar -xf <archive name> --preserve-permissions
The permissions are as stated in the file before archive creation.
2. Modify the permissions to the default umask value:
tar -xf <archive name> --no-same-permissions
The files take on the default Linux permissions.
18. File Ownership
Tar allows file ownership configuration. For example, to set the file owner when creating an archive, add the --owner
and --group
options and provide values for each:
tar -cf <archive> <file(s) or location(s)> --owner=<value> --group=<value>
The owner value represents the UID (User ID) while the group value is the GID (Group ID). To find these values for a user, run:
id <username>
For example, create an archive and set the ownership to root:
tar -cf files.tar files --owner=0 --group=0
ar allows preserving the ownership when extracting from an archive. To do so, add the --same-owner
option at the end:
tar -xf <archive> --same-owner
19. Write to External Program
The --to-command
option instructs tar to send each extracted file to the standard output for an external program. The basic syntax is:
tar -xf <archive> --to-command='<command>'
For example, extract the files.tar contents and pipe the file names as directories:
tar -xf files.tar --to-command='mkdir $TAR_FILENAME'
The command creates directories named after each extracted file. For more information and the available Linux environment variables, visit the manual page.
20. Create Daily Backups
To automate daily backups, create a bash script and add the following lines:
tar -czf backup-$(date +%Y%m%d).tar.gz files
find backup* -mtime +1 -delete
The tar command creates a compressed archive, while the find command seeks backup files older than one day. Change the +1
parameter to +7
for weekly or +31
for monthly backups.
No Comments