Useful Tar Command

Tar is known as tape archives which are most commonly used to take tape drive backup command used in Linux. Learn useful commands of tar in Linux. We can extract and untar all the tar files using the command with a folder in Linux. you can also check tar command Linux.

Learn Useful Tar Command With Example In Linux

Check Out: How To Secure Apache Web With Server Hardening In Linux

Most Common Tar Command options are listed below.

tar <options> <file>

-A, --concatenate: append tar file to achieve
-c, --create: create a new archive
-d, --diff, --compare, --delete: it finds the difference between archive and
 file system
-r, --append: append files to the end of an archive
-t, --list: list of contents of an archive
-u, --update: only append files which are newer than copy in the archive
-x, --extract, --get: extract the files
-w, --verify: attempt to verify the archive after writing it.
-f, --file: user archive file

Example 1: Create a tar file

I will create two files and will create a tar archive file.

touch abc abcd
mkdir tarfile
mv abc abcd tarfile
tar -cvf tarfile.tar tarfile

tar command linux

Example 2: List the contents of the tar file using the below commands.

tar -tvf tarfile.tar

extract tar linux

Check Out: How To Enable Instant Articles To Monetize Your Contents

Example 3: Compress files with gzip. You can either use tar.gz or tgz.

tar -zcvf tarfile.tar.gz tarfile
tar -zcvf tarfile.tgz tarfile

command untar linux

Example 4: Extracting file or directory from tar file and it can be used to extract all tar files.

tar -xvf tarfile.tar.gz

we can also extract the file at a particular location using the below command.

tar -xvf tarfile.tar.gz -C /opt/

tar folder in linux

Example 5: Create a tar.bz2 file and we will use the ‘-j’ option.

Check Out: How To Create a Clone of Existing Virtual Machine In VMWare

If you’re not able to compress files using it, you need to install bzip2 packages.

yum install bzip2

tar -cvfj tarfile.tar.bz2 tarfile
tar -cvfj tarfile.tar.tbz tarfile

Example 6: untar multiple extension .tar, tar.gz, .tar.tbz files in Linux.

tar -xvf tarfile.tar tarfile
tar -xzvf tarfile.tar.gz tarfile
tar -jxvf tarfile.tar.tbz tarfile

Example 7: Check the size of compressed files and displays the size in KB.

tar czf - tarfile.tar.gz | wc -c
tar czf - tarfile.tar.tbz | wc -c

Check Out: How To Find Facebook Page ID On Your Page From Profile

Example 8: Update the Existing tar file using the below command. we can use the ‘-r’ (or –append) option.

tar -rvf tarfile.tar filename 

Example 9: Find files using grep in the tar file.

tar -tvf tarfile.tar.gz | grep {filename.extension}
tar -tvf tarfile.tar.gz | grep abcd

Check Out: How To Install and Configure Node Exporter Prometheus

Example 10: Search a particular image format extension.

tar -tvf tarfile.tar.gz --wildcards "*.png"

Once you know the file name, you can also search it using the above command.

Example 11: Extract a file from a tar

tar --extract --file=tarfile.tar abcd.txt
tar -xvf tarfile.tar abcd.txt

Example 12: Extract a file from tar.gz

tar --extract --file=tarfile.tar.gz abcd.txt
tar -zxvf --file=tarfile.tar.gz abcde

Check Out: How To Create Route Tables In VPC Network On Amazon cloud

Example 13: Untar Multiple files from tar, tar.gz and tar.bz2 file.

tar -xvf tarfile.tar abcd “file 1” “file 2”
tar -zxvf tarfile.tar.gz abcd “file 1” “file 2”
tar -jxvf tarfile.tar.bz2 abcd “file 1” “file 2”

Example 14: Extract files with a particular extension. 

tar -xvf tarfile.tar --wildcards "*.png"
tar -zxvf tarfile.tar.gz --wildcards "*.png"
tar -jxvf tarfile.tar.bz2 --wildcards "*.png"

You’re done tar command linux extract command untar folder 


Please enter your comment!
Please enter your name here