How to Compress and Extract Files Using the tar Command on Linux

0
834

Tar command which is brief word of “tape archive”, is used by Linux/Unix system administrators. The purpose of the tar command is to create, compress, decompress and view the contents of compressed files. This makes it easier to save and move files from one computer to another. Let’s learn how to use the tar command.

Compresses the directory using the tar command
Assuming that the /home/filename directory exists, the tar command is used to compress this directory

#tar -cvf filename.tar/home/filename

In this case, the option has the following meaning:

 -c: create tar file
-v: show process execution
-f: tar file name created

If you want to compress the file using gzip, we can use option z as follows:

#tar -cvzf filename.tar/home/filename

Decompression
Assuming that the compressed file is filename.tar, to extract this file in the current directory we do as follows:

# tar -xvf filename.tar

In the case of extracting the tar file to the specified directory:

# tar -xvf filename.tar -C /home/username/test/
Or use “tar -zxvf” to extract the * .tar.gz file.
Or use “tar -jxvf” to extract the * .tar.bz2 file.

Extract some files with wildcard
Assuming that the compressed file is filename.tar, only extract the. C file in this file we do as follows:

# tar -xvf filename.tar –wildcard ‘* .c’
Same with * .tar.gz and * .tar.bz2:
#tar -zxvf filename.tar.gz –wildcard ‘* .c’
#tar -jxvf filename.tar.b2z –wildcard ‘* .c’

Extract the specified files
Assuming that the compressed file is filename.tar, only extract file1 and file2 but in this file we do as follows:

# tar -xvf filename.tar “file1” “file2”

Same with * .tar.gz and * .tar.bz2:

# tar -zxvf filename.tar.gz “file1” “file2”
# tar -jxvf filename.tar.b2z “file1” “file2”

Displays the contents of the tar file
To display the files, folders in file “filename.tar”, we execute the following command:

#tar -tvf filename.tar

This command can also be used with * .tar.gz and * .tar.bz2:

#tar -tvf filename.tar.gz
#tar -tvf filename.tar.b2z

Add files or directories to existing tar files
Please remember that tar command does not have the option to do this task.

Good luck!

LEAVE A REPLY

Please enter your comment!
Please enter your name here