Redhat Overall review 1 output redirect and use of Head tail

Source: Internet
Author: User
Tags rsync

1. Use the Bash command to complete the following tasks on the server machine. (Test Center: Use of head tail)

. Displays the first 12 lines of the/usr/bin/clean-binary-files file and outputs it to the/home/student/headtail.txt

. Displays the last 9 lines of the/usr/bin/clean-binary-files file and adds it to the/home/student/headtail.txt

(stored in a file, remember that there is an incremental and offsite synchronization of the command rsync and SCP implementation, MK in a blog has been mentioned. Here is the use of >> redirect, here > and >> difference, Naifuzhang in knowing that the:> is directed output to a file, if the file does not exist, create a file, if the file exists, it is emptied; usually when we back up the cleanup log file, , this is the method: first back up the log, and then ' > ', the log file is emptied (the file size becomes 0 bytes);>> This is to append the output to the target file. If the file does not exist, the file is created, and if the file exists, the new content is appended to the end of the file, and the original content in the file is not affected. Output the previous file to the following file, in the order of the error. )

1.1 Displays the first 12 lines of the/usr/bin/clean-binary-files file and outputs it to the/home/student/headtail.txt file

$ head-n 12/usr/bin/clean-binary-files >/home/student/headtail.txt

1.2 Displays the last 9 lines of the/usr/bin/clean-binary-files file and adds it to the/home/student/headtail.txt

$ tail-n 9/usr/bin/clean-binary-files >>/home/student/headtail.txt

A summary of the MK Blog is attached:

rsync Incremental Update

Then is the incremental update of the file, the main problem is that there are now two servers A and B, to copy the test directory on the a server to the test directory on the B server (the so-called incremental update refers to the B server the original file is no longer transmitted, only those who have A and b not), This allows the test folder on the B server to remain synchronized with a

This requires the use of the rsync command, which uses the same command as the SCP.

For example, execute commands under a server: Rsync-r/home/test/[email protected]:/home/test for incremental updates

Note: Here is a question that needs to be explained in detail:

If the test directory has 1.txt,2.txt these two files

If the source directory in the command is written like this:/home/test/

So the list of files that rsync prepares to update is

1.txt

2.txt

Then you will find these two files in the B machine's/home/test directory and do incremental updates to meet the requirements

But if the source directory is written like this:/home/test

So the list of files that rsync prepares to incrementally update is this:

Test/1.txt

Test/2.txt

Then in the B machine's/home/test directory to find Test/1.txt, found no test folder, and then created the test folder, so the result is the B machine directory structure will have these two files, and this is not expected to see

/home/test/test/1.txt

/home/test/test/2.txt

So pay attention to this problem. The following two formulations are correct:

Rsync-r/home/test/[Email protected]:/home/test

Or

Rsync-r/home/test [Email protected]:/home

Specific settings for the detailed parameters of rsync do not say here, only one of the recommended command run mode is recorded:

rsync-rtv/home/test/[Email protected]:/home/test

-T refers to determining whether a file has a time stamp and the size of the file, and if it is the same, skip the file (this is a less rigorous but fast way)

-V refers to the output of the execution of the log, in fact, can add a lot more v,v, the output of more logs

Authentication-Free Access

Now you can combine crontab and rsync, of course, first of all need to solve the problem of rsync-free between two machines, that is, the command can be written directly (do not need B machine user name and password):

rsync-rtv/home/test [Email protected]:/home/test

About this online to see some of the configuration of rsync, but feel too much trouble, think of this and the SCP is similar, is not also directly SSH verification of the kind of way to solve, tried, feasible

For SSH-free login, this is done (the users here all use root, of course can use the other):

Under the A machine:

First, generate the secret key file

Ssh-keygen-t Rsa-p '

Note "is a two single quotation mark, indicating that the password is empty

The resulting id_rsa.pub file is then copied to the B server (note the location of the file is/root/.ssh, if the other user should be/home/user/.ssh, which is the home directory for the current user)

scp/root/.ssh/id_rsa.pub [Email protected]:/root/

(The password is also entered here because the operation has not been completed.)

Then operate on the B machine:

If there is no Authorized_keys this file in the/root/.ssh/, then the id_rsa.pub file from the A machine SCP will be moved and renamed to Authorized_keys.

Mv/root/id_rsa.pub/root/.ssh/authorized_keys

If the Authorized_keys file already exists, append the contents of the Id_rsa.pub file to Authorized_keys

Cat/root/id_rsa.pub >>/root/.ssh/authorized_keys

In this case, you can access the B machine on a machine without a password, and be careful not to reverse it.

Test on the A machine:

SSH [email protected]

If you do not need to enter a password to log in, the configuration is successful

Redhat Overall review 1 output redirect and use of Head tail

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.