What is tar command and usage with examples? Define 10 tar commands in Linux?

What is tar command and usage with examples? Define 10 tar commands in Linux?

Tar stands for the "Tape Archieve" and is used to deal with tape drives. In linux "tar" command used to create tar file, which we can also say that to write the tar archive to a device file(in/dev). By using tar commands one user can easily archieve large collection of file and another user may extract those files.

10 tar commands in linux

1. Extract a tar.gz archive

$ tar -xvzf tarfile.tar.gz

2. Extract files to a specific directory or path

$ tar -xvzf abc.tar.gz -C /opt/folder/

You have to ensure that destination directory is exists.

3. Extract a single file out of an archive

$ tar -xz -f abc.tar.gz "./new/abc.txt"

4. Extract multiple files using wildcards

For the bunch of files we can also use the wild cards. Like all files with ".txt" extension

$ tar -xv -f abc.tar.gz --wildcards "*.txt"

5. List and search contents of the tar archive

$ tar -tz -f abc.tar.gz

6. Create a tar/tar.gz archive

$ tar -cvf abc.tar ./new/

compressed archive does not create with above exmaple.

"z" or "j" option for gzip or bzip is used respectively for compressed the files.

$ tar -cvzf abc.tar.gz ./new/

7. Ask confirmation before adding files

Only those files would be added which are given a yes answer. If you do not enter anything, the default answer would be a "No".

# Add specific files

$ tar -czw -f abc.tar.gz ./new/*
add ‘./new/abc.txt’?y
add ‘./new/cde.txt’?y
add ‘./new/newfile.txt’?n
add ‘./new/subdir’?y
add ‘./new/subdir/in.txt’?n

# Now list the files added

$ tar -t -f abc.tar.gz

8. Add files to existing archives

$ tar -rv -f abc.tar abc.txt

Files can only be added to plain tar archives not to compressed archives (gz or bzip)

9. Add files to compressed archives (tar.gz/tar.bz2)

$ gunzip archive.tar.gz
$ tar -rf archive.tar ./path/to/file
$ gzip archive.tar

For bzip files use the bzip2 and bunzip2 commands respectively.

10. Backup with tar

$ tar -cvz -f archive-$(date +%Y%m%d).tar.gz ./new/

make sure that the disk space is not overflown with larger and larger archives.
First release
Last update
0.00 star(s) 0 ratings