• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Learn tar command and know about its precaution

July 10, 2013 by Sharad Chhetri Leave a Comment

Learn tar command and know about its precaution

Tar(Tap Archive) command is used in Unix like operating system for archiving the files.
Archive is a file which has number of files and these files can be extracted to its original format with the help of extraction
programmes.
These archived files are called tarballs. If you have ever downloaded any source files of programmes,
they are generally you can get in tar format for eg. packag-name.tar.gz i.e called tarball .

Note: tar does not perform compression but these archives file can be compressed by using compression utilities for eg. gzip(z),
bzip2(bj2),compress(Z) etc

How to tar

These are the flags which are used in tar command

-c = create archive
-v = verbose
-f = use file archive
-x = extract

Use the below given command

tar give-tar-name.tar file1 file2 file3

or

tar give-tar-name.tar directory-name

or

tar give-tar-name.tar file1 file2 file3 directory-name

eg.

linux@tuxworld:/tmp$ tar -cvf sharad.tar file1 file2 test
file1
file2
test/
test/dddd
linux@tuxworld:/tmp$ tar -tvf sharad.tar 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

Using compression with tar

Many compression programs are available with tar, such as gzip, bzip2, xz, lzip, lzma, or compress,
Here we are taking only two compression utilities which are generally used.

These are the flags which represent the compresion utilities:

-z = gzip (tar.gz)
-j = bzip2 (tar.bj2)

Note: Use these flags alongwith -c (create) and -x (extract) repective of compression.

Below are eg. of creating tarball with compression

gzip:
	tar -cvzf test.tar.gz file1 file2
        tar -cvzf test.tar.gz directory-name
bzip2:
	tar -cvjf test.tar.bj2 file1 file2
	tar -cvjf test.tar.bj2 directory-name

Extracting tarball

Use -x option for extract. In case if there is any compression use the compression flag with -x
for eg. -xz (for gzip) , -xj (for bzip2)


gzip:
	tar -xvjf test.tar.gz 
bzip2:
	tar -xvjf test.tar.bj2
	

How to list files and directory from tarball without extracting

Use -t flag with tar command.

tar -tvf package-name.tar
tar -tvf package-name.tar.gz
tar -tvf package-name.tar.bj2

See the below example.


linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

linux@tuxworld:/tmp$ tar -tvf sharad.tar.bj2 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$ 

linux@tuxworld:/tmp$ tar -tvf sharad.tar.gz 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
linux@tuxworld:/tmp$

How to extract tarball to other location

For eg. when you use command tar -xvzf package-name.tar.gz it extract the files in current directory. If you want to extract
to some other path ,use flag -C (Capital C)

See the below eg.


	tar -xvf test.tar -C /opt

	tar -xvjf test.tar.gz  -C /opt

	tar -xvjf test.tar.bj2 -C /opt

Add file or directory into existing tarball

Use -r option for adding file or directory into existing tarball

linux@tuxworld:/tmp$ tar -rvf sharad.tar newfile 
newfile
linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
linux@tuxworld:/tmp$

linux@tuxworld:/tmp$ mkdir test-dir
linux@tuxworld:/tmp$ tar -rvf sharad.tar test-dir
test-dir/
linux@tuxworld:/tmp$ tar -tvf sharad.tar
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
drwxrwxr-x linux/linux       0 2013-07-10 11:50 test-dir/
linux@tuxworld:/tmp$ 


Note : You can not add new file or directory in compressed archived tarball. You will get the following error.

linux@tuxworld:/tmp$ tar -rvzf sharad.tar.gz newfile 
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.
linux@tuxworld:/tmp$ 

Extract particular file or directory from tar, tar.gz and tar.bj2

Use the below given format

tar -xvf archive-file-name.tar /path/of/fileORdir

Or

tar -xvzf archive-file-name.tar.gz /path/of/fileORdir

Or

tar -xvjf archive-file-name.tar.bj2 /path/of/fileORdir

eg.

linux@tuxworld:/tmp/workshop$ tar -tvf sharad.tar 
-rw-rw-r-- linux/linux       8 2013-07-10 07:02 file1
-rw-rw-r-- linux/linux       0 2013-07-10 07:02 file2
drwxrwxr-x linux/linux       0 2013-07-10 09:58 test/
-rw-rw-r-- linux/linux       0 2013-07-10 09:58 test/dddd
-rw-rw-r-- linux/linux       9 2013-07-10 11:42 newfile
drwxrwxr-x linux/linux       0 2013-07-10 11:50 test-dir/
linux@tuxworld:/tmp/workshop$ tar -xvf sharad.tar file2
file2
linux@tuxworld:/tmp/workshop$ ls -la
total 20
drwxrwxr-x  2 linux linux  4096 Jul 10 11:55 .
drwxrwxrwt 16 root  root   4096 Jul 10 11:54 ..
-rw-rw-r--  1 linux linux     0 Jul 10 07:02 file2
-rw-rw-r--  1 linux linux 10240 Jul 10 11:50 sharad.tar
linux@tuxworld:/tmp/workshop$ 

Using wildcard for multiple file or directory extraction from tarball

If you want to extract multiple files or directory with wildcard * (Asterisk), you have to use the switch called –wildcards.
The case is applicable to tar,tar.gz,tar.bj2
See below given eg.

linux@tuxworld:/tmp/workshop$ tar -xvf sharad.tar  --wildcards file*
file1
file2
linux@tuxworld:/tmp/workshop$ ls -la
total 24
drwxrwxr-x  2 linux linux  4096 Jul 10 12:01 .
drwxrwxrwt 16 root  root   4096 Jul 10 11:54 ..
-rw-rw-r--  1 linux linux     8 Jul 10 07:02 file1
-rw-rw-r--  1 linux linux     0 Jul 10 07:02 file2
-rw-rw-r--  1 linux linux 10240 Jul 10 11:50 sharad.tar
linux@tuxworld:/tmp/workshop$

Precautions to take ,while using Tar command:

Because after extracting the tarball it may be possible the tarball has same name of file as you have file names in
current working directory. It will overwrite the file and which may cause the loss of data or any damage in filesystem etc.

for eg. in /opt I have some important files with name data.txt,list.txt,file1,important-file. I got the tarball from another machine
and extracted out in /opt . But in this case tarball already has same name of some file like data.txt and important-file. Here it
will overwrite these two files then and I will loss my data.
This kind of behaviour is also called tarbomb.

Always follow these steps while working with tar command.

(1) Before extracting the file always list the files inside the tarball by using -t option.
eg. tar -tvf tarbal-name.tar , tar -tvzf tarbal-name.tar.gz ,tar -tvjf tarbal-name.tar.bj2

(2) Always create new directory and move the tarball to new directory and then extract there.

   mkdir workshop
    mv tarbal-name.tar.gz worksop
    cd workshop
    tar -xvzf tarbal-name.tar.gz
    

(3) Or use -C flag which will extract the files to its directed path.
for eg.

    tar -xvzf tarbal-name.tar.gz -C /tmp
    

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Install and set Java on Ubuntu 14.04 LTS with tar ball file
  2. Linux : clear command and its keyboard shortcut
  3. Learn Linux Date Command With Examples
  4. learn very useful alias command with examples
  5. Saving every command and its output in log in Unix
  6. Set group password,its use and check which group after newgrp command in linux
  7. How to create OpenSSH rpm package and its upgrade
  8. How To Find Absolute Full Path Of Command On Linux / Unix : which command
  9. type Command : Display Information About Command Type On Linux
  10. Replace keyword with its filename without extension : bash script

Filed Under: Linux, Linux Commands Tagged With: linux command, tar

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

safest method to remove softlink in linux

Replace keyword with its filename without extension : bash script

rsync all files,hidden files,symlinks,hardlinks to remotes Linux Server

How to convert rpm file into deb file

Terminal Recording with script and scriptreplay command

How to reset forgot root password in CentOS 6.x and Redhat 6.x

Change default editor of crontab in Ubuntu

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy