tar command examples


– tar is a very useful command in Linux to bundle files into one single file.

– It is very extensively used into various day-to-day operations by the Linux system administrators.

– Having knowledge of tar command is very indeed recommended.

Examples:

1. To create an archive


# tar -cvf test.tar test1 test2 test3 test4
# tar -cvf test.tar test*

# tar -cvf etc.bk /etc 

 

2. To display the content of the tar ball



# tar -tvf test.tar 

 

3. To extract the tar ball



# tar -xvf test.tar 

 

4. To add / append a file into the tar ball



# tar –append -f test.tar test5 

 

5. To delete / remove a files from the tar ball



# tar –delete -f test.tar test6 

 

6. Extract single file from tar ball


# tar -xvf test.tar test4

# tar -xvf test.tar /tmp/test4 

 

7. Extract multiple files from tar ball



# tar -xvf test.tar “test1” “test2” 

 

8. Extract group of files from tar ball


# tar -xvf test.tar “*.test” 

9. Add / append files or directories to tar ball



# tar -rvf test.tar /tmp 

 

10. To verify the tar ball



# tar tvfW test.tar 

 

11. Finding the difference between gzip archive file and file system



# tar -dvf test.tar.gz 

 

12. Finding the difference between bzip2 archive file and file system



# tar -dvf test.tar.bz2 

 

13. Creating a gzipped tar ball/archive



# tar -czvf test.tar.gz /tmp 

 

14. Creating a bzip2 tar ball/archive



# tar -jcvf test.tar.bz2 /tmp 

 

15. Extract gzip / bzip2 tar ball


# tar -xvf test.tar.gz

# tar -xvf test.tar.bz2 

 

16. List the contents for gzip / bzip2 tar ball


# tar tvf test.tar.gz

# tar tvf test.tar.bz2 

17. Extract single files from gzip / bzip2 tar archives


# tar -jxvf test.tar.bz2

# tar -zxvf test.tar.gz 

 

18. Extract group of files from gzip / bzip2 tar archives


# tar -jxvf test.tar.bz2 –wildcards “test*”

# tar -zxvf test.tar.gz –wildcards “test*” 

 

19. To take the backup of any file system


# tar -cvf etc.bk /etc
# tar -cvf tmp.bk /tmp
# tar -cvf var.bk /var

# tar cvf boot.bk /boot 

 

20. To backup your system



# tar -cvpzf backup.tar.gz –exclude=/backup.tar.gz –one-file-system /  

Leave a Reply

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