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.
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.
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.
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.
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 –
6. Extract .tar.gz archive file –
7. Extract .tar.bz2 archive file –
8. Extract .tar.xz archive file –
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 –
10. List .tar.gz archive file content-
11. List .tar.bz2 archive file content-
12. List .tar.xz archive file content-
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 –
14. Update .tar.gz archive file –
15. Update .tar.bz2 archive file –
16. Update .tar.xz archive file –
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.
18. Adding file to archive – use -r followed by filename to add more files to existing archive file.
No comments:
Post a Comment