Linux 打包命令 tar 详解

tar 是 Linux 中常用的一种存档格式,它可以将多个文件或目录压缩成单个文件进行分发,还可以创建和解压 tar 存档。本文将讲解如何使用 tar 命令在 Linux 中创建、解压和管理 tar 存档,以及一些常见的 tar 命令选项。

1. tar 命令基础语法

tar 命令基础语法:

tar [选项] [文件名]

选项是可选的,可以用来指定 tar 命令的操作和设置。如果没有选项,则 tar 命令会以默认方式执行。文件名是要处理的文件或目录的名称。

下面是一些最常用的 tar 命令选项:

  • -c:创建新的存档文件(Create)。
  • -x:从存档文件中提取文件(eXtract)。
  • -t:列出存档文件中的内容(lisT)。
  • -v:显示 tar 命令执行的详细信息(Verbose)。
  • -f:指定存档文件的名称(File)。
  • -z:在创建或提取存档文件时使用 gzip 压缩算法来进行压缩或解压缩(gzip)。
  • -j:在创建或提取存档文件时使用 bzip2 压缩算法来进行压缩或解压缩(bzip2)。
  • -C:指定 tar 命令的工作目录(Change directory)。

2. tar 命令的常用操作

2.1 创建 tar 存档文件

使用 tar 命令创建存档文件,格式如下:

tar -cvf archive_name.tar file1 file2 file3 ...

例如,要将/etc目录中的所有文件和子目录打包成一个名为etc_backup.tar的 tar 存档文件,可以执行以下命令:

tar -cvf etc_backup.tar /etc

上述命令会创建一个名为etc_backup.tar的 tar 存档文件,并将/etc目录中的所有文件和子目录添加到该文件中。如果您想查看 tar 命令执行的详细信息,请将 -v选项添加到命令行中。

tar -cvvf etc_backup.tar /etc

在创建 tar 存档文件时,还可以同时压缩存档文件。例如,要使用 gzip 压缩新创建的存档文件,可以执行以下命令:

tar -czvf etc_backup.tar.gz /etc

The above command creates a etc_backup.tar.gzgzipped tar archive named and /etcadds all the files and subdirectories in the directory to it.

Similarly, if you want to create a tar archive using the bzip2 compression algorithm, you can use the following command:

tar -cjvf etc_backup.tar.bz2 /etc

The above command will create a etc_backup.tar.bz2bzip2-compressed tar archive named .py and /etcadd all the files and subdirectories in the directory to the file.

Sometimes it is also necessary to compress tar archives into xz format. In Linux systems, the xz-utils package is usually used to provide related functions. To create a tar archive using the xz compression algorithm, the following command can be used:

tar -cJvf etc_backup.tar.xz /etc

Note that the xz compression algorithm has a higher compression ratio than the gzip and bzip2 compression algorithms, but it needs to consume more CPU and memory resources during compression and decompression.

2.2 Extract the tar archive

Use the tar command to extract the archive file in the following format:

tar -xvf archive_name.tar

For example, to extract files from a previously created etc_backup.tararchive, the following command can be executed:

tar -xvf etc_backup.tar

The above command will extract the archive file to the current directory. If you want to extract the archive to a different directory, you can -Cspecify the target directory with the option:

tar -xvf etc_backup.tar -C /tmp

The above command will extract the archive file into /tmpthe directory.

If the archive is gzip, bzip2 or xz compressed, the corresponding compression algorithm must be specified to decompress the archive, for example:

tar -xzvf etc_backup.tar.gz
tar -xjvf etc_backup.tar.bz2
tar -xJvf etc_backup.tar.xz

2.3 List the contents of the archive file

To see a list of the contents of a tar archive, use the following command:

tar -tvf archive_name.tar

For example, to view a list of the contents of a previously created etc_backup.tarfile, execute the following command:

tar -tvf etc_backup.tar

The above command will display a list of all files and directories contained in the archive.

Similar to when uncompressing a tar archive, if the archive is gzip, bzip2 or xz compressed, the corresponding compression algorithm must be specified to list the contents of the archive:

tar -tzvf etc_backup.tar.gz
tar -tjvf etc_backup.tar.bz2
tar -tJvf etc_backup.tar.xz

3. Advanced usage of tar command

3.1 Using wildcards to compress multiple files

Occasionally, you may need to compress multiple files matching certain criteria into a single tar archive. In this case, wildcards can be used to specify the files to compress, for example:

tar -cvf my_files.tar /path/to/files/*

The above command will compress /path/to/filesall files and subdirectories under the directory and save the archive file as my_files.tar.

3.2 Exclude specified files or directories

Sometimes, you may need to exclude certain files or directories when creating a tar archive. For example, you might not want to include files of a specific type, a certain directory, or a certain file in your archive. In this case, --excludethe option can be used to exclude files or directories.

For example, to exclude all files ending with when creating an archive .mp3, execute the following command:

tar -czvf backup.tar.gz /path/to/files --exclude '*.mp3'

The above command will create a backup.tar.gzcompressed tar archive named and exclude all .mp3files ending with . You can also use the multiple --excludeoptions to specify multiple files or directories to exclude.

3.3 Send archive files directly to remote server

After an archive file is created using the tar command, it can be sent to a remote computer to back up the data. On Linux systems, sshthe archive file can be sent to a remote computer using the command.

For example, assuming you have created backup.tar.gzan archive named and you want to send it to the directory 192.168.1.100on a remote computer with IP address , you can execute the following command:/backup

ssh [email protected] 'mkdir -p /backup' && \
cat backup.tar.gz | ssh [email protected] 'cd /backup && tar -xz'

The above command will create a backup.tar.gztar archive named and send it to the remote computer using the pipe character. On the remote computer, first /backupcreate a new directory in the directory (if it does not exist), then run tarthe command to extract the archive.

3.4 Splitting the archive into parts

Sometimes archive files can be large and difficult to store or transfer in a single file. In this case, --multi-volumethe archive file can be split into multiple parts using the option.

For example, to split an archive.tar.gzarchive named into multiple files of 10MB each, execute the following command:

tar -czvf - /path/to/archive | split -b 10m -d - archive.tar.gz.

The above command will pipe the compressed archive into splitthe command and split it into multiple 10MB files, each with a filename beginning with archive.tar.gz.and ending with a number (for example, archive.tar.gz. 00, archive.tar.gz.01, etc.).

3.5 Merging multiple archive files into a single file

Sometimes, it may be necessary to combine multiple existing archive files into a single archive file for easier management and transfer. In this case, --concatenatemultiple archive files can be combined into a single file using the option.

For example, to merge two archives named archive1.tar.gzand into one named archive, the following command can be executed:archive2.tar.gzcombined_archive.tar.gz

cat archive1.tar.gz archive2.tar.gz > combined_archive.tar.gz

The above command will catconcatenate the contents of the two archive files using the command and output the result to combined_archive.tar.gza file.

3.6 Create an incremental backup

When backing up data, it is often desirable to create the backup file using the least amount of time and resources. In this case, --incrementalan incremental backup can be created using the option, which only backs up the changes since the last backup.

/backupFor example, to create an incremental backup, one would first create a new snapshot in the specified directory (for example ):

tar -czvf /backup/snapshot.tar.gz /path/to/files

Then, on the next backup, you can use --listed-incrementaloptions to specify incremental backup files (for example /backup/incremental_backup.snar) so that the tar command only backs up the changes since the last backup. For example:

tar -czvf backup.tar.gz --listed-incremental=/backup/incremental_backup.snar /path/to/files

The above command will zip all files and subdirectories in a directory using wildcards provided by the shell /path/to/files, with --listed-incrementaloptions specifying the name of the incremental backup file. If you need to store differences between multiple backups, you can use --incrementalthe option to create incremental backups.

Guess you like

Origin blog.csdn.net/u012581020/article/details/131288313