9951 explained code solutions for 126 technologies

cli-tarHow do I use the Unix tar command to compress a folder?

The Unix tar command is used to compress a folder by creating a tarball (a tar archive file). To use the tar command, open the command line and type:

tar -cvf <filename>.tar <foldername>

This command will create a tar archive file named .tar containing the contents of the folder.

The parts of the command are as follows:

  • tar - the command to create an archive
  • -cvf - the flags used to create the archive
    • -c - create a new archive
    • -v - verbose output (optional)
    • -f - specify the filename of the archive
  • <filename>.tar - the name of the archive to be created
  • <foldername> - the name of the folder to be compressed

To extract the contents of the archive, use the following command:

tar -xvf <filename>.tar

This command will extract the contents of the .tar archive into the current working directory.

Helpful links

Edit this code on GitHub