-->

How to use tar command in Linux

Author Piyush Gupta

GNU TAR (Tape Archive) combines multiple files together into a single tape or disk archive, and can restore individual files from the archive. Here are some Linux tar command with Useful practical examples.
Some useful command line switches are given below, which are used in this article.
  • -c => create a new archive file
  • -v => show the detailed output (or progress) of command
  • -x => extract an archive file
  • -f => specify file name of an archive
  • -z => Use archive through gzip
  • -j => Use archive through bzip2
  • -J => Use archive through xz
  • -t => viewing content of archive
  • -O => Display file content on stdout
  • -r => append file to existing archive
  • -C => Use of define destination directory
  • -W => Verify a archive file

Creating Archive File

Use the following examples to create new archive file in different formats like .tar (a simple archive file), .tar.gz (gzip archive), .tar.bz2 (bzip2 archive file), and .tar.xz (xz archive file).
1. Create .tar archive file – Compress all content of /var/www directory to a archive.tar file including all subdirectories.
tar -cvf archive.tar /var/www
2. Create .tar.gz archive file – Compress all content of /var/www directory to a archive.tar.gz file including all subdirectories. This will create higher compressed file that above.
tar -zcvf archive.tar.gz /var/www
3. Create .tar.bz2 archive file – Compress all content of /var/www directory to a archive.tar.bz2 file including all subdirectories. This takes more time to compress than others and provides the highest compressed file that above.
tar -jcvf archive.tar.gz /var/www
4. Create .tar.xz archive file – Compress all content of /var/www directory to a archive.tar.xz file including all subdirectories. This takes more time to compress than others and provides the highest compressed file that above.
tar -Jcvf archive.tar.xz /var/www

Extract Archive File

Use the following commands example to extract archive files. In this section each example has two commands, the First command will extract content in the current directory and the second command will extract file content in the specified directory with -C option.
5. Extract .tar archive file –
tar -xvf archive.tar
tar -xvf archive.tar -C /tmp/
6. Extract .tar.gz archive file –
tar -zxvf archive.tar.gz
tar -zxvf archive.tar.gz -C /tmp/
7. Extract .tar.bz2 archive file –
tar -jxvf archive.tar.bz2
tar -jxvf archive.tar.bz2 -C /tmp/
8. Extract .tar.xz archive file –
tar -Jxvf archive.tar.xz
tar -Jxvf archive.tar.xz -C /tmp/

List Archive File Content

You can list all the content inside a tar (Tape ARchive) file without extracting it. It helps the user to check available files in an archive and save users time.
9. List .tar archive file content –
tar -tvf archive.tar
10. List .tar.gz archive file content-
tar ztvf archive.tar.gz
11. List .tar.bz2 archive file content-
tar jtvf archive.tar.bz2
12. List .tar.xz archive file content-
tar Jtvf archive.tar.xz

Update Archive File

You can use -u option to simply update archive file. Using this option, it will only append files newer than the copy in the archive file.
13. Update .tar archive file –
tar -uvf archive.tar /var/www
14. Update .tar.gz archive file –
tar -zuvf archive.tar.gz /var/www
15. Update .tar.bz2 archive file –
tar -juvf archive.tar.bz2 /var/www
16. Update .tar.xz archive file –
tar -Juvf archive.tar.xz /var/www

Other Useful Archive File Commands

Below are some more useful options available for handling tape archive files. Below examples are shown in the simple .tar file. You can use them with .tar.gz (-z option), .tar.bz2 (-j option) and .tar.xzf (-J option).
17. Display file content – use -O followed by filename, this will display content of specified file.
tar -uvf archive.tar -O backup/index.html
18. Adding file to archive – use -r followed by filename to add more files to existing archive file.
tar -rvf archive.tar add_new_file.txt

No comments:

Post a Comment