How to Tar a Directory (with Images)

Table of contents:

How to Tar a Directory (with Images)
How to Tar a Directory (with Images)
Anonim

The simplest and most popular way to manage large sets of files on Linux systems is to use the tar command. When you run the "tar" command on a directory, all the items contained in it are grouped into a single archive. The file obtained with the "tar" command can then be easily moved or archived. Alternatively it can also be compressed to reduce the space it occupies on the disk.

Steps

865895 1
865895 1

Step 1. Understand how the "TAR" format works

On Linux systems, archiving of multiple files is done through the use of the tar command. The latter creates a single archive made up of multiple files, allowing them to be easily transferred to another system or compressed and saved to tape or another storage device. The resulting file will have the extension.tar and often, in technical jargon, this type of file is referred to as a tarball.

It should be remembered that the tar command simply creates an archive consisting of all the elements present in a given path without performing any kind of compression. This means that the resulting file size will be equal to the sum of the original file sizes. However, it is possible to compress a.tar file using the gzip or bzip2 command, resulting in an archive with the extension.tar.gz or.tar.bz2. This step will be explained at the end of the article

865895 2
865895 2

Step 2. Create a TAR file from a single directory

When creating a folder "tarball", the "tar" command to use is made up of several parts. Here is an example of using the tar command:

tar -cvf file_name_TAR.tar / path / to / directory

  • tar - runs the "tar" archiving program.
  • c - this parameter tells the program to "Create" a ".tar" file and should always be the first parameter of the complete command.
  • v - this parameter indicates that the creation process will display on the screen the complete list of all the files that are added to the TAR file during creation. This is an optional parameter, which is often not used as it would produce a long and useless video output.
  • f - this parameter indicates that the next part of the "tar" command refers to the name that the final TAR archive will have to assume. Normally it is always indicated as the last parameter of the complete list of command parameters.
  • TAR_filename.tar - this is the name that will be assigned to the resulting TAR file. You can use whatever name you prefer; the important thing is to include the.tar extension at the end of the name. If you need to create the TAR file in a folder other than the one you are working in, you can specify the destination path along with the name of the TAR file.
  • / path / to / directory - this is the path where the source directory that will be used to create the final TAR file is stored. The path is relative to the workbook associated with your user account. For example, if the full directory path is ~ / home / username / Pictures and you are currently in the / home folder, you will need to use the following path / username / Pictures. Remember that all subfolders in the source directory will also be included in the final TAR file.
865895 3
865895 3

Step 3. Create a TAR file that includes multiple directories

Doing so is very simple: in fact, just enter at the end of the command all the paths of the source folders to be included. Here is an example of a tar command that creates a TAR archive from multiple directories:

tar -cvf file_name_TAR.tar / etc / directory1 / var / www / directory2

865895 4
865895 4

Step 4. Add a file or folder (or multiple items) to an existing TAR archive

To add a new file or directory to an existing TAR file, use the "append" parameter:

tar -rvf file_name_TAR.tar file.txt path / other / directory / source

r - this is the "append" parameter. In this case it replaces the c parameter, since the TAR file must not be created because it already exists

865895 5
865895 5

Step 5. Compress an existing TAR file

To quickly compress a ".tar" file you need to use the "gzip" command. If you need to get a higher compression ratio (to further reduce the TAR file size), you can use the "bzip2" command. In the latter case, the compression process will be longer than that of the "gzip" command.

gzip file_name_TAR.tar bzip2 file_name_TAR.tar

  • The gzip command creates a compressed file with the.gz extension, so the final file name will be filename_TAR.tar.gz
  • The bzip2 command adds the extension.bz2, so the full name of the compressed file will be filename_TAR.tar.bz2
865895 6
865895 6

Step 6. Compress the TAR file directly during the creation process

To compress an existing TAR file you can use the commands described in the previous step, but to create an already compressed TAR file you need to use the appropriate parameters:

tar -czvf name_TAR_file.tar.gz / path / to / directory tar -cjvf name_TAR_tar.tar.bz2 / path / to / directory

  • z - this parameter tells the program that the TAR file that will be generated must be compressed with the "gzip" command. In this case, you need to manually insert the.gz extension at the end of the file name.
  • j - this parameter indicates to the program that the TAR file that will be generated must be compressed with the "bzip2" command. In this case it is necessary to manually insert the.bz2 extension at the end of the file name.

Recommended: