Tar how to use

Tar how to use

How to Use Linux Tar Command

The tar command comes preinstalled in almost every Linux distribution out there. So, it’s ready when you need it.

In this article, I am going to show you how to use the Linux tar command to compress files and decompress compressed files. So, let’s get started.

Creating tar Archives:

I have a directory

/projects in my home directory. I have the following files and directories in the

/projects directory. I will use these files and directories to demonstrate how to make archive files with the tar command in this article.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

To create a tar archive of everything in the

/project directory, run the tar command as follows:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The archive project.tar should be created.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

As you can see, the archive file project.tar is created. It is 51 MB in size.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

By default, the tar archive is not compressed. But, if you want you can compress the contents of the archive using gzip and bzip2 algorithm.

To perform gzip compression in the earlier example, you have to use the -z option of the tar command as follows:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

project.tar.gz archive should be created. As you can see, the file size is slightly smaller than the uncompressed version. In real life scenario, you will get better results because I generated these files using the /dev/urandom and dd commands. So, the compression algorithms didn’t work that well.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

To perform bzip2 compression in the earlier example, you have to use the -z option of the tar command as follows:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

As you can see, the project.tar.bzip2 archive is created.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Compressing Specific Files and Directories:

You don’t have to compress a directory if you don’t want to. You can specify different files and directories in different path (relative or absolute) in the tar command and compress them as follows:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The specified files and directories are compressed into an archive file is important_etc.tar.gz.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Excluding Files and Directories:

When you need to compress an entire directory with the tar command and you don’t want to include some files and directories within, you can use the –exclude option of the tar command as follows:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

As you can see, the test.img file and docs/ directory including its contents are excluded from the archive.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Listing the Contents of a tar Archive:

Before you extract a tar archive, it’s always a good idea to know the file and directory structure of a tar archive. You can list all the files and directories inside of a tar archive with the following command:

As you can see, the file and directory structure of the tar archive is printed.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

To see the file and directory permissions and other information about the files and directories inside a tar archive, run the tar command as follows:

As you can see, the contents of the tar archive and a lot of information about each files and directories are listed.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Extracting tar Archives:

To extract a tar archive, you have to know whether the archive is compressed or not. If the archive is compressed, then you have to know what compression algorithm is used to compress the archive as well.

Usually, you find this information from the archive filename. If the archive filename ends with .tar, then by convention this is a tar archive and is not compressed.

If the filename of the archive ends with .tar.gz, then it’s a gzip compressed archive.

If the filename of the archive ends with .tar.bzip2, then it’s a bzip2 compressed archive.

Still, people can use any file extension they want to represent the tar archive file. Nothing is stopping them. So, a better way is to use the file command.

To find information about an archive (let’s say project2.tar), run the file command as follows:

As you can see, even though the file extension is not correctly set, the file command still says it’s a gzip compressed archive.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Now, to extract the non-compressed tar archive project.tar you just created on your current working directory, run the following command:

This command will extract the archive in your current working directory.

If you want to extract the archive to some other directory, let’s say

/Downloads, then run the tar command as follows:

NOTE: The directory you’re extracting the archive must exist before you run the command. If it does not, tar will not be able to extract the archive. So, make sure the directory exists and if it does not, create the directory with the mkdir command.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The archive project.tar is extracted into the

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

As you can see, the contents of the archive are now available in the

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

If the archive is gzip compressed, then use the -z option when you extract the archive as follows.

If the archive is bzip2 compressed, then use the -j option when you extract the archive as follows.

Getting Help:

The tar command has a lot of options. It’s not possible to cover every one of them in this article. But, you can read the manpage of the tar command to learn more about it. I’ve shown you how to get started with the tar command in this article. Now, you should be able to move forward on your own.

To open the manpage of the tar command, run the following command:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

So, that’s how you use the tar command in Linux. Thanks for reading this article.

tar command in Linux with examples

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

Syntax:

What is an Archive file?
An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.

Output :

Output :

Output :

8. Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

9. Update existing tar file in Linux

Output :

Output :

11. Applying pipe to through ‘grep command’ to find what we are looking for : This command will list only for the mentioned text or image in grep from archived file.

12. We can pass a file name as an argument to search a tarfile : This command views the archived files along with their details.

Output :

What are wildcards in Linux
Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?),which represents a single character.

Example :

Note: In above commands ” * ” is used in place of file name to take all the files present in that particular directory.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
This article is contributed by Akansh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

tar Command in Linux With Examples

Home » Backup and Recovery » tar Command in Linux With Examples

The GNU tar (short for Tape ARchiver) command is the most widely used archiving utility in Linux systems. Available directly in the terminal, the tar command helps create, extract, and list archive contents.

The utility is simple and has many helpful options for compressing files, managing backups, or extracting a raw installation.

This tutorial shows how to use the tar command through examples and the available options.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Tar Command Syntax

The tar command general syntax is:

There are three possible syntax styles to use the operations and options:

1. Traditional style, clustered together without any dashes.

2. UNIX short option style, using a single dash and clustered options:

Alternatively, a dash before each option:

3. GNU long-option style with a double-dash and a descriptive option name:

All three styles can be used in a single tar command.

Tar Command Options

The following table outlines the commonly used tar operations and options.

CommandRoleDescription
—create
-c
OperationCreates a new archive.
—list
-t
OperationLists an archive’s contents.
—extract
-x
OperationExtract one or more items from an archive.
—append
-r
OperationAppends files to an existing archive.
—concatenate
-A
OperationAppends archives to an existing archive.
—compare
—diff
-d
OperationCompares archive members with files on the system.
—deleteOperationDeletes a member from the archive.
—update
-u
OperationUpdates archive with new files only if they are not in the archive and are newer than existing files.
—file=
-f
OptionSpecifies the file.
-COptionChanges the directory.
—verbose
-v
OptionShows the file tar works on while running.
—wildcardOptionRenders wildcard search options.
—bzip2
-j
OptionRead or write compressed archives through bzip2 format.
—gzip
-z
OptionRead or write compressed archives through gzip format.
—xz
-J
OptionRead or write compressed archives through xz format.

Follow the examples in the next section to learn how to work with tar.

Tar Command Examples

The examples below have the following requirements:

1. Create a directory named tar_examples and navigate to the directory:

2. Make another directory called files in tar_examples and enter that directory:

3. Create files to populate the files directory:

To return to the parent directory, use:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

All the examples below work from the tar_examples directory.

1. Create an Archive

The syntax for creating an archive depends on the archive type. To make an archive, use tar with the -c or —create operation.

Create a tar Archive

To make a tar archive (also called a tarball), use:

For example, archive the files directory:

The output lists each file added to the archive. Display the directory contents to see the created file.tar archive:

The output lists each file added to the archive. Display the directory contents to see the created file.tar archive:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Create a tar.gz Compressed Archive

Add the -z option to create a compressed GNUzip (gzip) file:

Note: Combine tar with find and xargs to find and archive files.

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The file size is smaller than a regular tarball file and the original directory.

Create tar.bz2 Compressed Archive

The bzip2 is a file compression program and an alternative to gzip.

To create a tar.bz2 file, add the -j tag:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The bzip2 has a greater compression rate and takes longer than gzip.

Create tar.xz Archive

Use the -J tag to compress archives in the tar.xz format:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The xz compression takes the longest when compared to both gzip and bz2. However, with larger files, xz has the highest compression rates.

2. Remove Files After Creation

To remove the files from disk after archiving, use the —remove-files option at the end:

For example, create a tar archive with the files directory and remove it from the disk in one command:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Check the directory contents to confirm the operation works correctly.

3. Extract from Archive

Extract From tar Archive

The general syntax for extracting from a tar archive is:

By default, tar extracts all the components to the current directory. To indicate where to extract the components, add the -C option and specify the path:

For example, to create a directory named extracted_tar and extract the files from files.tar, run:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The command doesn’t output a confirmation message. Check the directory contents to confirm the components extracted successfully.

Extract From tar.gz Archive

Use the -z option to extract a tar.gz file:

The command extracts the contents in the current directory. Add the -C option to specify the location:

For example, create a new directory named extracted_gz and extract the files.tar.gz contents:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The tar.gz compressed archives take the least time to extract when compared to other compression formats.

Extract From tar.bz2 Archive

To extract files from a tar.bz2 compressed archive into the current directory, use:

Extract tar.bz2 archives into a specific directory with:

For example, create a directory and extract the contents from files.tar.bz2:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Extract From tar.xz Archive

Add the -J option to extract from tar.xz compressed archives. The syntax to extract in the current directory is:

To extract the contents to a specific directory, use the -C option and add the path:

As an example, create a directory and extract the files.tar.xz contents:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The xz compression format is the middle ground between gz and bz2 when it comes to extraction time.

4. Overwrite Control

Tar overwrite controls handle situations where file names in the archive overlap with files in the working directory.

The three possible overwrite actions are:

1. Overwrite files in the working directory:

2. Don’t overwrite files in the working directory:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

If the files already exist, tar doesn’t perform the extraction.

3. Extract files only if they are newer than the existing files:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

If the files in the working directory are newer or the same age, tar does not extract the files.

5. List Archive Contents

Use the following command to list an archive’s contents:

The option works for any file extension containing tar.

For example, list the files and directories in the files.tar archive:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The output lists all contents stored in the archive.

6. Find a File in an Archive

There are two ways to locate specific content using tar:

1. The -t option to list files in an archive is handy for locating specific files. Add the file name (or names) after the command:

For example, to locate file50.txt in the files.tar.gz archive, run:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The option requires knowing the possible path to the file.

2. Use the tar together with the grep command to filter the output:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The option doesn’t require knowing the possible path to the file.

7. Find Multiple Files in an Archive

Use the —wildcards option to match multiple file instances. For example:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Apply wildcard matching when files have a similar name, or for filtering a certain file type.

8. Exclude Files when Creating Archive

To exclude certain files from the archive during creation, add the following option:

List the archive contents:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

9. Extract Single File from Archive

Avoid extracting the whole archive if you need one or several files.

To get a single file from an archive:

1. List the contents and check if the file exists:

The output prints the path to the file needed for the next step.

2. Extract the specific file with:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The command creates the directory files with only the indicated file. Follow the same steps to extract a single file from compressed archives by adding the appropriate tag. For example, use the -z option to pull from a tar.gz file.

10. Verbose Option

The verbose option displays additional information after running a tar command. Add -v or —verbose to any operation to see the result.

For example, create a tar.gz file and add -v :

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The output shows each file as it is added to the archive.

Some tar commands show additional information when you add the -v tag twice. For example, try to add files to an archive with -vv :

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

11. Delete from Archive

To delete from the archive, locate the file you want to remove, for example:

Then, remove the file using the —delete tag:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The delete option does not work on compressed file formats.

12. Append Files to Archive

Append files to an existing archive using the -r tag. The syntax is:

For example, append the compressed files.tar.gz file to the files.tar archive:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Already compressed archives cannot be updated, so the syntax only works for tarball files.

13. Combine Archives

Use the —concatenate or -A option to combine multiple archives. The basic syntax is:

As an example, copy the existing files.tar file using the cp command:

Next, concatenate the two archives:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

To confirm the concatenation worked, check the file size.

14. Difference Between Archive and Files

To check the difference between an archive and files on disk, use the -d tag:

The command searches for the same contents and compares them to what is in the archive. The option only checks for existing files and ignores any newly added files.

1. Create a tar archive:

2. Compare the archive with the existing directory:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The output does not display anything, meaning there is no difference between the existing files.

3. Add text to an existing file in the files directory:

4. Compare the archive to the existing directory again:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

This time, the output shows differences in the modification time and the size for a specific file. Comparing provides insight into any changes made on the system after creating the archive.

15. Update Files in Archive

Update the existing files in the archive with a newer version from disk with the -u option:

For example, update the files.tar archive with a changed text file:

Check the tar contents for the changed file:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The command updates the archive with changed files without any overwrites.

16. Modified Time

Tar offers various options to modify the file’s timestamp. Set a custom date when creating an archive by adding the —mtime option and providing a date:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Alternatively, extract the files with the current date and time:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

A useful feature when working with time is filtering files modified after a specific date. For example, to extract files created after a date, use the —newer-mtime option and add the date:

17. Permissions

There are two possible ways to control file permissions with tar when extracting an archive:

1. Preserve original permissions:

The permissions are as stated in the file before archive creation.

2. Modify the permissions to the default umask value:

The files take on the default Linux permissions.

18. File Ownership

Tar allows file ownership configuration. For example, to set the file owner when creating an archive, add the —owner and —group options and provide values for each:

The owner value represents the UID (User ID) while the group value is the GID (Group ID). To find these values for a user, run:

For example, create an archive and set the ownership to root:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

Tar allows preserving the ownership when extracting from an archive. To do so, add the —same-owner option at the end:

19. Write to External Program

The —to-command option instructs tar to send each extracted file to the standard output for an external program. The basic syntax is:

For example, extract the files.tar contents and pipe the file names as directories:

Tar how to use. Смотреть фото Tar how to use. Смотреть картинку Tar how to use. Картинка про Tar how to use. Фото Tar how to use

The command creates directories named after each extracted file. For more information and the available Linux environment variables, visit the manual page.

20. Create Daily Backups

To automate daily backups, create a bash script and add the following lines:

The tar command creates a compressed archive, while the find command seeks backup files older than one day. Change the +1 parameter to +7 for weekly or +31 for monthly backups.

After reading this article, you know how to use the tar command. However, there are various other options available that are not in this tutorial. Use the man command to find all the details about tar options.

Tar Command in Linux: Compress and Extract Files

I have been asked to write an article that explains the Linux tar command in simple terms.

And I totally understand why…

The tar command is definitely not one of the easiest Linux commands to learn and all these flags can be quite confusing!

How does the Linux tar command work?

Linux distributions provide a tar binary that supports gzip compression without the help of external command. The same might not apply to other types of compression as we will see in this article.

Let’s start with three examples of tar commands to get familiar with the most common flags.

Table of Contents

Create an archive that contains two files

Here is a basic example of tar command, in this case we are not using compression:

This command creates an archive file called archive.tar that contains two files: testfile1 and testfile2.

Here is the meaning of the two flags:

-c (same as –create): create a new archive

-f: it allows to specify an archive file (in this case called archive.tar)

The file command confirms that archive.tar is an archive:

Another useful flag is the -v flag that provides a verbose output of the files processed during the execution of the tar command on Linux.

Weird, for some reason we get an error back…

The result is an archive called v as you can see from the ls output below:

The “No such file or directory” directory is caused by the fact that tar tries to create an archive called v that contains three files: archive.tar, testfile1 and testfile2.

But archive.tar doesn’t exist and hence the error.

This show how important is the order of flags for tar.

All good this time, the verbose flag show the names of the two files being added to the archive we are creating.

List all files in a tar archive verbosely

To list all the files in a tar archive without extracting its content we will introduce a fourth flag:

-t: list the contents of an archive

We can now put together three flags: -t, -v and -f to see the files in the archive we have previously created:

One of the things I noticed when I started using the tar command is that different people were running it in a slightly different way.

I will explain what I mean in the next section…

Shall I Use the Dash or Not with Tar?

I noticed that is some cases the dash before the flags was present but it wasn’t always the case.

So, let’s see if passing the dash or not makes any difference.

First of all, let’s try to run the same command without using the dash before the flags:

The output is the same, this mean the dash is not necessary.

Just to give you an idea, you can run the tar command in the following way and obtain the same output:

The last command is using the long-option style for flag provided to Linux commands.

You can see how it’s a lot easier to use the short version of the flag.

Extract all files from an archive

Let’s introduce an additional flag that allows to extract the content of a tar archive. It’s the -x flag.

To extract the content of the file we have created before we can use the following command:

As you can see we have used the -x flag to extract the content of the archive, the -v flag to do it verbosely and the -f flag to refer to the archive file specified after the flags (archive.tar).

NOTE: As mentioned before we are only typing the dash character once before all the flags. We could have specified the dash sign before each flag instead and the output would have been the same.

There is also a way to extract a single file from your archive.

In this scenario it doesn’t make much difference considering that there are only two files inside our archive. But it can make a huge difference if you have an archive that contains thousands of files and you only need one of them.

This is very common if you have a backup script that creates an archive of the log files for the last 30 days and you only want to see the content of the log file for a specific day.

To extract only testfile1 from archive.tar, you can use the following generic syntax:

And in our specific case:

Let’s see what changes if I create a tar archive that contains two directories:

Note: Notice that I have used the wildcard * to include in the archive any files or directories whose name starts with “dir”.

If I want to just extract testfile1 the command will be:

After the extraction the original directory structure is preserved, so I will end up with testfile1 inside dir1:

Reducing the size of a tar archive

Gzip and Bzip2 compression can be used to reduce the size of a tar archive.

To create a gzipped tar archive called archive.tar.gz with verbose output we will use the following command (also one of the most common commands used when creating tar archives):

And to extract its content we will use:

Now, let’s create an archive that uses bzip2 compression:

The error “bzip2: command not found” shows that the tar command is trying to use the bzip2 command for the compression but the command cannot be found on our Linux system.

The solution is to install bzip2. The procedure depends on the Linux distribution you are using, in my case it’s CentOS that uses yum as package manager.

Let’s install bzip2 using the following yum command:

I can confirm that the bzip2 binary exsts using the which command:

And now if I run the tar command with bzip2 compression again:

As you can see, Linux can distinguish between archives generated using the two different compression algorithms.

Conclusion

In this article you have learned the most common flags used with the tar command, how to create and extract a tar archive and how to create and extract a gzipped tar archive.

And you? What are you using the tar command for?

2 Tutorial Introduction to tar

2.1 Assumptions this Tutorial Makes

This chapter is paced to allow beginners to learn about tar slowly. At the same time, we will try to cover all the basic aspects of these three operations. In order to accomplish both of these tasks, we have made certain assumptions about your knowledge before reading this manual, and the hardware you will be using:

2.2 Stylistic Conventions

2.3 Basic tar Operations and Options

tar can take a wide variety of arguments which specify and define the actions it will have on the particular set of files or the archive. The main types of arguments to tar fall into one of two classes: operations, and options.

Some arguments fall into a class called operations; exactly one of these is both allowed and required for any instance of using tar ; you may not specify more than one. People sometimes speak of operating modes. You are in a particular operating mode when you have specified the operation which specifies it; there are eight operations in total, and thus there are eight operating modes.

The other arguments fall into the class known as options. You are not required to specify any options, and you are allowed to specify more than one at a time (depending on the way you are using tar at that time). Some options are used so frequently, and are so useful for helping you type commands more carefully that they are effectively “required”. We will discuss them in this chapter.

In the examples and in the text of this tutorial, we usually use the long forms of operations and options; but the “short” forms produce the same result and can make typing long tar commands easier. For example, instead of typing

For more information on option syntax, see Advanced GNU tar Operations. In discussions in the text, when we name an option by its long form, we also give the corresponding short option in parentheses.

The term, “option”, can be confusing at times, since “operations” are often lumped in with the actual, optional “options” in certain general class statements. For example, we just talked about “short and long forms of options and operations”. However, experienced tar users often refer to these by shorthand terms such as, “short and long options”. This term assumes that the “operations” are included, also. Context will help you determine which definition of “options” to use.

Similarly, the term “command” can be confusing, as it is often used in two different ways. People sometimes refer to tar “commands”. A tar command is the entire command line of user input which tells tar what to do — including the operation, options, and any arguments (file names, pipes, other commands, etc.). However, you will also sometimes hear the term “the tar command”. When the word “command” is used specifically like this, a person is usually referring to the tar operation, not the whole line. Again, use context to figure out which of the meanings the speaker intends.

2.4 The Three Most Frequently Used Operations

Here are the three most frequently used operations (both short and long forms), as well as a brief description of their meanings. The rest of this chapter will cover how to use these operations in detail. We will present the rest of the operations in the next chapter.

Create a new tar archive.

List the contents of an archive.

Extract one or more members from an archive.

2.5 Two Frequently Used Options

Specify the name of an archive file.

Show the files being worked on as tar is running.

For example, to create an archive in verbose mode:

Creating the same archive with the verbosity level 2 could give:

This works equally well using short or long forms of options. Using long forms, you would simply write out the mnemonic form of the option twice, like this:

Note that you must double the hyphens properly each time.

The full output consists of six fields:

Depending on the file type, the name can be followed by some additional information, described in the following table:

The file or archive member is a symbolic link and link-name is the name of file it links to.

‘ link to link-name ’

The file or archive member is a hard link and link-name is the name of file it links to.

The archive member is an old GNU format long link. You will normally not encounter this.

The archive member is an old GNU format long name. You will normally not encounter this.

The archive member is a GNU volume header (see section Tape Files).

Encountered only at the beginning of a multi-volume archive (see section Using Multiple Tapes). This archive member is a continuation from the previous volume. The number n gives the offset where the original file was split.

‘ unknown file type c ’

An archive member of unknown type. c is the type character from the archive header. If you encounter such a message, it means that either your archive contains proprietary member types GNU tar is not able to handle, or the archive is corrupted.

For example, here is an archive listing containing most of the special suffixes explained above:

2.6 How to Create Archives

To make this easier, in this section you will first create a directory containing three files. Then, we will show you how to create an archive (inside the new directory). Both the directory, and the archive are specifically for you to practice on. The rest of this chapter and the next chapter will show many examples using this directory and the files you will create: some of those files may be other directories and other archives.

The three files you will archive in this example are called ‘blues’, ‘folk’, and ‘jazz’. The archive is called ‘collection.tar’.

2.6.1 Preparing a Practice Directory for Examples

To follow along with this and future examples, create a new directory called ‘practice’ containing files called ‘blues’, ‘folk’ and ‘jazz’. The files can contain any information you like: ideally, they should contain information which relates to their names, and be of different lengths. Our examples assume that ‘practice’ is a subdirectory of your home directory.

Now cd to the directory named ‘practice’; ‘practice’ is now your working directory. (Please note: Although the full file name of this directory is ‘/ homedir /practice’, in our examples we will refer to this directory as ‘practice’; the homedir is presumed.)

2.6.2 Creating the Archive

To place the files ‘blues’, ‘folk’, and ‘jazz’ into an archive named ‘collection.tar’, use the following command:

The order of the arguments is not very important, when using long option forms, however you should always remember to use option as the first argument to tar. For example, the following is wrong:

The error message is produced because tar always treats its first argument as an option (or cluster of options), even if it does not start with dash. This is traditional or old option style, called so because all implementations of tar have used it since the very inception of the tar archiver in 1970s. This option style will be explained later (see section Old Option Style), for now just remember to always place option as the first argument.

That being said, you could issue the following command:

The order of the options becomes more important when you begin to use short forms. With short forms, if you type commands in the wrong order (even if you type them correctly in all other ways), you may end up with results you don’t expect. For this reason, it is a good idea to get into the habit of typing options in the order that makes inherent sense. See section Short Forms with ‘ create ’, for more information on this.

When you create an archive, you must specify which files you want placed in the archive. If you do not specify any archive members, GNU tar will complain.

If you now list the contents of the working directory ( ls ), you will find the archive file listed as well as the files you saw previously:

Creating the archive ‘ collection.tar ’ did not destroy the copies of the files in the directory.

Keep in mind that if you don’t indicate an operation, tar will not run and will prompt you for one. If you don’t name any files, tar will complain. You must have write access to the working directory, or else you will not be able to create an archive in that directory.

In the rest of the examples in this chapter, we will frequently use verbose mode so we can show actions or tar responses that you would otherwise not see, and which are important for you to understand.

2.6.4 Short Forms with ‘ create ’

As you can see, the system responds the same no matter whether you use long or short option forms.

One difference between using short and long option forms is that, although the exact placement of arguments following options is no more specific when using short forms, it is easier to become confused and make a mistake when using short forms. For example, suppose you attempted the above example in the following way:

The end result is that you may be quite confused about what happened, and possibly overwrite a file. To illustrate this further, we will show you how an example we showed previously would look using short forms.

is confusing as it is. It becomes even more so when using short forms:

For this reason, we recommend that you pay very careful attention to the order of options and placement of file and archive names, especially when using short option forms. Not having the option name written out mnemonically can affect how well you remember which option does what, and therefore where different names have to be placed.

2.6.5 Archiving Directories

To archive a directory, first move to its superior directory. If you have followed the previous instructions in this tutorial, you should type:

This will put you into the directory which contains ‘practice’, i.e., your home directory. Once in the superior directory, you can specify the subdirectory, ‘practice’, as a file name argument. To store ‘practice’ in the new archive file ‘music.tar’, type:

tar should output:

If you give tar a command such as

2.7 How to List Archives

The output of tar would then be:

Print member (as opposed to file) names when creating the archive.

With this option, both commands produce the same output:

Since tar preserves file names, those you wish to list must be specified as they appear in the archive (i.e., relative to the directory from which the archive was created). Continuing the example above:

the error message is produced because there is no member named ‘folk’, only one named ‘home/myself/folk’.

If you are not sure of the exact file name, use globbing patterns, for example:

See section Wildcards Patterns and Matching, for a detailed discussion of globbing patterns and related tar command line options.

Listing the Contents of a Stored Directory

For example, to find out about files in the directory ‘practice’, in the archive file ‘music.tar’, type:

When you use a directory name as a file name argument, tar acts on all the files (including sub-directories) in that directory.

2.8 How to Extract Members from an Archive

2.8.1 Extracting an Entire Archive

To extract an entire archive, specify the archive file name only, with no individual file names as arguments. For example,

2.8.2 Extracting Specific Files

First, make sure you are in the ‘practice’ directory, and list the files in the directory. Now, delete the file, ‘ blues ’, and list the files in the directory again.

You can now extract the member ‘blues’ from the archive file ‘collection.tar’ like this:

Remember that as with other operations, specifying the exact member name is important (See section Commands That Will Fail, for more examples).

2.8.3 Extracting Files that are Directories

Extracting directories which are members of an archive is similar to extracting other files. The main difference to be aware of is that if the extracted directory has the same name as any directory already in the working directory, then files in the extracted directory will be placed into the directory of the same name. Likewise, if there are files in the pre-existing directory with the same names as the members which you extract, the files from the extracted archive will replace the files already in the working directory (and possible subdirectories). This will happen regardless of whether or not the files in the working directory were more recent than those extracted (there exist, however, special options that alter this behavior see section Changing How tar Writes Files).

However, if a file was stored with a directory name as part of its file name, and that directory does not exist under the working directory when the file is extracted, tar will create the directory.

Because you created the directory with ‘practice’ as part of the file names of each of the files by archiving the ‘practice’ directory as ‘practice’, you must give ‘practice’ as part of the file names when you extract those files from the archive.

2.8.4 Extracting Archives from Untrusted Sources

Extracting files from archives can overwrite files that already exist. If you receive an archive from an untrusted source, you should make a new directory and extract into that directory, so that you don’t have to worry about the extraction overwriting one of your existing files. For example, if ‘untrusted.tar’ came from somewhere else on the Internet, and you don’t necessarily trust its contents, you can extract it as follows:

2.8.5 Commands That Will Fail

Here are some sample commands you might try which will not work, and why they won’t work.

If you try to use this command,

you will get the following response:

This is because these files were not originally in the parent directory ‘..’, where the archive is located; they were in the ‘practice’ directory, and their file names reflect this:

Likewise, if you try to use this command,

you would get a similar response. Members with those names are not in the archive. You must use the correct member names, or wildcards, in order to extract the files from the archive.

To extract the member named ‘practice/folk’, you must specify

Notice also, that as explained above, the ‘practice’ directory will be created, if it didn’t already exist. There are options that allow you to strip away a certain number of leading directory components (see section Modifying File and Member Names). For example,

will extract the file ‘folk’ into the current working directory.

2.9 Going Further Ahead in this Manual

(This message will disappear, once this node revised.)

This document was generated on March 24, 2021 using texi2html 5.0.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *