############################################################################## # BASH CHEATSHEET (中文速查表) - by skywind (created on 2018/02/14) # Version: 47, Last Modified: 2019/09/24 17:58 # https://github.com/skywind3000/awesome-cheatsheets ##############################################################################
常用快捷键(默认使用 Emacs 键位) CTRL+A CTRL+B CTRL+C CTRL+D CTRL+E CTRL+F CTRL+G CTRL+H CTRL+K CTRL+L CTRL+N CTRL+O CTRL+P CTRL+R CTRL+S CTRL+T CTRL+U CTRL+V CTRL+W CTRL+X CTRL+Y CTRL+Z CTRL+_ ALT+b ALT+d ALT+f ALT+t ALT+BACKSPACE CTRL+X CTRL+X CTRL+X CTRL+E
BASH 基本操作 exit env echo $SHELL bash which bash whereis bash whatis bash clear reset
目录操作 cd cd {dirname } pwd mkdir {dirname } mkdir -p {dirname } pushd {dirname } popd dirs -v cd - cd -{N}
文件操作 ls ls -l ls -1 ls -a ln -s {fn} {link } cp {src} {dest} rm {fn} mv {src} {dest} touch {fn} cat {fn} any_cmd > {fn} more {fn} less {fn} head {fn} tail {fn} tail -f {fn} nano {fn} vim {fn} diff {f1} {f2} wc {fn} chmod 644 {fn} chgrp group {fn} chown user1 {fn} file {fn} basename {fn} dirname {fn} grep {pat} {fn} grep -r {pat} . stat {fn}
用户管理 whoami who w users passwd finger {user} adduser {user} deluser {user} w su su - su {user} su -{user} id {user} id -u {user} id -g {user} write {user} last last {user} lastb lastlog sudo {command }
进程管理 ps ps ax ps aux ps auxww ps -u {user} ps axjf ps xjf -u {user} ps -eo pid,user,command ps aux | grep httpd ps --ppid {pid} pstree pstree {user} pstree -u pgrep {procname} kill {pid} kill -9 {pid} kill -KILL {pid} kill -l kill -l TERM killall {procname} pkill {procname} top top -u {user} any_command & jobs bg fg fg {job} trap cmd sig1 sig2 trap "" sig1 sig2 trap - sig1 sig2 nohup {command } nohup {command } & disown {PID|JID} wait
常用命令:SSH / 系统信息 / 网络 ssh user@host ssh -p {port} user@host ssh-copy-id user@host scp {fn} user@host:path scp user@host:path dest scp -P {port} ... uname -a man {help } man -k {keyword} info {help } uptime date cal vmstat vmstat 10 free df du uname hostname showkey -a ping {host} ping -c N {host} traceroute {host} mtr {host} host {domain} whois {domain} dig {domain} route -n netstat -a netstat -an netstat -anp netstat -l netstat -t netstat -lntu netstat -lntup netstat -i netstat -rn ss -an ss -s wget {url} wget -qO- {url} curl -sL {url} sz {file} rz
变量操作 varname=value varname=value command echo $varname echo $$ echo $! echo $? export VARNAME=value array[0]=valA array[1]=valB array[2]=valC array=([0]=valA [1]=valB [2]=valC) array=(valA valB valC) ${array[i]} ${#array[@]} ${#array[i]} declare -a declare -f declare -F declare -i declare -r declare -x declare -p varname ${varname:-word} ${varname:=word} ${varname:?message} ${varname:+word} ${varname:offset:len} ${variable#pattern} ${variable##pattern} ${variable%pattern} ${variable%%pattern} ${variable/pattern/str} ${variable//pattern/str} ${#varname} *(patternlist) +(patternlist) ?(patternlist) @(patternlist) !(patternlist) array=($text ) IFS="/" array=($text ) text="${array[*]} " text=$(IFS=/; echo "${array[*]} " ) A=( foo bar "a b c" 42 ) B=("${A[@]:1:2} " ) C=("${A[@]:1} " ) echo "${B[@]} " echo "${B[1]} " echo "${C[@]} " echo "${C[@]: -2:2} " $(UNIX command ) varname=$(id -u user) num=$(expr 1 + 2) num=$(expr $num + 1) expr 2 \* \( 2 + 3 \) num=$((1 + 2 )) num=$(($num + 1 )) num=$((num + 1 )) num=$((1 + (2 + 3 ) * 2 ))
事件指示符 !! !^ !:n !:n-$ !$ !-n:$ !string !^string1^string2 ! !
函数 function myfunc () { {shell commands ...} } myfunc myfunc arg1 arg2 arg3 myfunc "$@ " myfunc "${array[@]} " shift unset -f myfunc declare -f
条件判断(兼容 posix sh 的条件判断):man test statement1 && statement2 statement1 || statement2 exp1 -a exp2 exp1 -o exp2 ( expression ) ! expression str1 = str2 str1 != str2 str1 < str2 str2 > str2 -n str1 -z str1 -a file -d file -e file -f file -r file -s file -w file -x file -N file -O file -G file file1 -nt file2 file1 -ot file2 num1 -eq num2 num1 -ne num2 num1 -lt num2 num1 -le num2 num1 -gt num2 num1 -ge num2
分支控制:if 和经典 test,兼容 posix sh 的条件判断语句 test {expression} [ expression ] test "abc" = "def" test "abc" != "def" test -a /tmp; echo $? [ -a /tmp ]; echo $? test cond && cmd1 [ cond ] && cmd1 [ cond ] && cmd1 || cmd2 if test -e /etc/passwd; then echo "alright it exists ... " else echo "it doesn't exist ... " fi if [ -e /etc/passwd ]; then echo "alright it exists ... " else echo "it doesn't exist ... " fi [ -e /etc/passwd ] && echo "alright it exists" || echo "it doesn't exist" if [ "$varname " = "foo" ]; then echo "this is foo" elif [ "$varname " = "bar" ]; then echo "this is bar" else echo "neither" fi if [ $x -gt 10 ] && [ $x -lt 20 ]; then echo "yes, between 10 and 20" fi [ $x -gt 10 ] && [ $x -lt 20 ] && echo "yes, between 10 and 20" if [ \( $x -gt 10 \) -a \( $x -lt 20 \) ]; then echo "yes, between 10 and 20" fi [ \( $x -gt 10 \) -a \( $x -lt 20 \) ] && echo "yes, between 10 and 20" [ -x /bin/ls ] && /bin/ls -l https://www.ibm.com/developerworks/library/l-bash-test/index.html
流程控制:while / for / case / until while condition; do statements done i=1 while [ $i -le 10 ]; do echo $i ; i=$(expr $i + 1) done for i in {1..10}; do echo $i done for name [in list]; do statements done for f in /home/*; do echo $f done for (( initialisation ; ending condition ; update )); do statements done for ((i = 0 ; i < 10 ; i++)); do echo $i ; done case expression in pattern1 ) statements ;; pattern2 ) statements ;; * ) otherwise ;; esac until condition; do statements done select name [in list]; do statements that can use $name done
命令处理 command ls builtin cd enable help {builtin_command} eval $script
输出/输入 重定向 cmd1 | cmd2 < file > file >> file >| file n>| file <> file n<> file n> file n< file n>& n<& n>&m n<&m &>file <&- >&- n>&- n<&- diff <(cmd1) <(cmd2)
文本处理 cut cut -c 1-16 # 截取每行头16个字符 cut -c 1-16 file # 截取指定文件中每行头 16个字符 cut -c3- # 截取每行从第三个字符开始到行末的内容 cut -d':' -f5 # 截取用冒号分隔的第五列内容 cut -d';' -f2,10 # 截取用分号分隔的第二和第十列内容 cut -d' ' -f3-7 # 截取空格分隔的三到七列 echo "hello" | cut -c1-3 # 显示 hel echo "hello sir" | cut -d' ' -f2 # 显示 sir ps | tr -s " " | cut -d " " -f 2,3,4 # cut 搭配 tr 压缩字符
awk awk '{print $5}' file awk -F ',' '{print $5}' file awk '/str/ {print $2}' file awk -F ',' '{print $NF}' file awk '{s+=$1} END {print s}' file awk 'NR%3==1' file
sed sed 's/find/replace/' file sed '10s/find/replace/' file sed '10,20s/find/replace/' file sed -r 's/regex/replace/g' file sed -i 's/find/replace/g' file sed -i '/find/i\newline' file sed -i '/find/a\newline' file sed '/line/s/find/replace/' file sed -e 's/f/r/' -e 's/f/r' file sed 's#find#replace#' file sed -i -r 's/^\s+//g' file sed '/^$/d' file sed -i 's/\s\+$//' file sed -n '2p' file sed -n '2,5p' file
排序 - sort sort file sort -r file sort -n file sort -t: -k 3n /etc/passwd sort -u file
source /path/to/z.sh z z foo z foo bar z -l foo z -r foo z -t foo
键盘绑定 bind '"\eh":"\C-b"' bind '"\el":"\C-f"' bind '"\ej":"\C-n"' bind '"\ek":"\C-p"' bind '"\eH":"\eb"' bind '"\eL":"\ef"' bind '"\eJ":"\C-a"' bind '"\eK":"\C-e"' bind '"\e;":"ls -l\n"'
网络管理:ip / ifconfig / nmap … ip a ip a show eth1 ip a add 172.16.1.23/24 dev eth1 ip a del 172.16.1.23/24 dev eth1 ip link show dev eth0 ip link set eth1 up ip link set eth1 down ip link set eth1 address {mac} ip neighbour ip route ip route add 10.1.0.0/24 via 10.0.0.253 dev eth0 ip route del 10.1.0.0/24 ifconfig ifconfig -a ifconfig eth0 ifconfig eth0 up ifconfig eth0 down ifconfig eth0 192.168.120.56 ifconfig eth0 10.0.0.8 netmask 255.255.255.0 up ifconfig eth0 hw ether 00:aa:bb:cc:dd :ee nmap 10.0.0.12 nmap -p 1024-65535 10.0.0.12 nmap 10.0.0.0/24 nmap -O -sV 10.0.0.12
有趣的命令 man hier man test man ascii getconf LONG_BIT bind -P mount | column -t curl ip.cn disown -a && exit cat /etc/issue lsof -i port:80 showkey -a svn diff | view - mv filename.{old,new} time read cp file.txt{,.bak} sudo touch /forcefsck find ~ -mmin 60 -type f curl wttr.in/~beijing echo ${SSH_CLIENT%% *} echo $[RANDOM%X+1] bind -x '"\C-l":ls -l' find / -type f -size +5M chmod --reference f1 f2 curl -L cheat.sh
常用技巧 history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head netstat -n | awk '/^tcp/ {++tt[$NF]} END {for (a in tt) print a, tt[a]}' sshfs name@server:/path/to/folder /path/to/mount/point ps aux | sort -nk +4 | tail while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29 ));date ;tput rc;done &wget -qO - "http://www.tarball.com/tarball.gz" | tar zxvf - python -c "import test.pystone;print(test.pystone.pystones())" dd if =/dev/zero of=/dev/null bs=1M count=32768mount /path/to/file.iso /mnt/cdrom -oloop ssh -t hostA ssh hostB wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images mkdir -p work/{project1,project2}/{src,bin,bak}find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01" lsof -P -i -n | cut -f 1 -d " " | uniq | tail -n +2 :w !sudo tee > /dev/null % source ~/github/profiles/my_bash_init.shssh -CqTnN -R 0.0.0.0:8443:192.168.1.2:443 user@202.115.8.1 ssh -CqTnN -L 0.0.0.0:8443:192.168.1.2:443 user@192.168.1.3 ssh -CqTnN -D localhost:1080 user@202.115.8.1 http://www.skywind.me/blog/archives/2021
有用的函数 function q-extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar -xvjf $1 ;; *.tar.gz) tar -xvzf $1 ;; *.tar.xz) tar -xvJf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) rar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar -xvf $1 ;; *.tbz2) tar -xvjf $1 ;; *.tgz) tar -xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1 ;; *) echo "don't know how to extract '$1 '..." ;; esac else echo "'$1 ' is not a valid file!" fi } function q-compress () { if [ -n "$1 " ] ; then FILE=$1 case $FILE in *.tar) shift && tar -cf $FILE $* ;; *.tar.bz2) shift && tar -cjf $FILE $* ;; *.tar.xz) shift && tar -cJf $FILE $* ;; *.tar.gz) shift && tar -czf $FILE $* ;; *.tgz) shift && tar -czf $FILE $* ;; *.zip) shift && zip $FILE $* ;; *.rar) shift && rar $FILE $* ;; esac else echo "usage: q-compress <foo.tar.gz> ./foo ./bar" fi } function ccat () { local style="monokai" if [ $# -eq 0 ]; then pygmentize -P style=$style -P tabsize=4 -f terminal256 -g else for NAME in $@ ; do pygmentize -P style=$style -P tabsize=4 -f terminal256 -g "$NAME " done fi }
好玩的配置 export LESS_TERMCAP_mb=$'\E[1m\E[32m' export LESS_TERMCAP_mh=$'\E[2m' export LESS_TERMCAP_mr=$'\E[7m' export LESS_TERMCAP_md=$'\E[1m\E[36m' export LESS_TERMCAP_ZW="" export LESS_TERMCAP_us=$'\E[4m\E[1m\E[37m' export LESS_TERMCAP_me=$'\E(B\E[m' export LESS_TERMCAP_ue=$'\E[24m\E(B\E[m' export LESS_TERMCAP_ZO="" export LESS_TERMCAP_ZN="" export LESS_TERMCAP_se=$'\E[27m\E(B\E[m' export LESS_TERMCAP_ZV="" export LESS_TERMCAP_so=$'\E[1m\E[33m\E[44m' "\eh" : backward-char"\el" : forward-char"\ej" : next-history"\ek" : previous-history"\eH" : backward-word"\eL" : forward-word"\eJ" : beginning-of-line"\eK" : end-of-line
References https://github.com/Idnan/bash-guide http://www.linuxstall.com/linux-command-line-tips-that-every-linux-user-should-know/ https://ss64.com/bash/syntax-keyboard.html http://wiki.bash-hackers.org/commands/classictest https://www.ibm.com/developerworks/library/l-bash-test/index.html https://www.cyberciti.biz/faq/bash-loop-over-file/ https://linuxconfig.org/bash-scripting-tutorial https://github.com/LeCoupa/awesome-cheatsheets/blob/master/languages/bash.sh https://devhints.io/bash https://github.com/jlevy/the-art-of-command-line https://yq.aliyun.com/articles/68541