Basic Usage Tar Command To Compress And Extract Files And Directories

The Linux ‘tar’ command stands for archive and is use to create Archive, compress and extract of Archived files. Tar command in Linux is one of the command which provides archiving functionality in Linux.

What is an Archive file?

An Archive files is a files that’s carry are one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage or simply to compress files to use less storage space.

Syntax how to use tar command:

tar filename.tar filename

Flags and options:

-c for create a new archive

-x for Extract files from an archive

-t for List the contents of an archive

-v for Verbose output

-f  for file.tar.gz use archive file

-C for DIRChange to DIR before performing any operations

-z for Filter the archive through gzip i.e. compress or decompress archive

-u for archives and adds to an existing archive file

-A for Concatenates the archive files

-j for filter archive tar file using tbzip

-W for Verify a archive file

-r for update or add file or directory in already existed .tar file

wildcards for specify patterns in unix tar command.

Steps to create a tar file 

 

The procedure is as follows to create tar a file in Linux:

  • Open the terminal app in Linux.
  • Compress an entire directory by running tar -zcvf file.tar.gz  /path/to/dir/ command in Linux
  • Compress a single file by running tar -zcvf file.tar.gz  /path/to/filename command in Linux
  • Compress multiple directories file by running tar -zcvf file.tar.gz  dir1 dir2 dir3 command in Linux.

Command:

tar -czvf name-of-archive.tar.gz  /path/to/file

Output:

1

2

Command:

tar -czvf name-of-archive.tar.gz /path/to/directory

Output:

3

Command:

tar -cjvf Sample.tar.bz2 /path _of_the_file

Output:

5

How to extract the files or directory

Command:

tar -xvf name-of-tar-file

Output:

6

Command:

tar -xvf name-of-tar-directory

Output:

7

How to extract the gunzip and bz2 files or directories

Command:

tar -xvf name-of-tar.gz

Output:

8

 

Command:

tar -xvf name-of-tar.bz2

Output:

9

 

At last but not the lease in this blog we have seen how to compress, archive and extract the files or directories with the use of the tar command with multiple parameters. These are the some basics commands with the use of tar utility. For more commands and parameters of tar we can use help or man (eg. man tar) command in the linux bases O.S. machine.

Leave a Reply