Linux Bash

SET command

use:

The shell may be provided implementation, output environment variables without parameters.

> set [+-abCdefhHklmnpPtuvx]

Note:

1. [-] 表示设置参数
2. [+] 表示取消设置参数

Example:

> tmp="nice day"
> $tmp
nice day
> set | grep tmp
tmp="nice day"
> unset tmp
> $tmp

SSH command

use:

Connect to a remote computer.

> ssh  -p <port> <user>@<hostname> <remote cmd>

Note:

  1. [-P] specify the port number, the default is 22
  2. [Remote cmd] remote execution of commands and displays to continue working locally

Configuration [.ssh] in [~ / .ssh / config] in

Host <myhost>
User <username>
HostName <ip>
IdentityFile ~/env/<username>.id_rsa

Can quickly ssh connection ssh myhost.

SSHPASS command

Not prompted for a password, use the password directly configured for remote login for the script.

installation:

# Ubuntu
apt-get install sshpass
# Mac
brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

Process management command

Front and back handover command bg, fg

> fg <task id>
> bg <task id>

Note:

  1. When these two tasks, if prompted no job control in this shell, use the set -mcommand open.
  2. When you want to temporarily suspend a task process can be used in the implementation process ctrl + Z, to terminate, to enter the suspendedstate, use ctrl + C, into the terminatedstate.
  3. Pending tasks can be fgre-run to the foreground, or by bgrunning transferred to the background.
  4. To terminate the pending tasks, it can be used kill <pid>.

Role & characters

use:

Start on back set this parameter indicates the process as a background process, which is called a job. byjobs

Command to see how many commands are currently running in the background.

Example:

> ./run.sh &
> jobs -l

Note:

  1. [-L] Display pid.

nohup keep processes

When the user closes the window, the terminal will be HUP (hungup) signal to turn off all of its child processes.

To have command is not terminated, there are two approaches:

  1. Let the process ignore the HUP signal, nohupit is the
  2. Let the process run in the new session setsid, (<cmd> &)it is the

Example:

> nohup ping www.ibm.com &
> setsid ping www.ibm.com
> (ping www.ibm.com &)
> ps -ef | grep www.ibm.com

If no prior use way to keep in front of the process, you can use the following procedure to remedy:

  1. First ctrl+Zsuspend the process, using the jobs -lacquisition process ID;
  2. Use bg <pid>put it in the background;
  3. Re-use disown <pid>, to avoid affecting HUP signal.

Use nohupcommand to save the default will be output to a file nohup.ou, save the file can be modified in the following ways

> nohup ${my_app} > common.out 2> error.out
> nohup ${my_app} > all_in.out &

ps snapshot process

process status, lists the snapshot of the current running process. Command parameters:

  1. [-A] show all processes;
  2. [-E] display environment variables;
  3. [-F] relationship between the display program;
  4. [-Aux] show all processes contain other users.

Usually used in conjunction with the grep command to find the name of a specific process to find Wechat related processes, for example:

# 将所有进程中的包含 Wechat 的连同表头一起输出
> ps -ef | grep -Ei 'PID|Wechat'

The output on my computer is:

  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1079     1   0 Thu10AM ??         3:23.55 /Applications/WeChat.app/Contents/MacOS/WeChat

kill terminate the process

Command to terminate the process, the parameters are:

  1. [-9] forced to delete;

File management commands

tail end of the file view

> tail [-n <line numbers>] [-f] <file>

Note:

  1. [-N] log can specify the number of lines displayed, is not specified the default is 10 lines
  2. [-F] can monitor the growth of the file, an abbreviated command tailf
  3. Correspondingly front of the display command several log head

a cat display file contents

> cat [-ns] <file>...

Note:

  1. [-N] display line numbers
  2. [-S] may merge a continuous empty behavior
  3. As opposed to command tacdisplays the file from the tail to the head (Mac does not seem to support)

Example:

# 一次显示整个文件
cat <file1> <file2>
# 创建新文件
cat > <file>
# 将多个文件合并为一个文件
cat <file1> <file2> > <file> 

more display file contents page by page

> more [-s] [-<line numbers>] [+<line numbers>] <file>

Note:

  1. [-S] may merge a continuous empty behavior
  2. [-N] can be defined on each roller line n
  3. [+ N] can be specified is displayed from the n-th row
  4. ctrl +FScroll up one screen, 空格键scroll down a screen
  5. = The scope and number of lines to display the file name of the currently displayed

Example:

# 从第 100 行开始显示 debug.log, 每次滚动 10 行,合并空行为一
> more -s -10 +100 debug.log

less page display file contents

Than morepaging view the file command on the powerful true sense, moreone can load the entire file, lesspage by page to load. lessAlso supports page up and search.

> less [-gimNsS] [-x <numbers>] <file>

Note:

  1. [-G] highlight the previous search results
  2. Ignore case when [-i] Search
  3. [-M] has been shown to display content percentage
  4. [-N] line numbers
  5. [-S] continuous combined display an empty behavior
  6. [-S] long lines when discarding the excess
  7. [-x 4] set tabto a predetermined number of spaces
  8. / Look up
  9. ? Look down
  10. n Repeat look forward
  11. N Find inverted repeat
  12. b Back down one page
  13. d Backward half page
  14. y Turn one page forward
  15. u The flip ahead a half page
  16. q drop out

Example:

# 查找历史命令中包含run的命令
> history | less
/run
# 假设找到要运行的命令序号为 100,按 q 退出,再按如下重新执行
> !100

grep find the file contents

grep (global search regular expression (RE) and print out the line, comprehensive search regular expression and print out the line)

> grep <option> <file>...
  1. [-A num] num row behind the display matching lines
  2. [-B num] num matching lines show the front row
  3. [-C num] num matching lines before and after the display of each line
  4. [--Color = auto] automatic color matching results
  5. [-M num] After the match num lines no longer continue to find
  6. [-N] where the row number display matching lines
  7. [-V] --invert-match line output does not match
  8. [-I] case insensitive
  9. [-E] --extended-regexp support extended regular expressions

Example:

# 查询日志文件中包含 keyword 但不含 keywords 的行,最多输出 10 行匹配,且显示匹配行前后 2 行
> grep -C 2 -n -m 10 keyword *.log | grep -v keywords

# 同时查找多个关键字
> grep -E 'a_key|b_key' a.file

tar to extract the files

tar was originally designed to back up files to the tape ( T APE Ar Chie), named tar.

tar on behalf of uncompressed tar file has been compressed tar file additional extensions of compressed files, such as .tar.gzexpressed through the gzip compression.

Command parameters:

  1. [-X] --extract untie tar file
  2. [-Z] --gzip, - gunzip, - unzip call gzip compress or decompress
  3. Filename [-f] --file designated to be processed
  4. [-T] --list tar file lists the information contained in the file
# 解压 .tar.gz 文件到当前文件夹
> tar -zxf a_file.tar.gz
# 解压 .tar 文件到 tmp 文件夹
> tar -xf b_file.tar -C tmp/
# gzip 压缩文件 c_file 和目录 d_dir 到 out.tar.gz
> tar -czvf out.tar.gz c_file d_dir

gunzip extracting file

gzip -d all.gz

gunzip all.gz

awk text analysis

Language and text processing tool for the line in the file search and pattern matching and the specified action on these lines, the name is taken from the three founders Alfred A HO, Peter W einberger, and Brian K ernighan of the Family Name the first character. See AWK programming language , awk command - IBM and Linux awk command - runoob.com a complete learning. Following is a brief Syntax:

> awk '{[pattern] action}' {filenames}   # 行匹配语句 awk '' 只能用单引号

Pattern support:

  • Regular expressions, eg. awk '/smith?/' a_file
  • Relational expression, eg. awk '$1 > $3' b_file
  • Combination of modes, more modes
    • And or || && !, is the true match
    • Parentheses (), combined match
    • Comma ,a match before the match
  • BEGIN and END patterns
    • BEGIN input mode is executed before the read
    • END performed after reading the input pattern

Example:

# 指定分隔符(默认是空格或 TAB),有两种写法
## 用逗号分割
> awk -F, '{print $3}' a_file
> awk 'BEGIN{FS=","} {print $3}' a_file
## 先用空格分割再用逗号分割
> awk -F '[ ,]'  '{print $3}' a_file
> awk 'BEGIN{FS="[ ,]"} {print $3}' a_file
# 忽略匹配文本的大小写
> awk 'BEGIN{IGNORECASE=1} /a_key/' a_file
# 获取 Kafka 进程的 ID
> ps aux | grep server.properties | grep -v grep | awk '{print $2}'

and

alias Add aliases

Tree View Files:

# 注意:这种方式会列出所有文件,建议使用 brew instal tree. 因为可以使用 `tree -L [depth]`自定义深度
> alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
> source ~/.zshrc

Cancel Alias:

> unalias tree

Move the cursor ctrl

Windows shortcuts Mac keyboard shortcuts Features
ctrl + left and right keys option + left and right keys Jump between words
ctrl + a Same as on the left. Jump to the Bank of the line
ctrl + e Same as on the left. Skip to footer
ctrl + u Same as on the left. Delete the current text cursor in front of the
ctrl + k Same as on the left. Delete the text after the current cursor
ctrl + w、ctrl + d Same as on the left. Character delete operation for the current word, w delete the word preceding the cursor, d behind the character deletion
ctrl + y Same as on the left. Recover deleted text
ctrl + l Same as on the left. Clear screen

Synchronize a file server to another

The basic idea: to copy files locally and then covered with the same name as the file to another table. Command as follows, where server-a, and server-bis defined in the file ~/.ssh/configis HOST.

ssh server-a "cat ~/test.txt" > ~/Desktop/test.txt;scp ~/Desktop/run.sh server-b:~/test.txt

# scp
 scp [email protected]:/root/apps/web/app.mdeploy.config ~/Downloads

Tree command Chinese garbled

tree -N folder

Change file access permissions

The following quote from Chapter VI Bird Brother Linux private kitchens , a slight cut.

(1) digital type change file permissions

There are nine basic permissions Linux file are owner/group/othersthree kinds of identity have their own read/write/executerights, privileges characters of the file is: -rwxrwxrwxThis authority is nine three groups of three! Among them, we can use the number to represent each permission, the score table for each of the following rights:

r:4
w:2
x:1

Each identity ( owner/group/others) each three rights ( r/w/x) required cumulative score is, for example, when permissions: [-rwxrwx---]Score is

owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0

Chmod command syntax permission to change is this:

chmod [-R] xyz 文件或目录

Options and parameters:

  • xyz: is the digital rights attribute type just mentioned, the attribute value is added to rwx.
  • -R: recursive (recursive) continues to change, that change will be together with all documents under sub-directory.

(2) Symbol Type change file permissions

There is a way to change the permissions of Yo! We can see from the previous description, basically nine permissions are (1)user (2)group (3)othersthree kinds of identity it! Then we can by u, g, orights to represent three identities! In addition, aall of the identity represents all that is! So read and write permissions can be written r, w, x! That is the way you can use the bottom point of view:

format chmod u g o a + (Addition) - (removed) = (setting) r w x File or directory
example chmod a = r .bashrc

To implement about it! If we want to set the permissions of a file to be -rwxr-xr-xwhen, basically:

  • user (u): have read, write, execute permissions;
  • group and others (g / o): have permission to read and execute.
chmod  u=rwx,go=rx  .bashrc
# 注意喔!那个 u=rwx,go=rx 是连在一起的,中间并没有任何空格!
chmod  a+w  .bashrc
# 增加.bashrc这个文件的每个人均可写入的权限

Regular way to delete files (Delete files with regular expression)

Accidentally, use llthe command found in many desktop ~$files beginning, the temporary file is opened with Microsoft three-piece, the original file has been deleted, it can also be hidden staging files exist on the desktop, so you want to bulk delete them.

# 在当前目录找到以 “~$” 开头的文件,然后执行删除操作
find . -name "~\$*" -delete

wget to download files

wget "http://mirrors.hust.edu.cn/apache/kafka/2.1.0/kafka_2.11-2.1.0.tgz"

View System Information

# 查看内核/操作系统/CPU信息
uname -a 
# 查看操作系统版本
head -n 1 /etc/issue 
# 查看CPU信息
cat /proc/cpuinfo 
# 查看计算机名
hostname 
# 查看环境变量
env 
# 查看内存使用量和交换区使用量
free -m 
# 查看各分区使用情况
df -h 
# 查看指定目录的大小
du -sh <目录名> 
# 查看内存总量
grep MemTotal /proc/meminfo 
# 查看空闲内存量
grep MemFree /proc/meminfo 
# 查看系统运行时间、用户数、负载
uptime 
# 查看系统负载
cat /proc/loadavg 
# 查看所有磁盘分区
fdisk -l 

Let system's CPU becomes high

for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/dev/null & done

Investigation and take up high CPU Java threads

Https://github.com/oldratlee/useful-scripts can use the following command:

sh show-busy-java-threads

limit:

  1. Linux environment;
  2. Installed jstack, Amazon Linux is not the default tool, you need to sudo yum install java-1.8.0-develinstall.

Guess you like

Origin www.cnblogs.com/lshare/p/11334751.html