Linux tar Command - Archive and Compress
The tar command in Linux is used to create, extract, and manage archive files. It supports various options for compression and file manipulation. Below is a detailed guide with examples and descriptions of all options.
Basic Syntax
tar [options] [archive-file] [file or directory to archive]
Commonly Used Options
-c: Create a new archive.-x: Extract files from an archive.-v: Verbose mode, displays progress in the terminal.-f: Specify the name of the archive file.-z: Compress the archive using gzip.-j: Compress the archive using bzip2.-t: List the contents of an archive.--exclude: Exclude specific files or directories.
Examples
Create an Archive
tar -cvf archive.tar file1 file2 directory/
This command creates an archive named archive.tar containing file1, file2, and the contents of directory/.
Create a Compressed Archive
tar -czvf archive.tar.gz file1 file2 directory/
This command creates a gzip-compressed archive named archive.tar.gz.
Extract an Archive
tar -xvf archive.tar
This command extracts the contents of archive.tar into the current directory.
Extract a Compressed Archive
tar -xzvf archive.tar.gz
This command extracts the contents of archive.tar.gz.
List Contents of an Archive
tar -tvf archive.tar
This command lists the files and directories inside archive.tar.
Exclude Files or Directories
tar -cvf archive.tar --exclude='*.log' directory/
This command creates an archive of directory/ but excludes all files with the .log extension.
Extract to a Specific Directory
tar -xvf archive.tar -C /path/to/directory/
This command extracts the contents of archive.tar to the specified directory.
Append Files to an Existing Archive
tar -rvf archive.tar newfile
This command appends newfile to the existing archive archive.tar.
Update Files in an Archive
tar -uvf archive.tar updatedfile
This command updates the archive archive.tar with the latest version of updatedfile.
Remove Files from an Archive
tar --delete -f archive.tar file1
This command removes file1 from the archive archive.tar.
Using Different Compression Methods
To create a bzip2-compressed archive, use the -j option:
tar -cvjf archive.tar.bz2 file1 file2 directory/
To extract a bzip2-compressed archive, use:
tar -xvjf archive.tar.bz2
Main three type of compression
gzip: Fast compression, moderate size.bzip2: Slower compression, smaller size.xz: Slowest compression, smallest size.
To create an xz-compressed archive, use the -J option:
tar -cvJf archive.tar.xz file1 file2 directory/
To extract an xz-compressed archive, use:
tar -xvJf archive.tar.xz
Using Wildcards
You can use wildcards to specify files or directories:
tar -cvf archive.tar *.txt
This command creates an archive of all .txt files in the current directory.
some methord to read Compress file
To read a compressed file without extracting it, you can use the zcat, zless, or zmore commands:
zcat archive.tar.gz
This command displays the contents of archive.tar.gz without extracting it.
zless archive.tar.gz
This command allows you to view the contents of archive.tar.gz page by page.
zmore archive.tar.gz
This command allows you to view the contents of archive.tar.gz in a more interactive way.
Using tar with SSH
You can use the tar command over SSH to create or extract archives on remote servers:
ssh user@remote-server "tar -czvf archive.tar.gz /path/to/directory/"
This command creates a gzip-compressed archive of the specified directory on the remote server.
ssh user@remote-server "tar -xzvf archive.tar.gz -C /path/to/directory/"
This command extracts the contents of archive.tar.gz to the specified directory on the remote server.