Archiving & Extracting Content in Linux


Creating an tar, gzip, bz2 archive

tar cvf archive_name.tar dirname
tar cvfz archive_name.tar dirname
tar cvfj archive_name.tar dirname

  • c – create a new archive
  • v – verbosely lists files 
  • f – following is the archive file name
  • z – archive through gzip
  • j – archive through bzip2

bzip2 takes more time to compress and decompress than gzip.
bzip2 archival size is less than gzip.


Note: .tbz and .tb2 is same as .tar.bz2

Extracting tar, gzipped, bzipped archive

tar xvf archive_name.tar
tar xvfz archive_name.tar.gz
tar xvfj archive_name.tar.bz2
  • x – extract files from archive
Note: In all the above commands v is optional, which lists the file being processed.

View the tar archive file content without extracting

for tar       : tar tvf archive_name.tar
for tar.gz    : tar tvfz archive_name.tar.gz
for tar.bz2   : tar tvfj archive_name.tar.bz2


Extract a single file from tar, tar.gz, tar.bz2 file

tar xvf archive_file.tar /path/to/file
tar xvfz archive_file.tar.gz /path/to/file
tar xvfj archive_file.tar.bz2 /path/to/file


Extract a single directory

tar xvf archive_file.tar /path/to/dir/

Extract multiple directories

tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/
tar xvfz archive_file.tar.gz /path/to/dir/
tar xvfj archive_file.tar.bz2 /path/to/dir/


Extract group of files

tar xvf archive_file.tar --wildcards '*.pl'

–wildcards *.pl – all files with pl extension

Adding a file or directory to an existing archive

tar rvf archive_name.tar newfile
tar rvf archive_name.tar newdir/


Note: You cannot add file or directory to a compressed archive.(Only in tar)

Verifying files 

tar cvfW file_name.tar dir/

Estimate the tar archive size

The following command, estimates the tar file size ( in KB ) before you create the tar,tar.gz, tar.bz2 file.


tar -cf - /directory/to/archive/ | wc -c 20480
tar -czf - /directory/to/archive/ | wc -c 508
tar -cjf - /directory/to/archive/ | wc -c 428

No comments:

Post a Comment

Popular Posts