How to compress and extract files using tar command in Linux – Basic Example

Here is a quick example of how to compress and extract a tar files in linux operating system.

We can see what are the available options we have got of tar command by doing:

man tar

or

tar --help

We normally use the following 5 commonly used options. These are c, z, f, v, x

  • c = create a new tar file
  • v = verbose, display file to compress or uncompress
  • f = create the tar file with filename provided as the argument
  • z = use gzip to zip it
  • x = extract file

1. Create & Compress

Create and compress it with command tar tool. Syntax of the tar command is:

tar -cvzf new_tarname.tar.gz folder-you-want-to-compress

In the following example, we compress (gunzip) a folder named forum into a new tar file my_forum_files.tar.gz.

$ tar -cvzf my_forum_files.tar.gz forum

2. Extract or Uncompress

To UnCompress it, use command -x option with tar command.

In the following example, we are uncompressing:

$ tar -xvzf my_forum_files.tar.gz

Conclusion

With the tar command line interface we can compress and uncompress large files easily using c, v, f, z, x options.

Leave a Reply