Detailed explanation of commonly used scp, tail, grep commands under Linux

The article was edited at: Common commands under Linux

 scp command

Used to copy files or directories between local host and remote host, supporting encrypted transmission. It can transmit data through the ssh protocol, so the transmission process is safe.

Note: When using the scp command, if permission verification fails, you may need to check whether the ssh configuration between the local host and the remote host is correct.

scp [参数] [原路径] [目标路径]

[参数]可以省略,常用的参数有-r表示递归复制子目录,-P表示指定端口号等。

1. Copy the local files test.txtto the directory of the remote host /home/user:

scp test.txt user@remotehost:/home/user/

2. /home/user/test.txtCopy the files of the remote host to the current local directory:

scp user@remotehost:/home/user/test.txt .

3. Copy the entire directory (including subdirectories) to /home/userthe directory of the remote host: 

scp -r /path/to/local/dir user@remotehost:/home/user/

 4. Copy the entire directory (including subdirectories) from the remote host to the current local directory:

scp -r user@remotehost:/path/to/remote/dir .

 tailOrder

You can view the content at the end of the file, often used to view log files.

tail -f <文件名>:实时查看文件的最新内容,随着文件不断更新,继续输出新增的内容。

tail -n <行数> <文件名>:显示文件末尾的 <行数> 行内容,例如 tail -n 1000 log.txt 将会显示 log.txt 文件末尾的 1000 行内容。

 Note: If you use -fthe parameter to view the file contents in real time, you can Ctrl + Cexit the command by pressing the key combination tail. In addition, in many Linux distributions, dynamic tracking of log files can also be achieved by adding the --followor -Fparameter, so that even if the log file is deleted and re-created, the log file can still be monitored.

grepOrder

grepThe command can search in text files based on a specified pattern and output matching lines to the terminal.

Note: grepThe command is case-sensitive by default, so if you want to ignore case, you need to use -ithe argument. In addition, in order to improve search efficiency, you can limit the search scope and depth to avoid searching irrelevant directories and files.

grepThe syntax format of the command is as follows:

grep [参数] 模式 文件名

[参数]可以省略,常用的参数有 -i 表示忽略大小写,-r 表示递归搜索子目录,-n 表示显示匹配行号等。

1. error.logFind lines containing the string in the log file error:

grep "error" error.log

 2. Search multiple files for lines containing specific strings:

grep "error" *.log

3. Find Errorlines containing the string (ignoring case): 

grep -i "error" error.log

 4. Recursively search for errorlines containing the string in subdirectories: 

grep -r "error" /var/log/

Guess you like

Origin blog.csdn.net/qq_29639425/article/details/131315393