How to zip/tar unzip/untar in linux?

How to use tar command? 

tar (tape archive) command bundles a bunch of files together and creates an archive (commonly called a tar file or tarball) on a tape, disk drive, or floppy disk. The original files are not deleted after being copied to the tar file. To create an archive using tar, use a command like this, which bundles all the files in the current directory that end with .jpg into the alldocs.tar file:

tar -cvf alldocs.tar *.jpg

  • The below command tars all contents of “abcd” folder, into “abcd.tar”.

tar -cvf panda.tar abcd/

Below options are used throughout in all different commands .

c – Create a new archive.
t –  List the contents of an archive.
x – Extract the contents of an archive.
f –  The archive file name is given on the command line (required whenever the tar output is going to a file)
M- The archive can span multiple floppies.
v – Print verbose output (list file names as they are processed).
u – Add files to the archive if they are newer than the copy in the tar file.
z – Compress or decompress files automatically.

  • Command to create tar files on tape drives or floppy disks, like this:

tar -cvfM /dev/fd0 

abcd Archive the files in the abcd directory to floppy disk(s).                                                                                

(fd0 is the mount point for floppy disk)

  • To automatically compress the tar file as it is being created, add the z flag, like this:
tar -cvzf alldocs.tar.gz *.txt                                                                                                                                   
.gz suffix to the archive file name, as z flag tells tar to use the same compression as the gzipcommand.

  • Command to list all files in alldocs.tar. 
tar -tvf alldocs.tar                                                                                                                                              
  • To extract the contents of a tar file, use the x (extract) flag in a command, like this:

tar -xvf abcd.tar 
Extract files from abcd.tar

Above command will copy all the files from the abcd.tar file into the current directory. tar file bundles up all the files in a directory, as well as any subdirectories and the files in them. So when you’re extracting a tar file, keep in mind that you might end up with some new subdirectories in the current directory.

  • Creating a bzipped tar archive using option cvjf
tar -cvjf archive_name.tar.bz2 dirname/

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

Extract a gzipped tar archive ( *.tar.gz ) using option xvzf
tar -xvzf archive_name.tar.gz

Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf
tar -xvjf archive_name.tar.bz2
v-verbose is optional, which lists the file being processed.
To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.

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

tar xvf archive_file.tar /path/to/file 

Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.

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

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

The following extracts only a specific directory from a large tar file.

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

  • Extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below. 
tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/

  • Adding a file or directory to an existing archive using option -r

tar -rvf archive_name.tar newfile 

  • Adding a directory to the tar is also similar, 
tar rvf archive_name.tar newdir/

  • Verify files available in tar using option -W
tar -cvfW file_name.tar dir/

  • Estimate the tar archive size ( in KB )

tar -cf – /directory/to/archive/ | wc -c

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply