File compression with the 'tar' command


File compression with the 'tar' command

File compression is an essential utility in all platforms. It helps you to reduce file size and to share files efficiently. Copy to remote servers, and compressed files are accessible. We can compress old and rarely used files and protect them for future use; it helps to protect the disk space.

What is the ‘tar’ command?

We also use the tar command to compress and expand files from the command line. The following flags use the tar command to customize the command input:

Flag Explanation Usage
-c Create a new archive. We use this flag whenever we need to create a new archive.
-z Use gzip compression. When we specify this flag, the archive will be created using gzip compression.
-v Provide verbose output. Providing the -v flag shows details of the compressed files.
-f Archive file name. Archive file names are mapped using the -f flag.
-x Extract from a compressed file We use this flag when files need to be extracted from an archive.

How to remove files after compression?

It is not necessary to keep the original files after creating an archive. To do that, we can use –remove-- files flag.

How to view the contents of an archive?

We can able to see the contents of an archive without extracting it, and it can be done with the help of the –t flag.

How to extract an archive file?

It can be done with the help of –x flags.

Compress and exclude one or multiple folders

If you want to extract a folder but exclude one or several folders during the extraction, you can use the --exclude option.

Strip leading components - To strip any number of leading components, use the --strip-components option.