20 Linux Tar command Examples

By | February 9, 2017

By Word expanding Tar means Tape Archive.Tar command is Linux Zip kind of Software,means it is used to zip the files and folders.Like on windows (.zip) files,we have .tar.gz  (or)  .tgz extension on linux.Means,if you find any folder/file with this extension,you called it as a zipped file.Possible extension of linux zipped files are tarball or tar, gzip and bzip.

20 Linux Tar Command Examples

 Example 1: how to compress folder or a file name with “gzip”

[ SLTMachine: ] # tar -czvf myzipfilename.tar.gz folder1

In the above syntax i am trying to compress the folder1 as “myzipfilename.tar.gz”.

Here “-czvf” are different options we provide it (optional)

  • ‘c’ means create archive
  • ‘z’ means  use “gzip” technique
  • ‘v’ means diplay the progress (we call verbose)
  • ‘f’  means (filename for zipfile)

 Example 2: how to compress folder or a file name without “gzip”

[ SLTMachine: ] # tar -cvf myzipfilename.tar folder1

In the above syntax, i am trying to compress the folder1 as “myzipfilename.tar.gz”.If you observe it doesnot have “gzip” extension.

gzip means “Compress it” here.

Example 3: create tar file with more compression than “gzip” then use “bz2”

[ SLTMachine: ] # tar -cvfj myzipfilename.tar.bz2 folder1

The above created file “myzipfilename.tar.bz2” has less size than “myzipfilename.tar.gz”.

if you need more compression then use “bz2”

Example 4:  Extract contents from tar file.

[ SLTMachine: ] # tar -xzvf myzipfilename.tar.gz

the above command extrats the contents from “myzipfilename.tar.gz” to the current directory

Example 5:  Extract contents from tar file to specific directory

[ SLTMachine: ] # tar -xzvf myzipfilename.tar.gz -c /mylocation

the above command extrats the contents from “” to the directory “/mylocation”

Example 6:  Extract contents from tar file only (.tar extension)

[ SLTMachine: ] # tar -xvf myzipfilename.tar

the above command extrats the contents from “myzipfilename.tar” to the current directory

Example 7:  Extract contents from tar file only (.tar extension) to the specific location

[ SLTMachine: ] # tar -xvf myzipfilename.tar -c /mylocation

the above command extrats the contents from “myzipfilename.tar” to the “/mylocation” directory

Example 8:  Extract contents from tar.bz2 Archive File

[ SLTMachine: ] # tar -xvf myzipfilename.tar.bz2

Example 9:  I want to Extract Single File out of  tar.gz Archive File

[ SLTMachine: ] # tar -xz -f myzipfilename.tar.gz "/tmp/mysinglefilename"

The above command will extract only single file “mysinglefilename” out of the zip pakage “myzipfilename.tar.gz”

Example 10:  I want to Extract Multiple Files out of  tar.gz Archive File

[ SLTMachine: ] # tar -xv -f myzipfilename.tar.gz --wildcards "*.log"

The above command will extract only all files which has extension like “.log” file’s from  “myzipfilename.tar.gz”

Example 11:  I want to Contents of  tar Archive File

[ SLTMachine: ] # tar -tvf myzipfilename.tar

Above command displays the contents of tar file.

Example 12:  I want to Contents of  tar.gz Archive File

[ SLTMachine: ] # tar -tvf myzipfilename.tar.gz

Above command displays the contents of tar.gz file.

Example 13:  I want to Contents of  tar.bz2 Archive File

[ SLTMachine: ] # tar -tvf myzipfilename.tar.bz2

Above command displays the contents of tar.bz2 file.

Example 14:  Can i add new file to existing tar file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar extrafile.log

Above command will add the extrafile.log to the existing zip file name – myzipfilename.tar

Example 15:  Can i add new folder to existing tar file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar newfoldername

Above command will add the newfoldername folder to the existing zip file name – myzipfilename.tar

Example 16:  Can i add new file to existing tar.gz file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar.gz extrafile.log

Above command will add the extrafile.log to the existing zip file name – myzipfilename.tar.gz

Example 17:  Can i add new folder to existing tar file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar.gz newfoldername

Above command will add the newfoldername folder to the existing zip file name – myzipfilename.tar.gz

Example 18:  Can i add new file to existing tar.bz2 file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar.bz2 extrafile.log

Above command will add the extrafile.log to the existing zip file name – myzipfilename.tar.gz

Example 19:  Can i add new folder to existing tar.bz2 file ?

[ SLTMachine: ] # tar -rvf myzipfilename.tar.bz2 newfoldername

Above command will add the newfoldername folder to the existing zip file name – myzipfilename.tar.bz2

Example 20:  What is the size of the tar (or) (tar.gz) or (tar.bz2 ) ?

[ SLTMachine: ] # tar -czf - myzipfilename.tar | wc -c

Above command will give the size of the myzipfilename.tar file in the Kilobytes.

Bonus Tip:

Can i verify the given tar file is Really a Tar file ?

[ SLTMachine: ] # tar tvfW myzipfilename.tar

The above command will displays the about .tar file,whether it is a tar file or not.

Thanks for reading the my experience on the “Tar” command.If you like it,please share it with others.If you want to add any new usage,please let me know in the comments section.i will add it here.Let everyone benefit out of it.

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply