Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
In this tutorial, we will cover the tar
command in Linux. The tar
command is used to create and manipulate archive files, which are collections of files and directories stored in a single file. tar
stands for Tape ARchive, as it was initially designed for tape-based storage devices, but it is now used for various types of storage media.
Basic Usage of the tar Command
The basic syntax for the tar
command is as follows:
tar [OPTIONS] [ARCHIVE] [FILES/DIRECTORIES]
Creating a tar Archive
To create a new tar archive, use the -c
option, followed by the -f
option to specify the archive file name. For example, to create a tar archive named archive.tar
containing the files file1.txt
, file2.txt
, and the directory
folder, run:
tar -cf archive.tar file1.txt file2.txt directory
Listing the Contents of a tar Archive
To list the contents of a tar archive, use the -t
option, followed by the -f
option to specify the archive file name. For example, to list the contents of archive.tar
, run:
tar -tf archive.tar
Extracting a tar Archive
To extract the contents of a tar archive, use the -x
option, followed by the -f
option to specify the archive file name. For example, to extract the contents of archive.tar
to the current directory, run:
tar -xf archive.tar
To extract the contents to a specific directory, use the -C
option followed by the target directory. For example, to extract the contents of archive.tar
to the output
directory, run:
tar -xf archive.tar -C output
Adding Files to an Existing tar Archive
To add files to an existing tar archive, use the -r
option, followed by the -f
option to specify the archive file name. For example, to add the file file3.txt
to archive.tar
, run:
tar -rf archive.tar file3.txt
Creating and Extracting Compressed tar Archives
tar
can also work with compressed archives, such as those using gzip or bzip2 compression. To create a compressed archive, add the appropriate compression option:
-z
for gzip compression-j
for bzip2 compressionFor example, to create a gzip-compressed tar archive named archive.tar.gz
, run:
tar -czf archive.tar.gz file1.txt file2.txt directory
To extract a compressed archive, use the same compression option as during creation. For example, to extract the gzip-compressed archive archive.tar.gz
, run:
tar -xzf archive.tar.gz
Summary
The tar
command in Linux is a powerful tool for creating, manipulating, and extracting archive files. By using various options, such as -c
, -x
, -t
, -r
, -f
, -z
, -j
, and more, you can create, list, extract, and update archives, as well as work with compressed archives using gzip or bzip2 compression.
How to create a tar archive in Linux:
tar
command in Linux is used to create tar archives. It bundles files and directories into a single archive file.# Example: Creating a tar archive tar -cvf archive.tar files/
Extracting files from tar archives in Linux:
-x
option.# Example: Extracting files from a tar archive tar -xvf archive.tar
Compressing tar archives with gzip and bzip2:
tar
can be combined with compression tools like gzip
and bzip2
to create compressed tar archives.# Example: Creating a gzip-compressed tar archive tar -cvzf archive.tar.gz files/ # Example: Creating a bzip2-compressed tar archive tar -cvjf archive.tar.bz2 files/
Creating tarballs for backup in Linux:
tar
with compression options.# Example: Creating a tarball for backup tar -cvzf backup.tar.gz /path/to/backup/files/
Adding and extracting specific files with tar:
tar
allows adding specific files to an existing archive and extracting only selected files.# Example: Adding files to an existing tar archive tar -rvf archive.tar additional_file # Example: Extracting specific files from a tar archive tar -xvf archive.tar specific_file
Using tar for directory compression in Linux:
tar
can compress entire directories into a single archive for easy storage or transfer.# Example: Compressing a directory with tar tar -cvzf directory_archive.tar.gz /path/to/directory/
Creating and extracting compressed tar archives:
# Example: Creating and extracting a compressed tar archive tar -cvzf archive.tar.gz files/ && tar -xvzf archive.tar.gz -C /extract/to/directory/
Troubleshooting tar command issues in Linux:
tar
issues may involve checking file paths, permissions, or addressing errors during archive creation or extraction.# Example: Troubleshooting with tar tar -cvzf /nonexistent/path/archive.tar.gz files/