tar command - file archiving and decompression

You can use the tar command to combine a directory and the files below it into a single file. Even if you copy the combined files, the information such as the internal update date and time is maintained.

Archive to tar file

To archive the contents under the directory into a tar file, use the "cf" option of the tar command. (Suppose there is a directory called project in the current directory.)

tar cf project.tar project

C means to create an archive. f means a file, and is specified when a file or directory is the target. The first argument is the file name after archiving. It should look like project.tar. The second argument is the target directory. Archives are sometimes called library stacks in Japanese.

Extract the tar file

Use the xf option to extract the archived one. x means extract, which means extract.

tar xf project.tar

Difference between archiving and compression

tar is a command for archiving, and compression / decompression commands (gzip, gunzip etc.) is different. Archiving is the combination of multiple files into a single file. Compression is the reduction of the number of bytes in a file using a compression algorithm.

"z" option to compress / decompress gzip at the same time

Generally, the file is "tar.gz" and is often hardened. This is a file that has been compressed using gzip after archiving the file with tar.


You can unzip and unpack this all at once by adding the "z" option.

tar xfz project.tar.gz

You can also archive and compress at the same time by adding the z option.

tar cfz project.tar.gz project

Keep in mind that the "xfz" and "cfz" options are very common.

"v" option to display the processing contents of archiving and decompression

When used together with the "v" option, the processing contents of archiving and decompression are output.

tar xfzv project.tar.gz

It is easy to understand because you can see what kind of processing was done.

Associated Information