The tar
command in Linux is used to create and manipulate tar archives, which are files that contain multiple files and directories. It is commonly used for creating backups or compressing multiple files and directories into a single archive.
Here’s an overview of how to use the tar
command:
- Creating a Tar Archive:
To create a tar archive, use the-c
(create) option followed by the archive file name and the files or directories you want to include in the archive. For example:
tar -cvf archive.tar file1.txt file2.txt directory/
This command creates a tar archive named archive.tar
containing file1.txt
, file2.txt
, and directory/
.
- Extracting Files from a Tar Archive:
To extract files from a tar archive, use the-x
(extract) option followed by the archive file name. For example:
tar -xvf archive.tar
This command extracts the files and directories from archive.tar
to the current directory.
- Viewing the Contents of a Tar Archive:
To view the contents of a tar archive without extracting them, use the-t
(list) option. For example:
tar -tvf archive.tar
This command lists the files and directories contained in archive.tar
.
- Compressing Tar Archives:
Tar archives can be compressed using various compression algorithms. To compress a tar archive, use the appropriate compression option. For example:
- Compress with gzip:
tar -cvzf archive.tar.gz file1.txt file2.txt directory/
- Compress with bzip2:
tar -cvjf archive.tar.bz2 file1.txt file2.txt directory/
- Compress with xz:
tar -cvJf archive.tar.xz file1.txt file2.txt directory/
- Extracting Compressed Tar Archives:
Compressed tar archives can be extracted using the same extraction command as before. The compression algorithm is automatically detected based on the archive file extension. For example:
- Extract gzip-compressed archive:
tar -xvzf archive.tar.gz
- Extract bzip2-compressed archive:
tar -xvjf archive.tar.bz2
- Extract xz-compressed archive:
tar -xvJf archive.tar.xz
- Examples:
- Creating a tar archive:
tar -cvf archive.tar file1.txt file2.txt directory/
This command creates a tar archive namedarchive.tar
containingfile1.txt
,file2.txt
, anddirectory/
. - Extracting files from a tar archive:
tar -xvf archive.tar
This command extracts the files and directories fromarchive.tar
to the current directory. - Viewing the contents of a tar archive:
tar -tvf archive.tar
This command lists the files and directories contained inarchive.tar
. - Creating a compressed tar archive:
tar -cvzf archive.tar.gz file1.txt file2.txt directory/
This command creates a gzip-compressed tar archive namedarchive.tar.gz
. - Extracting files from a compressed tar archive:
tar -xvzf archive.tar.gz
This command extracts the files and directories from the gzip-compressed tar archivearchive.tar.gz
.
The tar
command provides a flexible way to create and manipulate tar archives. It can be used for creating backups, archiving files and directories, and transferring multiple files as a single archive.
For more information about the tar
command and its