LinuxShell script strategy-Chapter 6 Plan B

Source: Internet
Author: User
Tags crypt
61. you can use the tar archive 1tar command to archive files. you can use tar to save multiple files and folders as a single file, and retain all file attributes, such as owner and permissions, the file created by this command is usually called tarball2. follow the following method:

6.1 Archive with tar

The 1 tar command can be used to archive files. you can use tar to save multiple files and folders as a single file, and retain all file attributes, such as owner and permissions, the file created by this command is usually called the tarball

2. use tar to archive files as follows: tar-cf output.tar sources

For example, tar-cf output.tar file1 file2 file3 ....

-C in the command indicates creating a file, and-f indicates a specified file name.

3. run the following command to add a file to the archive: tar-rvf output.tar new_file.

4. use the following method to list the archive content: tar-tf output.tar

5. if you need to learn more details when archiving or listing the archived content, you can use the-v or-vv options to allow more detailed information to be output on the terminal.

6. the following command extracts the content of the archive file to the current directory: tar-xf output.tar

-X indicates that the content in the current archive file is extracted to the current directory. you can use the-C option to specify the directory to be extracted.

Tar-xf output.tar-C path

7. connect two archive files: tar-Af file1.tar file2.tar // The-A option is used to merge the two archive files.

8. delete a file from the archive file: tar-f output.tar -- delete file1 file2 // command to delete file1 and file2 from the archive file

9 exclude some files from the archive

For example, remove all. txt files: tar-cf output.tar * -- exclude "*. txt"

You can also put the list of files to be excluded into the file: tar-cf output.tar *-X file

6.2 Archive with cpio

1 cpio is another archive format similar to tar. it is used to store multiple files and folders as a single file and keep all file attributes, such as permissions and file ownership.

2 cpio obtains the input file name through stdin and writes the archive file to stdout. we must redirect stdout to a file to receive the output of cpio.

Create a test File: touch file1 file2 file3

Archive the test file as follows: echo file1 file2 file3 | cpio-ov> output.tar

3. to list the content in the cpio archive file, run the following command: cpio-it <output.tar // to list all the content in the given cpio archive file.

-I is used to specify input-t to list the content in the archive file.

6.3 compressed with gunzip or gzip

1 gzip is a common compression format on the GNU/Linux platform. gzip can only compress a single file, but cannot Archive directories and multiple files. Therefore, we generally submit this task to tar, and then use gzip to compress

2. to use gzip to compress the file, run the following command: gzip filename

3. decompress the gzip file as follows: gunzip filename.gz

4. list the attributes of the compressed file: gzip-l filename.gz.

5. the zcat command directly reads gzip files without decompression

Zcat filename.gz // This command can directly read the compressed file content

6. compression rate. we can specify the compression rate. the compression rate has 9 levels, of which:

Level 1 has the lowest compression rate, but the fastest compression speed. Level 9 has the highest compression rate, but the slowest compression speed.

6.4 use bunzip and bzip for compression

1 bunzip is another compression technology similar to gzip

2. compress with bzip2: bzip2 filename // bzip2deletes the source file and generates the file named filename.bz2.

3. decompress the bzip2 File: bunzip2 filename.bz2

4 when bzip2 or bunzip2 is used, it will delete the input file and generate the compressed output file. we can use option-k to avoid deleting the input file.

Bunzip2 filename.bz2-k

6.5 lzma compression

1. use lzma for compression: lzma filename // lzma deletes the original file and generates a file named filename. lzma.

2. retain input files: when lzma or unlzma is used, the input files are deleted and compressed output files are generated. However, you can specify the option-k to avoid deleting the original file.

6.6 archive and compress with zip

1 zip is a popular compression format, which can be seen on many platforms

2. archive files are compressed in zip format: zip output.zip source // The command will generate the output.zip file.

3. perform recursive operations on directories and files: zip-r output.zip directory

4. to extract content from a zip file, use the unzip output.zip // unzipcommand without deleting the output.zip file.

5. delete the content from the compressed archive file. run the-d: zip-d output.zip file // command to delete the file in the compressed file.

6. list the content in the archive file: unzip-l output.zip

6.7 encryption tools and hashes

Crypt

1 crypt is a simple encryption tool that receives a file and password from stdin as the input, and then outputs the encrypted data to the output

2 crypt <input_file> output_file

Enter passphrase:

Gpg

1 gpg is a widely used encryption solution. it uses the key signature technology to protect file content and only authenticated users can access data.

2. use gpg to encrypt the file: gpg-c filename // This Command uses an interactive password to read and generate filename. gpg

3. decrypt the gpg File: gpg filename. gpg // The Command reads the password and decrypts the file.

Md5sum and sha1sum

Both md5sum and sha1sum are unidirectional hash algorithms, and raw data cannot be released. they are usually used to verify data integrity or generate a unique key for specific data.

2. they can generate a unique key for each file: md5sum file or sha1sum file.

6.8 use rsync to back up system snapshots

1 rsync backs up files and directories located in different locations. it can minimize the amount of data transmitted through differential computing and compression technology.

2. copy the source directory to the destination: rsnyc-av source_path destination_path

-A indicates that the file is to be archived, and-v indicates that the detailed information or progress is printed on the standard output.

3. to back up data to a remote server or host, you can use: rsnyc-av source_dir username @ host: path.

You can use the following method to recover data from a remote host to a local host: rsnyc-av username @ host: path destination

The rsnyc command uses ssh to connect to the remote host and sets the address of the remote host in the form of user @ host. the user represents the user name, and the host represents the IP address or domain name of the remote host.

6.9 clone a disk with dd

1 dd command is originally intended to be data definition. incorrect use may lead to data loss, so it obtains the abuse of data destrory.

2 dd syntax: dd if = source of = target bs = block_size count = count

If indicates the input file or the input device path

Of indicates the path of the target file or device.

Bs indicates the block size, and count indicates the number of blocks to be copied. Both bs and count are optional.

3. to copy a partition to a file, you can use: dd if =/dev/sda1 of = sda1_partition.img // the/dev/sda1 in this command is the device path of the partition.

Restore partitions using backup: dd if = sda1_partition.img of =/dev/sda1

4 If you want to permanently delete all data in a partition, you can use dd to write 0 values to the partition.

Dd if =/dev/zero of =/dev/sda1 // dev/zero is a character device, which always returns the character '\ 0'

5. Clone hard disks with the same capacity: dd if =/dev/sda of =/dev/sdb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.