How to archive a directory with the "tar" command

Table of contents:

How to archive a directory with the "tar" command
How to archive a directory with the "tar" command
Anonim

The command tar, whose name comes from the English abbreviation of " Tape Archiver”, was intended, at the start of the computer age, to back up files to magnetic tape. Today, adapted to the most recent operating systems, it is still the most basic way to save multiple grouped files as a single uncompressed archive intended to be saved to a hard drive or USB memory, and which you can then compress and then transfer between two computers.

Steps

865895 1

Step 1. Learn how a tar archive is made

The archiving of multiple files in Linux is done by means of the tar command, which will have the effect of grouping several files in a single archive, which you can compress later to reduce its size for easier transfer. to a remote computer. The resulting archive will have the.tar extension. In their professional jargon, computer scientists very often refer to these files as tarballs.

  • The tar command will archive files, but will not compress them. This is why an archive .tar will be the same size as the set of files it contains. You can compress it later using the gzip or bzip2 utilities, which will result in the addition of a.tar.gz or.tar.bz2 extension respectively to the name of your archive. We will discuss this possibility further.
865895 2

Step 2. Create an archive containing a single directory

You will need to add several extensions to the tar command to create an archive containing a directory. These extensions are represented by a group of letters following the command tar and must be written without spaces between them. This list of options must be preceded by the sign -.

tar -cvf archive_name.tar / path / to / source_directory

  • tar: this is the call to the archiving program tar.
  • -c: this switch creates a new archive .tar and must always be placed first in the list of options for using the command.
  • v: This switch, optional, causes the explicit display of the progress information of the archiving process, mainly the list of all the files successively added to the archive and the errors that the creation process may possibly encounter.
  • f: this switch, which must always being placed in the last position on the list of options, indicates that the following file name will be that of the newly created archive.
  • archive_name.tar: this is the file name to give to your new archive. You can give it whatever name you want, but be sure to always assign extension .tar. If you want to save it in a different folder from where the archive was created, you will need to precede its name with the full path to the destination directory.
  • / path / to / source_directory: enter the path to the directory you want to archive. This path must be relative to the directory in which you are located: if your current working directory is ~ / home and you want to archive the ~ / home / user / Images folder, you will have to enter the path / user / Images (omitting the " ~ "). Note that all the folders contained in this directory will also be included in the archive.
865895 3

Step 3. Create an archive containing multiple directories

Including multiple directories in an archive .tar consists in adding to the command line all the paths to the directories to be included in the tar archive. Each of these paths should be separated by a space.

tar -cvf archive_name.tar / etc / source_directory1 / var / source_directory2

865895 4

Step 4. Add files or folders to an existing archive

You can then add documents and directories to the archive .tar that you just created using the add files option which is materialized by the -r switch following the tar command.

tar -rvf archive_name.tar ftext.txt path / to / source_directory

r: This switch has the effect of adding files or directories to an existing archive. As with the "-c" flag we just saw, it must be placed first on the list of options for using the command

865895 5

Step 5. Compress an already existing archive

You will have the choice between two compression utilities to reduce the size of your "tarball": For a fast compression, you can use the compressor gzip, but if you want to get a higher compression rate (thus a more significant reduction in the size of the archive), it will be better to use bzip2. The only drawback you will encounter with this utility is the slightly longer time it takes to work the archive due to the sophistication of the compression and decompression algorithms used.

gzip archive_name.tar bzip2 archive_name.tar

  • gzip will append the.gz extension to the name of the archive you are compressing, which will result in archive_name.tar.gz.
  • bzip2 will append the.bz2 extension to the name of the archive you are compressing, which will give you Archive_name.tar.bz2.
865895 6

Step 6. Compress your archive while creating it

While you can use the commands mentioned above to compress already existing archives, you also have the option of compressing them as you create them by simply using the appropriate option switches.

tar -czvf Archive_name.tar.gz / path / to / directory tar -cjvf Archive_name.tar.bz2 / path / to / directory

  • z: this switch will cause the archive to be compressed .tar using the utility gzip. Make sure to put the.gz extension at the end of your archive name.
  • j: this switch will cause the archive to be compressed .tar using the utility bzip2. Be sure to put the.bz2 extension at the end of your archive name.

Advice

  • Online commands for Unix, Linux, BSD and derivative systems must be invoked in lowercase letters, otherwise they will not be recognized. Some option switches must be written in lower case letters while others must be in upper case, and many of them must be preceded by a "-" sign, while others must be preceded by a "-" sign. "+" sign. The position of certain switches in an option chain is important. Regarding the "tar" command, the "-x" and "-X", "-g" and "-G" or "-f" and "-F" options will have different behavior depending on their case.
  • You can get the list and meaning of the switches applicable to the "tar" command by entering tar - ?, tar --help, tar --usage, or man tar. This last command will display in your terminal a detailed instruction manual of the program.

Popular by topic