Tutorial corner

Informatica,ETL,oracle,sql/plsql

Top 10 tar command example in unix

Spread the love

In Unix, tar command is useful as it can combine several files into a single uncompressed file and save space in unix. Tar command, also called tape archiving, can combine several files together and can create an archive folder  and it can be zipped as well. Later on we can extract file from  archived file as well. In below unix tutorial , we will go through some of important tar command example in unix .

tar command example in unix

tar command in unix

Check here for vi editor commands

Tar command example in unix

1. Tar command to archive files multiple from a folder

Below tar example command will bundle all files from /root/unix/tutorial/ and  create a tar archive file unix_tutorial.tar  in current working directory.

$ tar -cvf unix_tutorial.tar /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Option used in Tar command

  • c – Creates a new .tar archive file.
  • v – Verbosely show the .tar file progress.( will list down all the files  being archived)
  • f – File name type of the archive file. ( ex:  unix_tutorial.tar) in our example.

2. Tar command to Untar archived file in unix

Below tar example command to extract all files from unix_tutorial.tar  to /root/unix/tutorial/ .

$ tar -xvf unix_tutorial.tar -C /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Option used in Tar command

  • x – To extract files.
  • C – To extract file in given folder ( in our example : /root/unix/tutorial)

3. Tar command to List content of tar file.

Below tar example command to list all files from unix_tutorial.tar

$ tar -tvf unix_tutorial.tar
drwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/index.html
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/send_mail.ksh
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/data.txt
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/log/send_mail.log

Option used in Tar command

  • t – To list all  files.

4. Tar command to extract single file from tar file.

Below tar example command to extract data.txt , send_mail.ksh list all files from unix_tutorial.tar

$ tar -xvf unix_tutorial.tar data.txt
or
$ tar –extract –file=unix_tutorial.tar data.txt

5. Tar command to extract multiple file from tar file.

Below tar example command to extract send_mail.log , list all files from unix_tutorial.tar

$ tar -xvf unix_tutorial.tar send_mail.log send_mail.ksh
or
$ tar –extract –file=unix_tutorial.tar send_mail.log send_mail.ksh

Check here for find command example in unix

6. Tar command to add Files or Directories to tar Archive File

Below tar example to add user.csv to existing unix_tutorial.tar  tar file by using “r” option

$ tar -rvf unix_tutorial.tar user.csv
drwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/index.html
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/send_mail.ksh
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/data.txt
-rwxr-xr-x tecktricks 0 2014-01-15 11:12:45 /root/unix/tutorial/log/send_mail.log
-rwxr-xr-x tecktricks 0 2014-02-20 03:11:12 /root/unix/tutorial/log/user.csv

Tar command example in unix with Zipped/unzip option

7. Tar command to Create .gz zipped tar Archive File

To create zipped archived file , just add “z” option in tar command and also give archive file name as “unix_tutorial.tar.gz” or “unix_tutorial.tgz”

$ tar -cvzf unix_tutorial.tar.gz /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Option used in Tar command:

  • z – to gzip file

8. Tar command to uncompress tar.gz Archive File

To  extract files from zipped archived file , just add “z” option in untar command.

$ tar -xvzf unix_tutorial.tar.gz -C /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Option used in Tar command:

  • x- extract file
  • z- zipped file

9. Tar command to create tar.bz2 Archive File

To create more compressed  archived file , just add “j” option in tar command and also give archive file name as “unix_tutorial.tar.bz2” or “unix_tutorial.tb2”

$ tar -cvfj unix_tutorial.tar.gz /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Check here for find command with exec

10. Tar command to Uncompress tar.bz2 Archive File

To  extract files from zipped archived file , just add “j” option in untar command.

$ tar -xvjf unix_tutorial.tar.bz2 -C /root/unix/tutorial
/root/unix/tutorial/
/root/unix/tutorial/index.html
/root/unix/tutorial/send_mail.ksh
/root/unix/tutorial/data.txt
/root/unix/tutorial/log/send_mail.log

Option used in Tar command:

  • x- extract file
  • j- bz2 zipped file

So in above unix tutorial , we have gone through the tar command example in unix . For more info you can run man tar command in uni

The Author

Alisha Lamba

Hello Friends , I am Alisha Lamba .I love to write article on latest technologies like Informatica , ETL , data warehouse , SQL-PL SQL
Copyright 2015 - Tutorial Corner Frontier Theme