Using tar on Linux and FreeBSD to work with archives. Creating and extracting archives using the Tar command in Linux Tar unpack while maintaining rights
The tar utility in Linux is designed to combine (pack) several files into one, i.e., to create archives. She has good functionality for working with archives, can effectively move files (archives or tar files) from one device to another (including tape devices), while maintaining file attributes and file system structure. All this allows you to use the utility as a full-fledged tool for organizing backups. Of course, there are other specialized utilities for these purposes. Like dump, for example, but it is useful for system administrators to know how to handle tar as well.
Features of using the tar utility
As already noted, the utility combines several directories and files into one tar file, which can then be compressed using, for example, gzip, compress or bzip2. This is very convenient when you need to back up data and free up additional disk space. The utility is often used when you need to create short-term backup copies of data.
Among the non-GNU versions of tar, the vast majority do not support filename lengths greater than 100 characters, and also do not work with multiple tapes, so before using the specific version of the utility available, you must carefully read the official technical See the documentation (man tar command) for details like this. Also, not all versions of tar process symbolic links by default - different switches are used for this from version to version, which can also be found in the documentation. The utility is very well suited for quickly copying a directory tree.
tar command syntax and options
The tar utility has the following syntax:
tar[options ] Name_archive files_for_archive
For example, using the command:
$ tar -cf etc.tar /etc
You can make a copy of the /etc directory into the etc.tar archive. The following example demonstrates a common case of backing up using the tar command. Here the -c option means creating an archive, -f means outputting the results to a file. In the second part of the command, which takes the output stream from tar -cf as input, the -x (extract) option tells tar to extract the contents of the archive, and -p to save information about file access modes. There is also the -b option, which allows you to set the block combining ratio, which is useful when writing to tape devices. This in some cases allows for greater performance. The optimal value is 20, however, by experimenting, you can find the best option for specific conditions. The -v option allows you to print detailed information about the progress of the team's work.
Syntax for unpacking:
tar[options ] archive
For example the command
$tar xfv archive.tar
Unpack archive.tar into the current directory. To specify the directory where you want to unpack the archive, use the -c switch. For example
$ tar xfv archive.tar -c /var
Below is a basic list of tar command options
-A, --catenate, --concatenate attaching tar files to the archive
-c, --create creating a new archive
-d, --diff, --compare search for differences between the archive and file system
--delete deleting from the archive (not on magnetic tapes!)
-r, --append adding files to the end of the archive
-t, --list list the contents of the archive
--test-label checking the archive volume label and exiting
-u, --update adding only newer files to the archive
-x, --extract, --get extract files from the archive
Please note that when running the command, it is not necessary to put a hyphen in front of the keys
tar examples
As already mentioned, to create compressed archives, you first create the archive itself with the tar command, and then this archive is compressed using gzip. But you can do all this with one tar command:
$tar -cvzf archive.tgz *
As a result, it will be created compressed gzip archive.tgz archive, which will be compressed using gzip and thanks to the z option. The “*” symbol indicates that everything in the current directory will be archived. The target archive archive.tgz will also be created here. Instead of the z option, you can specify others - Z, J, j, which specify the corresponding compression algorithms - LZV, xz and bzip2.
Just in case, before creating backups, it is better to go to the root directory “/” - the utility removes the initial “/” character from file names/paths, which is why information from archives may not be restored correctly later.
In case of backup(on tape) you can use the following commands:
$ cd / $ tar -cpf /dev/st0 --label="Backup at `date "+%d-%B-%Y"`." -C /home
As you can see, /dev/st0, a tape device, is used here as the archive name. Therefore, to identify archives, it is useful to use the --label option, which specifies a description of a specific archive. The backquotes contain a mask for the date/time the archive was created. The -C (or --directory) option specifies the directory to be backed up. To perform incremental archiving, i.e. when you need to back up only files that have changed since the previous archiving, you should use the -N option:
$ cd / $ tar -cpNf /dev/st0 --label="Backup at `date "+%d-%B-%Y"`." -C /home
After creation backup copy It would be a good idea to check using the -d option that the archive is intact:
$ cd / $ tar -dvf /dev/st0
To backup the entire system, you can use the following command:
$ cd / $ tar -cpf /sysarchive/system-at-`date "+%d-%B-%Y"`.tar -C / --exclude=proc --exclude=mnt --exclude=archive -- exclude=cache --exclude=*/lost+found .
Here, using the --exclude options, you specify which directories should be excluded from the archiving procedure, and the dot symbol "." at the end of the command says that it is necessary to process all directories and files located in the directory specified by the -C option. It should be noted that in in this example, unlike the previous ones, the tar file itself is specified as the target archive, and not the tape device.
If you find an error, please highlight a piece of text and click Ctrl+Enter.
Utility command line tar is used to work with archives on UNIX-based operating systems. With its help, you can archive data and optimize the use of disk space.
Creating and unpacking archives
Create
Creation is performed with a key c. The syntax is as follows:
tar -c<опции> <новый архивный файл> <что сжимаем>
Creation example:
tar -czvf archive.tar.gz /home/dmosk
* in this example an archive will be created archive.tar.gz user's home directory ( /home/dmosk)
** Where z— compress the archive in gzip (without this parameter, tar does not compress, but creates a so-called tarball); c— key to create an archive; v— verbose mode, that is, with the process displayed on the screen (very convenient for monitoring the progress of work, but in scripts it can be missed); f— use a file (we definitely specify it, since we mostly work with files).
Unpack
Unpacking is carried out with a key x with syntax:
tar -x<опции> <архивный файл>
Unpacking example:
tar -xvf archive.tar.gz
Examples
gz
Unpacking the .gz file:
tar -xvf archive.tar.gz
* if an error occurs This does not look like a tar archive, you can use the command gzip -d archive.tar.gz.
bz2
tar -xvjf archive.tar.bz2
* key j is responsible for working with bz2.
If the system complains about bzip2, then you need to install it:
yum install bzip2
apt-get install bzip2
pkg install bzip2
* respectively, for CentOS (RPM based), Ubuntu (deb based), FreeBSD (BSD based).
If we see an error tar: Unrecognized archive format, let's use the following command:
bzip2 -d archive.tar.bz2
gzip
tar -xvzf archive.tar.gzip
* the z key is responsible for working with gzip.
tgz
Unpacks like gzip:
tar -xvzf archive.tgz
Unzip to a specific folder
tar -C /home/user -xvf archive.tar.gz
* key -C used to specify the folder where files from the archive should be unpacked.
Unpacking without subfolder
This method can be used to unpack into a pre-prepared directory. There will be some effect of renaming the directory or an analogue of “Unpack here”:
tar -C /home/admin/mytar -xvf admin.tar.gz --strip-components 1
* catalog /home/admin/mytar must be created in advance; --strip-components 1 will skip one subfolder inside the archive.
Excluding files by mask
If you need to skip some files, enter the command with the --exclude switch:
tar -czvf archive.tar.gz /wwwsite --exclude="sess_*"
* in this example we will create an archive archive.tar.gz, which will not contain files starting with sess_.
Description of tar keys
Commands for action
* You cannot use several of the above keys in one command.
Additional options
Key | Description |
---|---|
--atime-preserve | Leave the same access timestamp for the file. |
-b N | Set the block size to N x 512. |
-C | Change directory. By default, the one we are in is used. |
--checkpoint | Show folder names when reading an archive file. |
-G | Use the old incremental backup format when displaying or retrieving. |
-g | Use the new incremental backup format when displaying or retrieving. |
-h | Don't duplicate symbolic links. Only the files that these symlinks point to. |
-i | Ignore blocks of zeros. |
-j | Using bzip2. |
--ignore-failed-read | Ignore unreadable files. |
-k | When unpacking, existing files are not replaced by the corresponding files from the archive. |
-L N | Changing magnetic tape after N*1024 bytes. |
-m | When retrieving, ignore the object's modification time. |
-M | Multi-volume archives. |
-N DATE | Only save newer files relative to DATE |
-O | Direct output to standard output. |
-p | Retrieval of protected information. |
-P | Does not strip leading slash (/) from names. |
-s | Sorting files during extraction. |
--preserve | Same as -ps |
--remove-files | Delete source files after adding to archive. |
--same-owner | Preserve owner when extracting. |
--totals | Output bytes when creating an archive. |
-v | Action logging—displays a list of objects on which an action occurs. |
-V NAME | Creating an archive on a volume labeled NAME. |
--version | Show tar version. |
-w | Require confirmation for every action. |
-W | Checking the archive after recording. |
--exclude FILE | Exclude file FILE. |
-X FILE | Exclude FILE files. |
-Z | Filters the archive using compress. |
-z | Using gzip. |
* the current list of options can be obtained with the command man tar.
On Unix (this is Linux, Freebsd, etc.) systems, the tar command is the main archiving utility. Understanding this command's many options will allow you to master archive manipulation.
In this article I would like to look at different examples, including creating a tar archive (using gzip and bzip compression), unpacking separate file or directory, viewing the contents of a tar archive, validating the integrity of a tar archive, finding out the difference between a tar archive and a file system, calculating the size of an archive before creating it, and others.
Creating an archive using the tar command
Creating and unpacking a tar archive is done using the cvf option. This is what the basic command for creating an archive looks like:
$tar cvf archive_name.tar dirname/
Let's look at each key from the option separately:
- c – creating a new archive
- v – output of a list of files for processing
- f – archive file name
To create a gzip compressed archive you need to use the cvzf option. The previous cvf option does not use any compression at all. To use gzip compression add the z option as shown below:
$ tar cvzf archive_name.tar.gz dirname/
- z – packs the archive using gzip compression
Personally, I prefer to always leave the cvf option unchanged and only add it at the end required keys if compression is required. For example, cvfz or cvfj. It's easier to remember this way.
Now let's create an archive using bzip2 compression:
$ tar cvfj archive_name.tar.bz2 dirname/
- j – packs the archive using bzip2 compression
gzip or bzip2? Compressing and decompressing an archive using bzip2 takes a little longer and the archive itself is smaller.
Creating a tar archive with the current date in the archive name
If you are creating a backup, it is very important to know exactly when it was created. The easiest way to find out about this is from his name.
Tar -cvf archive-$(date +%Y%m%d).tar.gz dirname/
Unpacking an archive using the tar command
To unpack the archive, use the xvf command:
$tar xvf archive_name.tar
- x – extract files from archive
To unpack tar.gz use the xvfz option:
$tar xvfz archive_name.tar.gz
To unpack a bzip2 compressed archive, use the xvfj option:
$tar xvfj archive_name.tar.bz2
Viewing files in a tar archive
To view the contents of a tar archive, use the tvf option.
$tar tvf archive_name.tar
To view the contents of an archive compressed using gzip, use the tvfz option
$ tar tvfz archive_name.tar.gz
To view the contents of an archive compressed using bzip2, use the tvfj option
$tar tvfj archive_name.tar.bz2
Extracting a single file from tar, tar.gz, tar.bz2 archives
There are situations when large archive Only 1 file needs to be extracted:
$ tar xvf archive_file.tar path/to/file
For gzip and bzip2 compressed archives respectively use:
$ tar xvfz archive_file.tar.gz path/to/file $ tar xvfj archive_file.tar.bz2 path/to/file
Extracting a single folder from tar, tar.gz, tar.bz2 archives
Extracting an individual folder, including all subfolders and files, is performed in the same way as extracting an individual file:
$ tar xvf archive_file.tar /path/to/dir/
To extract several folders from an archive, specify their names in order:
$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/
For compressed archives the same is true, only using additional keys:
$ tar xvfz archive_file.tar.gz /path/to/dir/ $ tar xvfj archive_file.tar.bz2 /path/to/dir/
Extracting a group of files from tar, tar.gz, tar.bz2 archives using regular expressions
To extract a group of files based on certain characteristics in the name you can use regular expressions. For example, let's extract from the archive all files with html extension.
$ tar xvf archive_file.tar --wildcards "*.pl"
Adding a file or folder to an existing tar archive
To add a new file or folder to an existing archive, use the -r option:
$ tar rvf archive_name.tar newfile
Adding a folder is done in the same way:
$ tar rvf archive_name.tar newdir/
Remember that the operation of adding files and folders only works for uncompressed tar archives.
Determining tar archive size before creating it
The following command allows you to determine the size of a tar.gz or tar.bz2 archive before creating it (in KB)
Tar -czf - /directory/to/archive/ | wc –c $ tar -cjf - /directory/to/archive/ | wc -c
In this article, we will show you how to use the Tar utility to extract and create tar archives using practical examples And detailed explanations the most common Tar parameters.
What is Tar?
The tar command is used to create tar archives by converting a group of files into an archive. It also has the ability to extract tar archives, display a list of files included in the archive, add additional files into an existing archive, as well as various other types of operations.
Tar supports a wide range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. When creating compressed tar archives, it is common practice to add a compressor suffix to the archive file name. For example, if the archive was compressed using gzip, it would be named archive.tar.gz.
Tar was originally designed to create archives when storing files on magnetic tape, which is why it gets its name " T ape AR chive."
There are two versions of tar, BSD tar and GNU tar, with some functional differences. Majority Linux systems come with GNU tar preinstalled by default. If you don't have tar installed, we recommend installing it first by following our article.
Command line syntax
Before we move on to using the tar command, let's start by reviewing the basic syntax.
Tar
- OPERATION – Only one argument is allowed and required. Most commonly used operations:
- --create (-c) – Create a new tar archive.
- --extract (-x) – Extract the entire archive or one or more files from the archive.
- --list (-t) – display a list of files included in the archive
- OPTIONS – Most frequently used operations:
- --verbose (-v) – Show files processed by the tar command.
- --file=archive=name (-f archive-name) – Specifies the name of the archive file.
- ARCHIVE_NAME – Archive name.
- FILE_NAME(s) – A space-separated list of file names to be extracted. If not provided, the entire archive will be extracted.
When executing the tar command, you can use the long or short form of tar operations and parameters. Long forms are more readable and short forms are faster. Long form options are prefixed with a double dash (--). Short form parameters are prefixed with a single dash (-), which can be omitted.
Creation of Tar Archive
Use the -c operator to create a tar archive. For example, to create an archive file named archive.tar from files named file1, file2, file3, run the following command:
Tar -cf archive.tar file1 file2 file3
Here is the equivalent command using long form parameters:
Tar --create --file=archive.tar file1 file2 file3
The following example will create a backup.tar archive from the /home/username directory:
Tar -cf backup.tar /home/username
You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless the --no-recursion option is specified. Use the -v option if you want to view the files that are being processed.
Creating a Tar Gz archive
Gzip is the most popular compression algorithm for tar files. When compressing tar archives with gzip, the archive name must end with either tar.gz or tgz .
To create a tar.gz archive from the given files, you can use the following command:
Tar -czf archive.tar.gz file1 file2
The -z option tells tar to compress the archive using the gzip algorithm.
Creating a Tar Bz2 archive
Another popular compression algorithm for tar files is bzip2. When compressing tar archives with bzip2, the archive name must end with either tar.bz2 or tbz .
When the -j option is specified, tar will use the bzip2 archive compression algorithm.
The following command will create a tar.bz2 archive from the given files:
Tar -cjf archive.tar.bz2 file1 file2
Tar archive listing
To list the contents of a tar archive, use the --list (-t) operation.
Tar -tf archive.tar file1 file2 file3
If you specify the contents of the archive using the --verbose (-v) option, tar will print more information, such as owner, file size, timestamp, etc.:
Tar -tvf archive.tar -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file1 -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file2 -rw-r--r-- linuxize/users 0 2018-09-08 20:15 file3
Extracting the Tar Archive
To extract a tar archive, use the --extract (-x) operator and specify the archive file name:
Tar -xf archive.tar
The -v option is also usually added to print the names of the extracted files.
Tar -xvf archive.tar
By default, tar will extract the contents of the archive in the current working directory. Use --directory (-C) to extract archived files in a specific directory:
For example, to extract the contents of an archive into the /opt/files directory, you could use:
Tar -xf archive.tar -C /opt/files
Extracting Tar Gz and Tar Bz2 archives
When extracting compressed archives, such as tar.gz or tar.bz2, you do not need to specify a decompression option. The command is the same as when extracting the tar archive:
Tar -xf archive.tar.gz tar -xf archive.tar.bz2
Extracting specific files from a tar archive
To extract specific files from a tar archive, add a list of file names, separated by spaces, to be extracted after the archive name:
Tar -xf archive.tar file1 file2
When extracting files, you must provide their exact names, including the path, as typed --list (-t).
Extracting one or more directories from an archive is similar to extracting files:
Tar -xf archive.tar dir1 dir2
If you try to extract a file that does not exist, you will receive an error message similar to the following:
Tar -xf archive.tar README tar: README: Not found in archive tar: Exiting with failure status due to previous errors
Extracting files from a Tar archive using a wildcard
To extract files from an archive based on a wildcard pattern, use the --wildcards switch and specify the pattern to prevent shell interpretation.
For example, to extract files whose names end in .js (Javascript files), you can use:
Tar -xf archive.tar --wildcards "*.js"
Adding files to an existing archive
To append files or directories to an existing tar archive, use the --append (-r) operation.
For example, to add a file named newfile to archive.tar, you could use:
Tar -rvf archive.tar newfile
Removing files from a tar archive
Use the --delete operation to delete files from the archive.
For example, to remove a file named file1 from archive.tar, you could use:
Tar --delete -f archive.tar file1
Conclusion
By now you should have a good understanding of how to create and extract tar archives.
There is nothing better for remembering information than reading and then writing this information in a notebook, so I like to read and write information in my notebook website
Today we will talk about such a thing as archiving, namely packing and unpacking formats such as: tar, gzip, zip and rar in Unix and Linux operating systems
We will divide our article into sections for easy navigation.
Archiving in Linux
Linux or Linux is now the most popular system not only for system administrators, but also for the majority of ordinary citizens, and the reason is banal - the departure from Microsoft at the state level and the widespread introduction of “free software of Russian design”, but no matter what we call it, we all understand that the only free software that can replace the operating system for the average user system for office work is the Linux family. Therefore, as they say, you can already start studying it for everyone everywhere)))) but let’s not go away from the topic, but let’s get back to our sheep!
Although Linux has a graphical shell and you can even download graphic packages for working with the most popular archiver extensions, we will consider working with archives at the console level (but if anyone needs to, we will review packages for working not through the console)
Archiving Tar in Linux
Tar- or as we called him, he is not an archiver, the reason for this is the logic of his work of this instrument! it's just a packer, or to be more precise, tar's task is to create a container in tar format into which it places the files you select (without compression). But if you need to compress this data or compress it to reduce the size of the archive, then additional tools such as Gzip or bzip2 are used (you should have noticed that when downloading packages or files they had a strange name-file format .tar.gz) The whole point is that the principle of operation of the data compression tool only works in single-threaded mode, therefore, in order to archive many files and compress them, we needed to pack them into TAR (one file), and only then run them through Gzip or bzip2 so these tools work like Bonnie and Clyde)
Creating a TAR archive
First, let's start by packing the Tar archive in Linux/ubuntu, and for this you need to remember the pattern or command syntax:
tar [options]<имя файла.tar>
- c (create) - create an archive
- f (file) — specify the archive name
- additionally using bzip2
- additionally using gzip
To create a tar archive, we need only two options c and f, but if you want to see the process of packing files, you can add the v key and then you will see a running list of files on the screen (so to speak, an “analog” of Windows progress in the form of percentages)
tar -cvf newfile.tar /foto
in this command we indicated to create a newfile.tar archive and pack the entire foto folder
In the list of options, you noticed two keys j and z; these keys are used for compression into a gzip or bzip2 archive; if you need to archive with compression, then use the following command:
tar -cvjf newfile.tar /foto- will create an archive + compression newfile.tar.bz2
tar -cvzf newfile.tar /foto- will create an archive + compression newfile.tar.gz
Unpack TAR archive
Let's look at the command syntax to unpack or unzip a tar archive in linux/ubunta:
tar [options]<имя файла.tar>[files or folders that we will unpack]
Options for creating a tar archive:
- x (extract) - extract files from the archive
- f (file) — specify the archive name
- j (bzip2) - compress/unpack archive additionally using bzip2
- z (gzip) - compress/unpack archive additionally using gzip
- v (verbose) - lists progress
- C (upper case) - indicate an alternative location for unzipping files
tar -xvjf newfile.tar.bz2
tar -xvzf newfile.tar.gz -C /home/nibbl/desktop
General information on working with Tar
- tar --help — call help on commands and parameters
- man tar - calling extended documentation
- Official documentation for the tar command - link
Zip Archiving in Linux
The zip archiver is a star in the software world (well, not counting rar), this format is very popular for windows systems and by default it is already built into the clean assembly operating system, so only after installing Windows can you unzip the files into zip format
but let's get down to business, in Linux and Unix systems things are different, this is the Tar format by default, but for zip you need to install the unzip package
Installation:
- sudo apt-get update && apt-get upgrade
- sudo apt-get install unzip
Creating a ZIP archive
First, let's remember the command template or syntax for creating a zip archive in Linux:
zip [options]<имя файла.zip>[files or folders that we will pack]
Options for creating a tar archive:
- r (recurse) - recursive creation of an archive
- s (size) - splitting the archive into a specific size k (kB), m (MB), g (GB) or t (TB)
example: zip-s 300m<файл 1 ГБ>
at the output we get:
file.zip (300 mb, master file)
file.001.zip (300 mb)
file.002.zip (300 mb)
file.003.zip (100 mb) - P (password) — password-protect the archive (you can use the key e then you will enter the password in a separate line with asterisks)
example: zip -P mypassword -r file.zip ./home/nibbl/foto
example2: zip -er file.zip ./home/nibbl/foto - x - exclude files or directories from the archive
- 1-9 — compression level (where 1 is without compression, and 9 is the best compression)
zip –r -9 -P 123 — archive.zip /home/nibbl/desktop/myfile
- with this command we compressed the myfile folder, created an archive named archive.zip and set a password for the archive 123
Unpack zip archive
Let's look at the command syntax to unpack or unzip a tar archive:
unzip<имя файла.zip>
Options for creating a tar archive:
- d (directory) - specify the directory for unzipping
- l — display a list of files in the archive
- d - delete a specific file or directory from an already created archive
- v - shows detailed information on files in the archive ()
unzip<имя файла.zip> — unpack the archive into the category in which we are located
unzip<имя файла.zip>-d /home/nibbl/desktop- unpack the archive to your desktop
General information on working with Zip
- zip --help or unzip --help - call help on commands and parameters
- man zip or man unzip - call extended documentation
- Official documentation for the zip command - link