Home  >  Article  >  Operation and Maintenance  >  What does linux archive mean?

What does linux archive mean?

藏色散人
藏色散人Original
2023-03-02 10:37:302055browse

Linux archiving is a process of aggregating many files and combining them into one large file. It is usually used as part of a system backup and is often used to move old data from a system to some long-term storage. In the case of devices; the tar command can create archives for Linux files and directories.

What does linux archive mean?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What does linux archive mean?

Archiving is a common file management task used in conjunction with compression operations. Archiving is the process of aggregating many files and combining them into one large file, usually as part of a system backup, and is often used in situations where old data is moved from a system to some long-term storage device.

The tar command can create archives for Linux files and directories. Using tar, you can create an archive (backup file) for a specific file, change files in the archive, or add new files to the archive. tar was originally used to create archives on tapes, but now users can create archives on any device. Using the tar command, you can package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission.

Linux tar command

The tar command is used to package multiple files into one file package on Linux. Convenient for transfer and can also be used for file backup. We can also use tar to decompress archive files.

tar command compression format

The compression format supported by the tar command. Generally, when we use the tar command, we will compress the file at the same time to reduce space usage. Usually we use the following naming convention to agree on which compression method we use.

.tar Default mode, no compression, just put all files into one package.

.tar.gz Common mode, compressed using gzip algorithm. The compression rate is average and the compression time is medium.

.tar.bz2 is not commonly used. Compressed using bz2 algorithm. The compression ratio is slightly worse and the compression time is shorter.

.tar.xz is not commonly used and is compressed using the xz algorithm. The compression rate is better and the compression time is longer.

tar overview

tar syntax introduction

tar generally supports the following two syntax formats, the second is the old The way.

tar OPTION... [NAME]...
tar LETTER... [ARGUMENT]... [OPTION]... [NAME]...

The main parameters of tar are called [operation types]: for example, c creates, x decompresses. We call other parameters [additional options], which are used to change the progress of our operations, such as file names and target files to be archived.

Main parameters [Operation type] must be placed at the front.

[NAME] in both syntax formats specifies the name of the file we want to archive. [NAME] can be a file or directory, or multiple files or directories. [NAME] must be specified clearly, and it must already exist on the file system before we execute the tar command.

Other knowledge about tar

tar "interprets the file name of the relative path as a relative directory. If we specify an absolute path, tar will remove the first / To package. The purpose of this is to prevent the system directory from being replaced during decompression. Unless we deliberately specify the [--absolute-names] option.

If we specify a directory, tar will recurse Specify all subdirectories and files in the directory for archiving. For example, if we specify /, it means that the entire file system will be archived.

It is very important to distinguish the file name and the members to be archived when using shell wildcards The reason is that the shell will only use wildcards for existing files, and tar itself will also use wildcards for archive members, so we must ensure that the wildcards only work for tar, not the shell. Usually we can use backslash\ in "*" or "?" or put it in single quotes, that's fine.

Usually we put the detachment's files on the command line. In fact, we can also specify the archive by reading the file. The file name after the archive.'--files-from=FILE-OF-NAMES' ('-T FILE-OF-NAMES')

If we do not specify the file name after the archive, then append, delete, The other operation commands will not do anything. create will output a diagnostic message and prevent tar operations. Other operation options will operate on the entire archive directory

In addition to normal and successful exit, tar will fail for many reasons. There are The situation can be recovered. For example, when the tar command is not written normally, the error will be postponed until the file is processed and reported. Some errors may not be intentional, but they are still dangerous. Then, tar will immediately Exit.

However, all abnormal exits, whether immediate or delayed, will have error output and diagnostic information.

Possible tar program exit codes are: 0( Normal exit), 1 (a file has changed), 2 (serious error, unrecoverable error)

tar option style

Three options style

The standard tar command supports a total of 8 operating modes for us to complete a variety of tasks. We can only choose one operating mode each time we use the tar command. Depending on the actual situation, we may want to specify the tar operation mode. For example, we may modify the output format of outputs. Or if we want a file format that requires us to specifically specify the order of operations.

The tar option is an option appended after we specify the tar operation mode. Depending on tar's operating mode, we can specify one or more options. Different options have different functions. Usually, the options will modify the specific content of the operation mode. For example, modify the archive format, archive file name, or user interaction level.

Some options are valid for all operating modes, and some options are only valid for specific operating modes. There are some fixed options that we use frequently. Others may be rarely used or not used at all.

Options are case-sensitive. For example, -T and -t are different. T requires a parameter name to specify the file name or file list. t does not require parameters and is another way of writing --list.

The tar option usually supports 3 different styles: long naming style (--), short naming style (-), and old naming style (without -). Either style can be used for operating modes or options. The different styles in the 3 were developed at different times.

Some options require a parameter. Where to place the parameter usually depends on which style we use. These differences may be subtle, but they are important. Options placed in the wrong location may result in overwriting some important files. We must note these differences. And only use the options we understand until we understand the other options as well. For options that take arguments, they may have a longest or short form without an old-style counterpart. The rules for using options that require specified arguments are very strict.

For example, the --file option needs to receive an archive file name as a parameter. If we do not specify an archive file name, tar will use a default but confusing name. It is recommended that we must specify a specific archive file name.

Long named option style

All parameter options of tar have at least one long named parameter. Long named parameter options are displayed more clearly than short named and old parameter name types. Sometimes long-name options have different names but have the same effect. For example, --compare and --dif have the same meaning. In addition, long naming methods can also use unique abbreviations. For example, --create can be replaced by --cre. Because there are no other options starting with --cre.

tar Long naming options are more obvious and easier to remember, and their names are easily identifiable. For example, tar --create --verbose --blocking-factor=20 --file=/dev/rmt0, all use long named parameters, and the meaning of each parameter can be clearly seen.

If a long-named option must receive parameters, it must be written immediately after the option. There are two ways to specify the necessary parameters, one is to use spaces, and the other is to use the = symbol. For example, with the --file option, we can use --file archive.tar or --file=archive.tar to specify parameters.

Correspondingly, if the option can receive parameters or not. In this case, if you want to receive parameters, you must use the = symbol to specify them. For example, the --backup optional argument is received. If you want to specify it, you must use '--backup=BACKUP-TYPE'.

Short naming option style

Most options also have corresponding short naming options. Short naming options use the - symbol followed by a letter. For example -t (corresponding to the long named option --list), these forms are functionally identical and can be interchanged.

Compared with the long naming option, an obvious advantage of the short naming method is that it is easy to write.

Short named options are usually implemented with spaces if they need to receive parameters immediately after the option. You can also omit the spaces and write the options immediately. For example, to specify the archive name, we can use -f archive.tar (with spaces) -farchive.tar (with spaces omitted), --file=archive.tar. -f ARCHIVE-NAME and - -file=ARCHIVE-NAME indicates that this option specifies the archive file name.

Short named options with optional parameters. If you want to specify parameters, the parameters need to be followed by the short option name, and no spaces can be used.

Short named options can be written together, but are not required. When short options appear in a collection, only a - is used at the beginning of the option, such as tar -cvf, and only the last option is allowed to receive required parameters. (Gathering many options together, with the last option having a parameter is a rather opaque way of writing it.)

When options are used separately, the parameters for each option need to follow the option. tar -c -v -b 20 -f /dev/rmt0. If the parameters are not adjusted properly, the file may be overwritten.

When we re-adjust an option, we should pay attention to moving the parameters belonging to that option at the same time.

Old option style

像短命名选项一样,旧选项风格也只有一个字母。但是,旧风格选项必须将所有选项放在一起,不得有空格将他们分开,或者-连接他们。在tar命令和空格之后,比较紧跟这些字母选项的集合。旧选项不能在任意地方出现。旧选项风格的选项和短命名风格的选项含义是一样的。例如 在旧选项风格中,选项 t 和 短命名选项-t 已经长命名选项--list有一样的作用。 tar cv 命令中,选项v是c选项的额外附加内容。

在旧选项风格中,但选项需要参数的时候,这些选项要一起提供,所有的选项按选项书写顺序提供。例如  tar -c -v -b 20 -f /dev/rmt0  要写成 tar cvbf 20 /dev/rmt0这里20是b选项的参数 /dev/rmnt0是f的参数。

我们可以看出,旧风格选项,匹配选项字母和对应的参数是很困难的,而且很容易令人困惑。短命名方式逗比旧命名方式要好很多。如果要调整选项的字母,必须要同时准确地调整对应的参数位置。

旧选项风格有的时候甚至可以震惊一个熟悉的用户。

tar cfz archive.tar.gz file  这个例子中 archive.tar.gz 被认为是f的参数,z是压缩选项。

tar -cfz archive.tar.gz file 这个例子中 z会被认为是f的选项值,尽管不是我们想的。

旧选项风格是为了向下兼容而保留的。而且很多用户已经习惯了这种方式。

第二个例子我们有几种方式来修改。

tar -czf archive.tar.gz file
tar -cf archive.tar.gz -z file
tar cf archive.tar.gz -z file

通常tar命令的第一个选项一般都会当做包含操作模式和选项来对待处理,尽管没有用-开始。因此 tar c与tar -c一样,都是和--create一样来创建归档。

混合选项风格

三种选项风格可以同时出现在一条tar 命令中,但是旧选项必须在tar命令后 第一个出现,现代风格的选项必须在旧选项集合输入之后出现,这个是必须遵守的原则,否则可能我们输入的命令跟我们预想的结果根本不一样。可能会造成严重的错误或灾难。

 tar选项

操作模式

'--concatenate'  '--catenate' '-A' 将其他归档文件放在当前归档文件末尾

'--append' ' -r'  追加文件到归档文件

'--compare'  '--diff'  '-d'  将归档文件和文件系统上的文件进行对比

'--create' '-c' 创建一个归档文件

'--delete' 从归档文件中删除一些文件。(不能在tape上操作)

'--extract' '--get' '-x' 提取归档文件到当前文件系统

'--list'  '-t' 显示归档文件的组成文件列表

'--update'  '-u' 更新归档文件中的某个文件,只有在该文件有更新时。或者不存在与归档文件。

 

通用选项

-C, --directory=DIR  改变工作目录。

-f, --file=ARCHIVE  指定归档文件名

-j, --bzip2  使用bzip2压缩方式过滤

-J, --xz  使用xz压缩方式过滤

-p, --preserve-permissions 解压时保留文件的权限。

-v, --verbose 详细的列出处理的文件

-z, --gzip  使用gzip压缩方式过滤

提取时覆盖选项

--remove-files 提取归档后删除归档文件

-k --keep-old-files 执行的时候不覆盖已经存在的文件,会输出错误

--keep-newer-files 执行的时候不覆盖比archive里文件新的文件

--keep-directory-symlink 不把已经存在的连接替换成目录

--no-overwrite-dir 保存已经存在的目录的原信息

--overwrite  覆盖已经存在的文件

--overwrite-dir 覆盖已经存在的目录

--recursive-unlink 在提取目录文件前清除该目录结构

--skip-old-files 不替换已经存在的文件。静默方式跳过

-U, --unlink-first 在提取之前先删除所有文件

-W, --verify 校验

 

处理文件的属性

--atime-preserve[=METHOD]  保存文件atime到归档文件

--delay-directory-restore 直到解压完才改变目录的时间戳属性

--group=NAME 强制修改文件所属组

--mode=CHANGES 强制连接模式修改

--mtime=DATE-OR-FILE 修改文件的mtime

-m, --touch 不提取文件的 mtime

--no-delay-directory-restore 取消delay-directory-restore

--no-same-owner  extract files as yourself (default for ordinary users)

--no-same-permissions  apply the user's umask when extracting permissions from the archive (default for ordinary users)

--numeric-owner 使用number来表示owner和group

--owner=NAME  修改归档的文件的owner

-p, --preserve-permissions, --same-permissions Preserve file attributes

--preserve Preserve file attributes same as both -p and -s

-- same-owner Preserves file attributes

-s, --preserve-order, --same-order Preserves file attributes

Local directory file Select

--add-file=FILE to add the file to the archive. If a file name starts with -

--backup[=CONTROL] Backup before deleting

-C, --directory=DIR Modify the working directory to the specified directory

--exclude=PATTERN Exclude some files

--exclude-backups Exclude backup and lock files

--exclude-caches Exclude other files in the cache (CACHEDIR.TAG) directory, except tagfile

--exclude-caches-all Exclude the directory including CACHEDIR.TAG

--exclude-caches-under excludes all other files in the directory with tagfile, but there is a directory

--exclude-tag=FILE excludes the directory with a specified file, except this file

exclude contents of directories containing FILE, except for FILE itself

--exclude-tag-all=FILE exclude directories containing FILE exclude the entire directory containing a file

--exclude -tag-under=FILE excludes all contents in the directory containing a certain file

--exclude-vcs excludes the directory under vcs version control

-h, --dereference saves the soft link , archive the target files as well

follow symlinks; archive and dump the files they point to

--hard-dereference Save the hard and soft links, archive the target files as well

-K, --starting-file=MEMBER-NAME Start archiving with a file in the directory, and the previous files will not be archived

--newer-mtime=DATE When the content is modified, compare the date of the file and time

-P, --absolute-names Pack with absolute paths. The source file will be overwritten when extracting --recursion

--suffix=STRING. Back up before deletion. Use the set prefix to overwrite

-X, --exclude-from=FILE to exclude the specified The agreed files in the file

Recommended study: "Linux Video Tutorial"

The above is the detailed content of What does linux archive mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn