In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to create and manage archived documents in Linux". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In short, an archive is a single file that contains a series of files and / or directories. Archived files are usually used for transfer locally or on the Internet, or as a backup copy of a series of files and directories, allowing you to work with one file (if compressed, its size will be less than the sum of all files). Similarly, archiving is also used for software application packaging. To facilitate transfer, this single file can be easily compressed, while the files in the archive retain the original structure and permissions.
We can use the tar tool to create, list, and extract files in the archive. Archives generated with tar are often referred to as "tar files", "tar archives", or "archives" (because all archived files are combined into one file).
This tutorial shows how to create, list, and extract content from an archive using tar. All three operations use two common options-f and-v: use-f to specify the name of the archive file, and use the-v ("redundant") option to make tar output the file name when processing the file. Although the-v option is not required, it allows you to observe the process of tar operation.
In the following parts of this tutorial, you will cover three topics: 1, create an archive file; 2, list the contents of the archive file; 3, extract the contents of the archive file. In addition, we will answer 6 practical questions about archive management to end this tutorial. What you learn from this tutorial is critical to performing tasks related to network security and cloud technology.
1. Create an archive file
To create an archive file using tar, use the-c (create) option, and then use the-f option to specify the name of the archive file to be created. It is common practice to use a name with a .tar extension, such as my-backup.tar. Note that unless otherwise noted, all commands and parameters used in the rest of this article are used in lowercase. Remember, when you enter commands for this article on your terminal, you don't need to enter the $prompt at the beginning of each command line.
Enter the file name to archive as the parameter, or if you want to create an archive file that contains all files and their subdirectories, provide the directory name as the parameter.
To archive the contents of the project directory, enter:
$tar-cvf project.tar project
This command will create an archive named project.tar that contains all the contents of the project directory, while the original directory project will remain unchanged.
The-z option allows you to compress the archive file so that the output is the same as creating an uncompressed archive and then compressing it with gzip, but it saves extra steps.
To create a project.tar.gz package from the project directory, type:
$tar-zcvf project.tar.gz project
This command will create a project.tar.gz package that contains all the contents of the project directory, while the original directory project will remain unchanged.
Note: when using the-z option, you should use the .tar.gz extension instead of the .tar extension to indicate that it is compressed. Although it is not necessary, it is a good practice.
Gzip is not the only form of compression, but also bzip2 and xz. When we see a file with a .xz extension, we know that it is compressed using xz and that a file with a .bz2 extension is compressed with bzip2. As bzip2 is no longer maintained, we will move away from it and focus on xz. When using xz compression, it takes longer. However, it is usually worth the wait because the compression is much better, which means that the compression package is usually smaller than using other forms of compression. Even better, there is not much difference in extracting or extracting files between different forms of compression. Below we will see an example of how to use xz when using tar to compress files:
$tar-Jcvf project.tar.xz project
We just need to convert the-z option of gzip to the uppercase-J of xz. Here are some outputs that show the differences between the forms of compression:
As you can see, xz has the longest compression time. However, it does the best job of reducing file size, so it's worth the wait. The larger the file, the better the compression.
2. List the contents of the archived file
To list but not extract the contents of the tar archive, use the-t option.
To list the contents of the project.tar, enter:
$tar-tvf project.tar
This command lists the contents of the project.tar archive. Using the-v and-t options together outputs the permissions and modification time of each file, as well as the file name. This is the same format that the ls command uses when using the-l option.
To list the contents of the project.tar.gz package, enter:
$tar-tzvf project.tar.gz3, extract content from the archive
To extract (extract) the contents of the tar archive, use the-x (extract) option.
To extract the contents of the project.tar archive, type:
$tar-xvf project.tar
This command extracts the contents of the project.tar archive to the current directory.
If an archive file is compressed, usually with a .tar.gz or .tgz extension, include the "- z" option.
To extract the contents of the project.tar.gz package, type:
$tar-zxvf project.tar.gz
Note: if there are files or subdirectories in the current directory with the same name as the contents of the archive file, those files or subdirectories will be overwritten when the archive file is extracted. If you don't know which files are included in the archive, please consider looking at the contents of the archive first.
Another reason to list archived content before extracting it is to determine whether the content in the archive is included in the directory. If not, and the current directory contains many unrelated files, you may confuse them with the files extracted in the archive.
To extract the files into their own directory, create a new directory, move the archive files to that directory, and then you can extract the files in the new directory.
FAQ
Now that we have learned how to create an archive and list and extract its contents, we can move on to the nine practical questions that Linux professionals are often asked.
Can I add content to the package without unzipping it?
Unfortunately, once the file will be compressed, you cannot add content to it. You need to extract or extract the content, then edit or add the content, and finally compress the file again. If the file is small, this process will not take long, otherwise please wait a while.
Can I delete the contents of the archive without unzipping it?
This depends on the version of tar used when compressing. Newer versions of tar support the-delete option.
For example, suppose you have file1 and file2 in the archive, and you can remove them from file.tar using the following command:
$tar-vf file.tar-delete file1 file2
Delete the directory dir1:
What's the difference between $tar-f file.tar-delete dir1/* compression and archiving?
The easiest way to see the difference between archiving and compression is to look at its unzipped size. When archiving files, multiple files are merged into one. So, if we archive 10 100kb files, we end up with a file the size of 100kb. If you compress these files, you may end up with a file that is only a few kb or close to 100kb.
How do I compress an archive file?
As mentioned above, you can use the tar command with the cvf option to create and archive files. To compress an archive file, you have two options: run the archive file through a compression program, such as gzip, or use the compression option when using the tar command. The most common compression flag-z for gzip,-j for bzip,-J for xz. For example:
$gzip file.tar
Alternatively, we can use the compression flag when using the tar command, and the following command uses the gzip flag z:
How does $tar-cvzf file.tar / some/directory create archives of multiple directories and / or files at a time?
It is not uncommon to file more than one file at a time. Archiving multiple files and directories at a time is not as difficult as you think. You only need to provide multiple files or directories as parameters of tar:
$tar-cvzf file.tar file1 file2 file3
Or
$tar-cvzf file.tar / some/directory1 / some/directory2 how do I skip directories and / or files when creating an archive?
You may encounter a situation where you want to archive a directory or file, but not all files, in which case you can use the-- exclude option:
$tar-exclude'/ some/directory'-cvf file.tar / home/user
In the example, everything in the / home/user directory will be archived except / some/directory. It is important to put the-- exclude option before the source and destination, and enclose the files or directories to be excluded in single quotes.
Summary
The tar command is useful for showing unwanted files to create backups or compressed files. It is a good practice to back up files before changing them. If something doesn't work as expected after the change, you can always revert to the old file. Compressing files that are no longer in use helps keep the system clean and reduces disk space usage. There are other utilities that can be archived or compressed, but tar leads the way because of its versatility, ease of use, and popularity.
That's all for "how to create and manage Archives in Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.