“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Hi, everyone, last time introduced Lnux commonly used zip compression and decompression command detailed explanation, found that we still pay more attention to the relevant knowledge, three days have 1000+ reading amount, thank you for your support and love, thank you for the platform recommendation. So for your needs, this time I’ll introduce another common use of tar for compression and decompression.

Portal from the previous article: Details of common Zip compression and decompression commands in Linux

The tar command

I met

The tar command is familiar to many developers, as it is often used when backing up files on Linux. In Linux, the tar command is short for tape archive. The tar command is used to back up files. Tar command is a tool used to create and restore backup files. It can be added to and unzip backup files. The tar command can be used to pack individual files or folders or files and folders in a directory into a single file and store them. If you need to extract the content, you can run the decompression command to restore all files.

Run the tar command to package the compressed file to.tar.gz. You can also run the tar command to decompress the. Tar. gz file.

parameter

The tar parameters Parameter Description
– A or – catenate Add a file to an existing backup file.
-b< block number > or –blocking-factor=< block number > Set the number of blocks for each record. Each block size is 12Bytes.
– B or – read – full – records Reset the block size when reading data.
– or c – create Create a new backup file.
-c < destination directory > or –directory=< destination directory > Switch to the specified directory.
-d or –diff or –compare Compare the differences between backup files and files on the file system.
-f< backup file > or –file=< backup file > Specify the backup file.
-f <Script file > or –info-script=<Script file > Each time the tape is replaced, the specified Script file is executed.
– g or – listed – incremental Handles massive backups in GNU format.
– G or incremental Handles large backups of older GNU formats.
H – or – dereference Instead of creating a symbolic link, copy the original file to which the link points.
– or – I ignore – zeros Ignore the 0 Byte block, or EOF, in the backup file.
– k or – keep – old – files When unpacking backup files, existing files are not overwritten.
-k < file > or –starting-file=< file > Restores from the specified file.
– l or — one – file – system The file or directory to be copied must be stored in the same file system as that on which the tar command is executed. Otherwise, the file or directory will not be copied.
-l < media capacity > or -tape-length=< media capacity > Set the storage capacity of each body, in 1024 Bytes.
The -m or – modification – time When restoring a file, the change time of the file is not changed.
– M or – multi – volume Use multi-volume mode when creating, restoring, or listing the contents of backup files.
-n < date format > or –newer=< date time > Only files updated from the specified date are saved to the backup file.
-o or –old-archive or –portability Use V7 format when writing data to backup files.
– or – O stdout Output files restored from backup files to the standard output device.
P or — the same permissions Restore the file with the original file permissions.
P or — absolute — names Use the absolute name of the file name. Do not remove the slash (/) before the file name.
The -r or append Add a file to the end of an existing backup file.
The -r or – block – number Lists the block number of each message in the backup file.
The -s or – same – order The sequence of restoring files is the same as that of storing backup files.
The -s or sparse If a file contains a large number of consecutive 0 bytes, it is saved as a sparse file.
– t or – the list Lists the contents of backup files.
-t < template > or –files-from=< template > Specifies a template file that contains one or more template styles that tar can unlock or create files that match the Settings.
-u or – update Only replace files that are newer than those in the backup file.
-u or – unlink – first Unzip the file Before restoring the file, unbind the file.
V or — verbose Display command execution process.
-v < volume name > or –label=< volume name > Creates a backup file with the specified volume name.
– w or — interactive When encountering a problem, ask the user first.
– W or verify After writing the backup file, verify that the file is correct.
-x or –extract or –get Restore files from backup files.
-x –exclude — from= exclude –exclude Specifies a template file that contains one or more template styles, allowing AR to exclude files that match the Settings.
-z or –gzip or –ungzip Process backup files through the gzip directive.
-z or –compress or –uncompress Process backup files using the COMPRESS directive.
-< Device number >< Storage density > Set the number of peripheral devices used for backup and store data density.
–after-date=< date date > This parameter has the same effect as specifying the “-n” parameter.
–atime-preserve Do not change the file access time.
–backup=< backup mode > or –backup Back up files before removing them.
–checkpoint Lists directory names when reading backup files.
–concatenate This parameter has the same effect as specifying the “-a” parameter.
–confirmation This parameter has the same effect as specifying the “-w” parameter.
–delete Deletes the specified file from the backup file.
–exclude=< template style > Exclude files that match the template style.
–group=< group name > Sets the group of a file added to a device file to the specified group.
–help Online help.
–ignore-failed-read Ignore data read errors without interrupting the execution of the program.
– new – volume – script = > < script file This parameter has the same effect as specifying the “-f” parameter.
–newer-mtime Save only the files that have changed.
–no-recursion No recursive processing, that is, all files and subdirectories in the specified directory are not processed.
–null Read file names from null devices.
–numeric-owner Replace the user name and group name with the user id and group id.
–owner=< user name > Sets the owner of the file added to the backup file to the specified user.
–posix POSIX format is used when writing data to backup files.
–preserve This parameter has the same effect as specifying the “-ps” parameter.
–preserve-order This parameter has the same effect as specifying the “-a” parameter.
–preserve-permissions This parameter has the same effect as specifying the “-p” parameter.
–record-size=< number of blocks > This parameter has the same effect as specifying the “-b” parameter.
–recursive-unlink Before restoring a directory, disconnect all files in the directory.
–remove-files Delete files after they are added to backup files.
–rsh-command=< execute command > Sets the instruction to be executed on the remote host in place of the RSH instruction.
–same-owner Try restoring the file with the same file owner.
–suffix=< backup suffix string > Back up files before removing them.
–totals After the backup file is created, list the file size.
— use-compressed-program =< execute command > Process backup files by specifying instructions.
–version The version information is displayed.
–volno-file=< number file > Replaces the preset volume number with the number in the specified file.

The sample

In order to facilitate understanding, a simple example will be introduced, of course, this time using the zip test file to demonstrate, just for you to learn to use. First enter the directory, the file contains: admin folder, index.html, test. HTML three files and folders.

Compress files

Zip the file index.html to index.tar.gz

tar -czvf index.tar.gz index.html
Copy the code

-czvf In the command, c creates a new backup file, z processes the backup file using the gzip command, v displays the execution process, and f specifies the backup file.

Of course, the execution process can also be omitted by removing v and executing the command as follows. It can be seen that the execution process is no longer displayed in the red part after comparison.

tar -czf test.tar.gz test.html
Copy the code

Display compressed file contents

When we encounter multiple backup files, how to display the details of the compressed file? Run the following command to view the permission, user, size, creation time, and name of the backup file.

tar -tzvf test.tar.gz
Copy the code

Unzip the files

We already have an index.tar.gz file, we unzip it and execute the following command:

tar -xzvf index.tar.gz
Copy the code

As shown in the picture below, we first delete the original file, and then execute the command to decompress it. We can see that the file has been decompressed out. This will restore our backup files.

See the help

If you forget the tar command information during command execution, you can directly invoke the help information of the tar command to provide the meaning and usage of the command. Note the use of two –. Run the following command:

tar --help
Copy the code

Backup folder

There is an admin folder that needs to be backed up as admin.tar.gz. The following commands need to be executed:

tar -czvf admin.tar.gz admin/
Copy the code

conclusion

Well, the above is Linux commonly used tar compression and decompression command details, more command examples in continuous improvement, welcome supplementary guidance, thank you for reading, I hope you like, if you have help, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.