Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»Linux tar Command with Useful Practical Examples

    Linux tar Command with Useful Practical Examples

    RahulBy RahulApril 16, 20134 Mins ReadUpdated:February 26, 2021

    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

    How to Create Archive using Tar

    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
    

    How to Extract Archive using Tar

    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/
    

    How to List Archive File Content with Tar

    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
    

    How to Update Tar 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
    

    Conclusion

    In this tutorial you have learned about Linux tar command with useful practical examples.

    For more details, visit: http://www.gnu.org/software/tar/manual/tar.html

    archive extract archive gzip tar tar.bz2 tar.gz tar.xz
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to use zip command in Linux
    Next Article File Encryption using GPG Key Pair in Linux

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    How To Compare Two Files in Linux

    Updated:August 23, 20215 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.