方便的 Bash 終端技巧大集合

文章轉發自專業的Laravel開發者社區,原始連接: https://learnku.com/laravel/t...

Bash 集合

我很高興你在這裏!幾年前我從事生物信息學方面的研究工做。對那些簡單的 bash 命令感到驚訝,他們比個人枯燥腳本快不少。經過學習命令行的快捷方式和腳本幫助我節省了不少時間。近年來,我從事雲計算相關的工做,並在這裏繼續記錄那些有用的命令。而且我在努力的使他們簡短並且迅速。我主要使用 Ubuntu,RedHat ,Linux Mint 以及 CentOS 系統,若是命令在您的系統上不生效,那麼我很抱歉。php

該博客將重點介紹我從工做以及 LPIC 的考試中得到的用於解析數據和 Linux 系統維護的簡單命令,可是他們可能來自於親愛的 Google 和 Stackoverflow。html

英語和 bash 並非個人母語,請隨時糾正我,謝謝。若是你知道其餘有趣的命令,請教教我。node

這是更新潮的版本Bash-Oneliner~python

[](https://github.com/onceupon/B... bash 一行命令

[](https://github.com/onceupon/B...

使用 Ctrl 鍵
Ctrl + n : 相似向下的鍵
Ctrl + p : 相似向上的鍵
Ctrl + r : 反向搜索命令的歷史記錄(按住 Ctrl + r )
Ctrl + s : 終端中止輸出.(譯者注:如 apt / yum,nload,watch 等,按 Enter 繼續輸出)
Ctrl + q : 在 Ctrl + s 以後從新恢復以前的 terminal.
Ctrl + a : 移動光標到行的開始處
Ctrl + e : 移動光標到行的結尾處
Ctrl + d : 若是當前的 terminal 命令行有輸入,Ctrl + d 或刪除光標處的字符,不然會退出當前的 terminal
Ctrl + k : 刪除從當前光標到結尾的全部字符
Ctrl + x + backspace : 刪除當前光標到行開始的全部字符
Ctrl + t : 交換當前光標下的字符和其前面字符的位置。Esc + t 交換光標前面的兩個單詞
Ctrl + w : 剪切光標以前的單詞,而後 Ctrl + y 粘貼它
Ctrl + u : 剪切光標以前的行; 而後 Ctrl + y 粘貼它
Ctrl + _ : 撤銷以前的操做
Ctrl + l : 至關於清除
Ctrl + x + Ctrl + e : 召喚起 $EDITOR 環境變量設置的編輯器程序,對多行命令有效
[](https://github.com/onceupon/B...
Esc + u
# 將文本從光標的開始到結尾的單詞轉換爲大寫
Esc + l
# 將文本從光標的開始到結尾的單詞轉換爲小寫
Esc + c
# 將光標下的字母轉換爲大寫
[](https://github.com/onceupon/B... (例如 e.g. 53)
!53
[](https://github.com/onceupon/B...
!!
[](https://github.com/onceupon/B... : echo 'aaa' -> 如今運行: echo 'bbb')
#最後的一條命令: echo 'aaa'
^aaa^bbb

#echo 'bbb'
#bbb

#注意只有惟一的第一個 aaa 將會被替代,若是你想替代全部的 aaa,使用「:&」替代:
^aaa^bbb^:&
#或者
!!:gs/aaa/bbb/
[](https://github.com/onceupon/B... (e.g. cat 文件名)
!cat
# 或者
!c
#再次運行cat文件名
[](https://github.com/onceupon/B... 通配符
# '*' 用做文件名擴展的 "通配符" 。
/b?n/?at      #/bin/cat

# '?' 用做文件名擴展的單字符 "通配符" 。
/etc/pa*wd    #/etc/passwd

# ‘[]’ 用於匹配範圍內的字符。
ls -l [a-z]*   #列出全部文件名中帶有字母的文件。

# ‘{}’ 可用於匹配多個模式的文件名 
ls {*.sh,*.py}   #列出全部.sh和.py文件
[](https://github.com/onceupon/B...
$0   :shell或shell腳本的名稱。
$1, $2, $3, ... :位置參數。
$#   :位置參數的數量。
$?   :最新的管道退出狀態。
$-   :爲shell設置的當前選項。
$$   :當前shell(不是subshell)的pid。
$!   :最新後臺命令的PID。

$DESKTOP_SESSION     當前顯示管理器
$EDITOR   首選文本編輯器。
$LANG   當前語言。
$PATH   搜索可執行文件的目錄列表(即準備運行的程序)
$PWD    當前目錄
$SHELL  當前 shell
$USER   當前用戶名
$HOSTNAME   當前主機名

篩選

[返回頂部]linux

[](https://github.com/onceupon/B...
grep = grep -G # Basic Regular Expression (BRE)
fgrep = grep -F # fixed text, ignoring meta-charachetrs
egrep = grep -E # Extended Regular Expression (ERE)
pgrep = grep -P # Perl Compatible Regular Expressions (PCRE)
rgrep = grep -r # recursive
[](https://github.com/onceupon/B...
grep -c "^$"
[](https://github.com/onceupon/B...
grep -o '[0-9]*'
#or
grep -oP '\d'
[](https://github.com/onceupon/B...(例如3)
grep ‘[0-9]\{3\}’
# or
grep -E ‘[0-9]{3}’
# or
grep -P ‘\d{3}’
[](https://github.com/onceupon/B...
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
# or
grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
[](https://github.com/onceupon/B...(例如 「target」 )
grep -w 'target'

#or using RE
grep '\btarget\b'
[](https://github.com/onceupon/B...(例如「bbo」)
# return also 3 lines after match
grep -A 3 'bbo'

# return also 3 lines before match
grep -B 3 'bbo'

# return also 3 lines before and after match
grep -C 3 'bbo'
[](https://github.com/onceupon/B...(例如「S」)
grep -o 'S.*'
[](https://github.com/onceupon/B...(例如 w1,w2 )
grep -o -P '(?<=w1).*(?=w2)'
[](https://github.com/onceupon/B...(例如 bbo )
grep -v bbo filename
[](https://github.com/onceupon/B...(例如 # )
grep -v '^#' file.txt
[](https://github.com/onceupon/B...(例如 bbo="some strings" )
grep "$boo" filename
#remember to quote the variable!
[](https://github.com/onceupon/B...(例如 bbo )
grep -m 1 bbo filename
[](https://github.com/onceupon/B...(例如 bb )
grep -c bbo filename
[](https://github.com/onceupon/B... (例如一行出現3次則記爲3次)
grep -o bbo filename |wc -l
[](https://github.com/onceupon/B...(例如 bbo/BBO/Bbo )
grep -i "bbo" filename
[](https://github.com/onceupon/B...(例如 bbo )
grep --color bbo filename
[](https://github.com/onceupon/B...(例如 bbo )
grep -R bbo /path/to/directory
# or
grep -r bbo /path/to/directory
[](https://github.com/onceupon/B...,不輸出文件名(例如 bbo )
grep -rh bbo /path/to/directory
[](https://github.com/onceupon/B...輸出匹配的文件名(例如 bbo )
grep -rl bbo /path/to/directory
[](https://github.com/onceupon/B...(例如 A 或 B 或 C 或 D )
grep 'A\|B\|C\|D'
[](https://github.com/onceupon/B...(例如 A 與 B )
grep 'A.*B'
[](https://github.com/onceupon/B...(例如 ACB 或 AEB )
grep 'A.B'
[](https://github.com/onceupon/B...(例如 color 或 colour )
grep ‘colou?r’
[](https://github.com/onceupon/B... 在文件B中查找文件A的內容
grep -f fileA fileB
[](https://github.com/onceupon/B...
grep $'\t'
[](https://github.com/onceupon/B...
$echo "$long_str"|grep -q "$short_str"
if [ $? -eq 0 ]; then echo 'found'; fi
#grep -q will output 0 if match found
#remember to add space between []!
[](https://github.com/onceupon/B...
grep -oP '\(\K[^\)]+'
[](https://github.com/onceupon/B...(例如 AAEL0000-RA )
grep -o -w "\w\{10\}\-R\w\{1\}"
# \w 文字字符 [0-9a-zA-Z_] \W 非文字字符
[](https://github.com/onceupon/B...(例如 bbo )
grep -d skip 'bbo' /path/to/files/*

流編輯器

[返回頂部]ios

[](https://github.com/onceupon/B...
sed 1d filename
[](https://github.com/onceupon/B...(刪除第1-100行)
sed 1,100d filename
[](https://github.com/onceupon/B...: bbo)
sed "/bbo/d" filename
- case insensitive:
sed "/bbo/Id" filename
[](https://github.com/onceupon/B...(例如第5個字符不等於2)
sed -E '/^.{5}[^2]/d'
#aaaa2aaa (you can stay)
#aaaa1aaa (delete!)
[](https://github.com/onceupon/B...(編輯並保存)
sed -i "/bbo/d" filename
[](https://github.com/onceupon/B...(例如$i)時,請使用雙引號" "
# e.g. add >$i to the first line (to make a bioinformatics FASTA file)
sed "1i >$i"
# notice the double quotes! in other examples, you can use a single quote, but here, no way!
# '1i' means insert to first line
[](https://github.com/onceupon/B...
# 使用反斜槓 $ 符,同時使用雙引號來標記變量
sed -e "\$s/\$/\n+--$3-----+/"
[](https://github.com/onceupon/B...
sed '/^\s*$/d'

# 或

sed '/^$/d'
[](https://github.com/onceupon/B...
sed '$d'
[](https://github.com/onceupon/B...
sed -i '$ s/.$//' filename
[](https://github.com/onceupon/B...(例如『[』)
sed -i '1s/^/[/' file
[](https://github.com/onceupon/B...(例如添加「 something 」到第1行和第3行)
sed -e '1isomething -e '3isomething'
[](https://github.com/onceupon/B...(例如『]』)
sed '$s/$/]/' filename
[](https://github.com/onceupon/B...
sed '$a\'
[](https://github.com/onceupon/B...(例如 bbo)
sed -e 's/^/bbo/' file
[](https://github.com/onceupon/B...(例如『}』)
sed -e 's/$/\}\]/' filename
[](https://github.com/onceupon/B... n (例如每四個字符t添加 n)
sed 's/.\{4\}/&\n/g'
[](https://github.com/onceupon/B... 鏈接/結合/合併文件。例如(用「,」去分割)
sed -s '$a,' *.json > all.json
[](https://github.com/onceupon/B... B 去替換 A)
sed 's/A/B/g' filename
[](https://github.com/onceupon/B... aaa = 開頭的行替換成 aaa=/my/new/path)
sed "s/aaa=.*/aaa=\/my\/new\/path/g"
[](https://github.com/onceupon/B... bbo)
sed -n '/^@S/p'
[](https://github.com/onceupon/B...
sed '/bbo/d' filename
[](https://github.com/onceupon/B...
sed -n 500,5000p filename
[](https://github.com/onceupon/B... n 行打印一次
sed -n '0~3p' filename

# catch 0: start; 3: step
[](https://github.com/onceupon/B...
sed -n '1~2p'
[](https://github.com/onceupon/B...,包括第一行
sed -n '1p;0~3p'
[](https://github.com/onceupon/B...
sed -e 's/^[ \t]*//'
# Notice a whitespace before '\t'!!
[](https://github.com/onceupon/B...
sed 's/ *//'

# 注意'*'前的空格!!
[](https://github.com/onceupon/B...
sed 's/,$//g'
[](https://github.com/onceupon/B...
sed "s/$/\t$i/"
# $i 是你要添加的值

# 將文件名添加到文件的最後一列
for i in $(ls);do sed -i "s/$/\t$i/" $i;done
[](https://github.com/onceupon/B...
for i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np
[](https://github.com/onceupon/B...
sed ':a;N;$!ba;s/\n//g'
[](https://github.com/onceupon/B...: 第 123 行)
sed -n -e '123p'
[](https://github.com/onceupon/B...: 第 10 行到第 33 行)
sed -n '10,33p' <filename
[](https://github.com/onceupon/B...
sed 's=/=\\/=g'
[](https://github.com/onceupon/B... (例如: A-1-eA-2-e 或者 A-3-e ....)
sed 's/A-.*-e//g' filename
[](https://github.com/onceupon/B...
sed '$ s/.$//'
[](https://github.com/onceupon/B...: AAAAAA—> AAA#AAA)
sed -r -e 's/^.{3}/&#/' file

Awk

[返回頂部]laravel

[](https://github.com/onceupon/B... tab 爲分隔符
awk -F $'\t'
[](https://github.com/onceupon/B... tab 爲輸出字段分隔符
awk -v OFS='\t'
[](https://github.com/onceupon/B...
a=bbo;b=obb;
awk -v a="$a" -v b="$b" "$1==a && $10=b" filename
[](https://github.com/onceupon/B...
awk '{print NR,length($0);}' filename
[](https://github.com/onceupon/B...
awk '{print NF}'
[](https://github.com/onceupon/B...
awk '{print $2, $1}'
[](https://github.com/onceupon/B...(例如檢查第一列)
awk '$1~/,/ {print}'
[](https://github.com/onceupon/B...,並循環輸出
awk '{split($2, a,",");for (i in a) print $1"\t"a[i]}' filename
[](https://github.com/onceupon/B...,打印數據 (例如在 bbo 出現7次以後,中止輸出數據)
awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
[](https://github.com/onceupon/B...
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file
[](https://github.com/onceupon/B... (例如在第三列前面加「chr」)
awk 'BEGIN{OFS="\t"}$3="chr"$3'
[](https://github.com/onceupon/B... (例如刪除含有 bbo 的行)
awk '!/bbo/' file
[](https://github.com/onceupon/B...
awk 'NF{NF-=1};1' file
[](https://github.com/onceupon/B... NR 和 FNR 的用法
# 例如如下2個文件:
# fileA:
# a
# b
# c
# fileB:
# d
# e
awk 'print FILENAME, NR,FNR,$0}' fileA fileB
# fileA    1    1    a
# fileA    2    2    b
# fileA    3    3    c
# fileB    4    1    d
# fileB    5    2    e
邏輯與
# 好比下面這兩個文件:
# 文件 A:
# 1    0
# 2    1
# 3    1
# 4    0
# 文件 B:
# 1    0
# 2    1
# 3    0
# 4    1

awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA 文件 B
# 1    0
# 2    1
# 3    0
# 4    0
[](https://github.com/onceupon/B...
awk '{while (match($0, /[0-9]+\[0-9]+/)){
    \printf "%s%.2f", substr($0,0,RSTART-1),substr($0,RSTART,RLENGTH)
    \$0=substr($0, RSTART+RLENGTH)
    \}
    \print
    \}'
[](https://github.com/onceupon/B...
awk '{printf("%s\t%s\n",NR,$0)}'
[](https://github.com/onceupon/B...
#例如分開一下內容:
# David    cat,dog
# into
# David    cat
# David    dog

awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file

# 詳情介紹請點擊這裏: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
[](https://github.com/onceupon/B...
awk '{s+=$1}END{print s/NR}'
[](https://github.com/onceupon/B... Linux)
awk '$1 ~ /^Linux/'
[](https://github.com/onceupon/B... (例如 1 40 35 12 23 --> 1 12 23 35 40)
awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s\t", a[i] ); printf( "\n" ); }'
[](https://github.com/onceupon/B...,它等於column4減去最後一列5)
awk '{$6 = $4 - prev5; prev5 = $5; print;}'

Xargs

[回到頂部]git

[](https://github.com/onceupon/B...:空格)
xargs -d\t
[](https://github.com/onceupon/B...
echo 1 2 3 4 5 6| xargs -n 3
# 1 2 3
# 4 5 6
[](https://github.com/onceupon/B...
echo a b c |xargs -p -n 3
[](https://github.com/onceupon/B...
xargs -t abcd
# bin/echo abcd
# abcd
[](https://github.com/onceupon/B...
find . -name "*.html"|xargs rm

# when using a backtick
rm `find . -name "*.html"`
[](https://github.com/onceupon/B... 「hello 2001」)
find . -name "*.c" -print0|xargs -0 rm -rf
[](https://github.com/onceupon/B...
xargs --show-limits
[](https://github.com/onceupon/B...
find . -name "*.bak" -print 0|xargs -0 -I {} mv {} ~/old

# or
find . -name "*.bak" -print 0|xargs -0 -I file mv file ~/old
[](https://github.com/onceupon/B...
ls |head -100|xargs -I {} mv {} d1
[](https://github.com/onceupon/B...
time echo {1..5} |xargs -n 1 -P 5 sleep

# a lot faster than:
time echo {1..5} |xargs -n1 sleep
[](https://github.com/onceupon/B... A 複製到 B
find /dir/to/A -type f -name "*.py" -print 0| xargs -0 -r -I file cp -v -p file --target-directory=/path/to/B

# v: verbose|
# p: keep detail (e.g. owner)
sed 相關
ls |xargs -n1 -I file sed -i '/^Pos/d' filename
[](https://github.com/onceupon/B...
ls |sed 's/.txt//g'|xargs -n1 -I file sed -i -e '1 i\>file\' file.txt
[](https://github.com/onceupon/B...
ls |xargs -n1 wc -l
[](https://github.com/onceupon/B...
ls -l| xargs
[](https://github.com/onceupon/B...
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
# "--" 信號選項結束,並進一步進行選項的處理
[](https://github.com/onceupon/B...,也統計總行數。
ls|xargs wc -l
[](https://github.com/onceupon/B... 以及 grep 命令
cat grep_list |xargs -I{} grep {} filename
[](https://github.com/onceupon/B... 和 sed 命令 (將全部舊的 ip 地址替換成在 etc 文件下面新的 ip 地址)
grep -rl '192.168.1.111' /etc | xargs sed -i 's/192.168.1.111/192.168.2.111/g'

Find(查詢)

[返回頂部]程序員

[](https://github.com/onceupon/B... 列出當前目錄中的全部子目錄/文件
find .
[](https://github.com/onceupon/B... 列出當前目錄下的全部文件
find . -type f
[](https://github.com/onceupon/B... 列出當前文件下的全部目錄
find . -type d
[](https://github.com/onceupon/B... 編輯當前目錄下的全部文件(例如,將「www」替換爲「ww」)
find . -name '*.php' -exec sed -i 's/www/w/g' {} \;

# 若是沒有子目錄
replace "www" "w" -- *
# a space before *
[](https://github.com/onceupon/B... 查詢文件 並 打印文件名(例如 「mso」)
find mso*/ -name M* -printf "%f\n"
[](https://github.com/onceupon/B... 查找 並 刪除 文件大小 小於(例如74字節)的文件
find . -name "*.mso" -size -74c -delete

# M 表明 MB, 等等

Condition and loop (條件與循環)

[返回頂部]github

[](https://github.com/onceupon/B... 語句
# 使用 if 和 else 來進行條件判斷 
if [[ "$c" == "read" ]]; then outputdir="seq"; else outputdir="write" ; fi

# 判斷myfile是否包含字符串「test」
if grep -q hello myfile; then …

# 判斷mydir 是不是一個目錄, 修改 mydir 的內容 而且 執行其餘操做:
if cd mydir; then
  echo 'some content' >myfile
else
  echo >&2 "Fatal error. This script requires mydir."
fi

# 判斷 variable(變量)  是否爲空
if [ ! -s "myvariable" ]
#返回指定對象的長度  若是是 "字符串"  返回 0.

# 判斷文件是否存在
if [ -e 'filename' ]
then
  echo -e "file exists!"
fi

# 判斷 myfile 文件是否存在   或者  myfile 鏈接是否存在 
if [ -e myfile ] || [ -L myfile ]
then
  echo -e "file exists!"
fi

# 判斷 變量 x  是否大於 等於 5
if [ "$x" -ge 5 ]; then …

#  在 bash/ksh/zsh 中 判斷 變量 x 是否大於等於 5,:
if ((x >= 5)); then …

# 使用  (( ))(雙括號) 進行數學運算  將u+2的計算結果賦值給  j 
if ((j==u+2))

# 使用 [[ ]](雙中括號)  進行數值比較   在[[]] 中 會將 特殊符號 自動轉換爲 比較符號 (例如  = 轉換爲 == )  
if [[ $age -gt 21 ]]

More if commands

[](https://github.com/onceupon/B...
For 語法
for i in $(ls); do echo file $i;done
#或者
for i in *; do echo file $i; done

# 按任意鍵繼續執行遍歷
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key;done

# 當按下一個鍵會逐行打印文件
oifs="$IFS"; IFS=$'\n'; for line in $(cat myfile); do ...; done
while read -r line; do ...; done <myfile

# If only one word a line, simply ( 遍歷文件內容 一行一行的遍歷)
for line in $(cat myfile); do echo $line; read -n1; done

#遍歷一個數組
for i in "${arrayName[@]}"; do echo $i;done
[](https://github.com/onceupon/B... 語句,
# 文件的列減法 (例如 a 3 columns file)
while read a b c; do echo $(($c-$b));done < <(head filename)
#在兩個 "<" 的中間有個空格 

# 彙總 列 減法
i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)

# 繼續檢查正在運行的進程(例如perl),並在啓動後當即啓動另外一個新進程(例如python)。 (最好使用wait命令!Ctrl + F " wait")
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
[](https://github.com/onceupon/B... (case in bash)
read type;
case $type in
  '0')
    echo 'how'
    ;;
  '1')
    echo 'are'
    ;;
  '2')
    echo 'you'
    ;;
esac

[](https://github.com/onceupon/B...

變量

[返回頂部]

[](https://github.com/onceupon/B...
# foo=bar
 echo "'$foo'"
#'bar'
# 單引號/雙引號  quotes around single quotes make the inner single quotes expand variables(在單引號內使用變量 變量會被解析)
[](https://github.com/onceupon/B...
var="some string"
echo ${#var}
# 11
[](https://github.com/onceupon/B...
var=string
echo "${var:0:1}"
#s

# or
echo ${var%%"${var#?}"}
[](https://github.com/onceupon/B... 從第一位 或最後一位 開始刪除變量中的 n 個字節
var="some string"
echo ${var:2}
#me string
[](https://github.com/onceupon/B... (例如. 刪除第一個位置的 0 )
var="0050"
echo ${var[@]#0}
#050
[](https://github.com/onceupon/B... (例如. 將 字符"a" 替換爲 字符",")
{var/a/,}
# 使用 grep 
 test="god the father"
 grep ${test// /\\\|} file.txt
 # turning the space into 'or' (\|) in grep (在grep 的空間中將 替換   or 和 (\|) )
[](https://github.com/onceupon/B...(參數擴展)
var=HelloWorld
echo ${var,,}
helloworld
[](https://github.com/onceupon/B... Expand and then execute variable/argument (先執行腳本進行賦值 再輸出變量內容)
cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo

數學

[back to top]

[](https://github.com/onceupon/B... (運算符: +, -, *, /, %, 等等)
#總結: a++ 先運算a,後a的值加1;++a,則相反,先加一,再參與運算。同理a--,與--a
echo $(( 10 + 5 ))  #15
x=1
echo $(( x++ )) #1 , 注意它仍然是1,由於它是後遞增的
echo $(( x++ )) #2
echo $(( ++x )) #4 , 注意,它不是3,由於它是預增量
echo $(( x-- )) #4
echo $(( x-- )) #3
echo $(( --x )) #1
x=2
y=3
echo $(( x ** y )) #8
[](https://github.com/onceupon/B... (好比: 50)
factor 50
[](https://github.com/onceupon/B...。 (好比: seq 10)
seq 10|paste -sd+|bc
[](https://github.com/onceupon/B...(文件中每行僅包含一個數字)
awk '{s+=$1} END {print s}' filename
[](https://github.com/onceupon/B...
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
[](https://github.com/onceupon/B...
expr 10+20 #30
expr 10\*20 #600
expr 30 \> 20 #1 (true)
[](https://github.com/onceupon/B...
# 小數位數/有效數字
echo "scale=2;2/3" | bc
#.66

# 指數運算符
echo "10^2" | bc
#100

# 使用變量
echo "var=5;--var"| bc
#4

Time

[back to top]

[](https://github.com/onceupon/B...
time echo hi
[](https://github.com/onceupon/B...(例如10秒)
sleep 10
[](https://github.com/onceupon/B...(例如10秒)
TMOUT=10
# 一旦你設置了這個變量,註銷計時器開始運行!
[](https://github.com/onceupon/B...
# 僅僅運行 `sleep 10` 一秒。
timeout 1 sleep 10
[](https://github.com/onceupon/B...(例如,從如今開始 1分鐘)
at now + 1min  # 時間單位能夠是 minutes, hours, days, 或 weeks
⚠️: 命令將使用 /bin/sh
at> echo hihigithub >~/itworks
at> <EOT>   # 按 Ctrl + D 退出
job 1 at Wed Apr 18 11:16:00 2018

[](https://github.com/onceupon/B...

Download

[回到頂部]

[](https://github.com/onceupon/B... (你正在看的內容)
curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -f markdown -t man | man -l -

# 或者 w3m (一種基於文本的瀏覽器和呼叫器)
curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc | w3m -T text/html

# 或者使用emacs (在emac文本編輯器中)
emacs --eval '(org-mode)' --insert <(curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -t org)

# 或者使用 emacs (在終端上先按Ctrl+x,再按Ctrl+c退出)
emacs -nw --eval '(org-mode)' --insert <(curl https://raw.githubusercontent.com/onceupon/Bash-Oneliner/master/README.md | pandoc -t org)
[](https://github.com/onceupon/B...
wget -r -l1 -H -t1 -nd -N -np -A mp3 -e robots=off http://example.com

# -r: recursive and download all links on page遞歸併下載頁面上全部連接
# -l1: only one level link僅一級連接
# -H: span host, visit other hosts跨越主機,訪問其餘主機
# -t1: numbers of retries重試次數
# -nd: don't make new directories, download to here不要建立新目錄,下載到這裏
# -N: turn on timestamp打開時間戳
# -nd: no parent沒有父級
# -A: type (separate by ,)類型(以,豆號分隔)
# -e robots=off: ignore the robots.txt file which stop wget from crashing the site, sorry example.com忽略robots.txt文件,該文件阻止wget使網站崩潰,抱歉example.com
[](https://github.com/onceupon/B... (https://transfer.sh/)
#  上傳文件 (例如: filename.txt):
curl --upload-file ./filename.txt https://transfer.sh/filename.txt
# 上面的命令將返回一個url,例如:https://transfer.sh/tG8rM/filename.txt

# 接下來您能夠經過如下方式下載它:
curl https://transfer.sh/tG8rM/filename.txt -o filename.txt
[](https://github.com/onceupon/B...(若有須要)
data=file.txt
url=http://www.example.com/$data
if [ ! -s $data ];then
    echo "downloading test data..."
    wget $url
fi
[](https://github.com/onceupon/B... 命令獲取文件名 (當文件名很長時)
wget -O filename "http://example.com"
[](https://github.com/onceupon/B... 將文件保存到文件夾
wget -P /path/to/directory "http://example.com"
[](https://github.com/onceupon/B...,直到到達最終目的地:
curl -L google.com

[](https://github.com/onceupon/B...

隨機

[back to top]

[](https://github.com/onceupon/B...(例如,生成5個長度爲13的密碼)
sudo apt install pwgen
pwgen 13 5
#sahcahS9dah4a xieXaiJaey7xa UuMeo0ma7eic9 Ahpah9see3zai acerae7Huigh7
[](https://github.com/onceupon/B...
shuf -n 100 filename
[](https://github.com/onceupon/B...
for i in a b c d e; do echo $i; done| shuf
[](https://github.com/onceupon/B...(例如,從0-100 內隨機選擇15個數字)
shuf -i 0-100 -n 15
[](https://github.com/onceupon/B...
echo $RANDOM
[](https://github.com/onceupon/B...
echo $((RANDOM % 10))
[](https://github.com/onceupon/B...
echo $(((RANDOM %10)+1))

[](https://github.com/onceupon/B...

Xwindow

[回到頂部]

X11 GUI 應用程序! 若是你對純文本的環境感到厭煩,這裏有一些適合你的 GUI 工具。

[](https://github.com/onceupon/B... X11 轉發,以便在服務器上使用圖形應用程序。
ssh -X user_name@ip_address

# 或者經過 xhost 設置
# --> Install the following for Centos:
# xorg-x11-xauth
# xorg-x11-fonts-*
# xorg-x11-utils
[](https://github.com/onceupon/B... xwindow 工具
xclock
xeyes
xcowsay
[](https://github.com/onceupon/B... ssh 服務器中打開圖片/圖像
1\. ssh -X user_name@ip_address
2. apt-get install eog
3. eog picture.png
[](https://github.com/onceupon/B... 在服務器上觀看視頻
1\. ssh -X user_name@ip_address
2. sudo apt install mpv
3. mpv myvideo.mp4
[](https://github.com/onceupon/B... gedit (GUI 編輯)
1\. ssh -X user_name@ip_address
2. apt-get install gedit
3. gedit filename.txt
[](https://github.com/onceupon/B... ssh 服務器上打開 PDF
1\. ssh -X user_name@ip_address
2. apt-get install evince
3. evince filename.pdf
[](https://github.com/onceupon/B... ssh 服務器上使用谷歌瀏覽器
1\. ssh -X user_name@ip_address
2. apt-get install libxss1 libappindicator1 libindicator7
3. wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
4. sudo apt-get install -f
5. dpkg -i google-chrome*.deb
6. google-chrome

[](https://github.com/onceupon/B...

系統

[back to top]

[](https://github.com/onceupon/B...
journalctl -u <service_name> -f
[](https://github.com/onceupon/B...
# 殭屍進程已經死了,因此你不能殺死它。您能夠消除經過殺死其父進程。
# 首先,找到殭屍進程的PID
ps aux| grep 'Z'
# 接下來發現殭屍的父進程的PID
pstree -p -s <zombie_PID>
# 而後你能夠殺死它的父進程,你會發現殭屍進程已經不見了。
sudo kill 9 <parent_PID>
[](https://github.com/onceupon/B...
free -c 10 -mhs 1
# 每隔1秒打印10次
[](https://github.com/onceupon/B...
#每秒刷新一次
iostat -x -t 1
[](https://github.com/onceupon/B...(例如 enp175s0f0)
iftop -i enp175s0f0
[](https://github.com/onceupon/B...
uptime
[](https://github.com/onceupon/B...
if [ "$EUID" -ne 0 ]; then
        echo "Please run this as root"
        exit 1
fi
[](https://github.com/onceupon/B...: bonnie)
chsh -s /bin/sh bonnie
# /etc/shells: valid login shells
[](https://github.com/onceupon/B... (e.g. 更改 root 爲 newroot)
chroot /home/newroot /bin/bash

# To exit chroot
exit
[](https://github.com/onceupon/B...(例如filename.txt)的文件狀態(大小;訪問、修改和更改時間等)
stat filename.txt
[](https://github.com/onceupon/B...
ps aux
[](https://github.com/onceupon/B...
pstree
[](https://github.com/onceupon/B...
cat /proc/sys/kernel/pid_max
[](https://github.com/onceupon/B...
dmesg
[](https://github.com/onceupon/B...
$ip add show

# or
ifconfig
[](https://github.com/onceupon/B...
打印之前和當前的 SysV 運行級別
runlevel

#或者
who -r
[](https://github.com/onceupon/B... SysV 運行級別 (例如修改成 5)
init 5
#或者
telinit 5
[](https://github.com/onceupon/B...
chkconfig --list
# 至關於 ubntu 中 chkconfig 的 update-rc.d
[](https://github.com/onceupon/B...
cat /etc/*-release
[](https://github.com/onceupon/B... 程序員的手冊:文件系統的層次結構的說明
man hier
[](https://github.com/onceupon/B...
#  檢查 cron 的狀態
systemctl status cron.service

#  中止一個 cron 服務
systemctl stop cron.service
[](https://github.com/onceupon/B...
jobs -l
[](https://github.com/onceupon/B... (例如 ./test.sh)
# 較好的值在 -20 (最有利)到 19 之間調整
# 應用程序越好,優先級越低
# 默認值:10 ,默認優先級:80

nice -10 ./test.sh
[](https://github.com/onceupon/B...
export PATH=$PATH:~/path/you/want
[](https://github.com/onceupon/B...
讓文件可執行
chmod +x filename
# 如今你能夠執行 ./filename
[](https://github.com/onceupon/B...
uname -a

# 檢查系統硬件平臺 (x86-64)
uname -i
[](https://github.com/onceupon/B...
links www.google.com
[](https://github.com/onceupon/B...,設置密碼
useradd username
passwd username
[](https://github.com/onceupon/B... bash 變量(例如顯示整個路徑)
1\. joe ~/.bash_profile
2. export PS1='\u@\h:\w\$'
# $PS1 是一個定義命令提示符外觀和樣式的變量
3\. source ~/.bash_profile
[](https://github.com/onceupon/B... (例如 alias)
1\. joe ~/.bash_profile
2. alias pd="pwd" //no more need to type that 'w'!
3\. source ~/.bash_profile
[](https://github.com/onceupon/B...
alias -p
[](https://github.com/onceupon/B... (例如別名 ls='ls --color=auto' 以後)
unalias ls
[](https://github.com/onceupon/B... shell 選項
# 打印全部的 shell 選項
shopt

# 取消(或者中止)別名
shopt -u expand_aliases

# 設置(或者開始)別名
shopt -s expand_aliases
[](https://github.com/onceupon/B... (例如 PATH)
echo $PATH
#用冒號分隔的目錄列表
[](https://github.com/onceupon/B...
env
[](https://github.com/onceupon/B...
Unset 環境變量 (e.g. unset 變量 'MYVAR')
unset MYVAR
[](https://github.com/onceupon/B...
lsblk
[](https://github.com/onceupon/B...
partprobe
[](https://github.com/onceupon/B... bin
ln -s /path/to/program /home/usr/bin
# 必須是程序的絕對路徑
[](https://github.com/onceupon/B...
hexdump -C filename.class
[](https://github.com/onceupon/B...
rsh node_name
[](https://github.com/onceupon/B...(佔用的網絡端口)
netstat -tulpn
[](https://github.com/onceupon/B...
readlink filename
[](https://github.com/onceupon/B... (e.g. python)
type python
# python 是 /usr/bin/python
# 這裏有5中不一樣的類型,使用 'type -f' 標誌進行檢查
# 1\. alias    (shell alias)
# 2\. function (shell function, 也會打印函數主體)
# 3\. builtin  (shell builtin)
# 4\. file     (disk file)
# 5\. keyword  (shell reserved word)

# 你也可使用 `which`
which python
# /usr/bin/python
[](https://github.com/onceupon/B...
declare -F
[](https://github.com/onceupon/B...
du -hs .

# or
du -sb
[](https://github.com/onceupon/B...
複製使用權限目錄
cp -rp /path/to/directory
[](https://github.com/onceupon/B...
pushd .

# then pop
popd

#或着使用dirs顯示當前所在目錄的列表。
dirs -l
[](https://github.com/onceupon/B...
df -h

# 或者
du -h

#或者
du -sk /var/log/* |sort -rn |head -10
[](https://github.com/onceupon/B...
runlevel
[](https://github.com/onceupon/B...
init 3

#或者
telinit 3
[](https://github.com/onceupon/B...
1\. edit /etc/init/rc-sysinit.conf
2. env DEFAULT_RUNLEVEL=2
[](https://github.com/onceupon/B... root 用戶
su
[](https://github.com/onceupon/B...
su somebody
[](https://github.com/onceupon/B...
repquota -auvs
[](https://github.com/onceupon/B...
getent database_name

# (e.g.  'passwd' 數據庫)
getent passwd
# 列出全部用戶賬戶(全部本地賬戶和LDAP)

# (e.g. 獲取用戶組列表)
getent group
# 在 'group' 數據庫保存
[](https://github.com/onceupon/B...
chown user_name filename
chown -R user_name /path/to/directory/
# 改變用戶組名稱
掛載和取消掛載
# 例如 掛載 /dev/sdb 到 /home/test
mount /dev/sdb /home/test

# 例如 取消掛載 /home/test
umount /home/test
[](https://github.com/onceupon/B...
mount
# 或者
df
[](https://github.com/onceupon/B...
cat /etc/passwd
[](https://github.com/onceupon/B...
getent passwd| awk '{FS="[:]"; print $1}'
[](https://github.com/onceupon/B...
compgen -u
[](https://github.com/onceupon/B...
compgen -g
[](https://github.com/onceupon/B...
group username
[](https://github.com/onceupon/B... uid,gid,用戶組
id username
[](https://github.com/onceupon/B... root
if [ $(id -u) -ne 0 ];then
    echo "You are not root!"
    exit;
fi
# 'id -u' output 0 if it's not root
[](https://github.com/onceupon/B... CPU 信息
more /proc/cpuinfo

# 或者
lscpu
[](https://github.com/onceupon/B... (例如磁盤軟大小限制: 120586240; 硬限制: 125829120)
setquota username 120586240 125829120 0 0 /home
[](https://github.com/onceupon/B...
quota -v username
[](https://github.com/onceupon/B...
ldconfig -p
[](https://github.com/onceupon/B... (例如 for 'ls')
ldd /bin/ls
[](https://github.com/onceupon/B...
lastlog
[](https://github.com/onceupon/B...
編輯全部用戶的路徑
joe /etc/environment
#編輯這個文件
[](https://github.com/onceupon/B...
ulimit -u
[](https://github.com/onceupon/B... TCP 鏈接
nmap -sT -O localhost
#notice that some companies might not like you using nmap
[](https://github.com/onceupon/B...
nproc --all
[](https://github.com/onceupon/B...
1\. top
2. press '1'
[](https://github.com/onceupon/B... PID
jobs -l
[](https://github.com/onceupon/B...
service --status-all
[](https://github.com/onceupon/B...
shutdown -r +5 "Server will restart in 5 minutes. Please save your work."
[](https://github.com/onceupon/B...
shutdown -c
[](https://github.com/onceupon/B...
wall -n hihi
[](https://github.com/onceupon/B...
pkill -U user_name
[](https://github.com/onceupon/B...
kill -9 $(ps aux | grep 'program_name' | awk '{print $2}')
[](https://github.com/onceupon/B...
在服務器上設置 gedit 首選項
# 你可能須要去安裝如下軟件:

apt-get install libglib2.0-bin;
# 或者
yum install dconf dconf-editor;
yum install dbus dbus-x11;

# 檢查列表
gsettings list-recursively

# 修改一些設置
gsettings set org.gnome.gedit.preferences.editor highlight-current-line true
gsettings set org.gnome.gedit.preferences.editor scheme 'cobalt'
gsettings set org.gnome.gedit.preferences.editor use-default-font false
gsettings set org.gnome.gedit.preferences.editor editor-font 'Cantarell Regular 12'
[](https://github.com/onceupon/B... (例如 把用戶名爲「nice」的永不添加到分組「docker」,這樣此用戶就能夠在不用 sudo 的狀況下運行 docker)
sudo gpasswd -a nice docker
[](https://github.com/onceupon/B... python 包
1\. pip 安裝 --用戶 package_name
2. 你可能須要將  ~/.local/bin/ 導出到  PATH: export PATH=$PATH:~/.local/bin/
[](https://github.com/onceupon/B... Linux內核 (當 /boot 幾乎滿的時候...)
1\. uname -a  #檢查當前內核,哪些是不能移除的
2\. sudo apt-get purge linux-image-X.X.X-X-generic  #替代掉舊的版本
[](https://github.com/onceupon/B...
sudo hostname your-new-name

#若是不起做用,也能夠:
hostnamectl set-hostname your-new-hostname
# 而後檢查:
hostnamectl
# 或者檢查 /etc/hostname

# 若是一直不工做....,編輯:
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-ensxxx
#增長 主機名="你的新主機名稱"
[](https://github.com/onceupon/B...
apt list --installed

# 或者在 Red Hat:
yum list installed
[](https://github.com/onceupon/B...
lsof /mnt/dir
[](https://github.com/onceupon/B...
killall pulseaudio
# 而後按下 Alt-F2 並輸入 pulseaudio
當聲音不工做的時候
killall pulseaudio
[](https://github.com/onceupon/B... SCSI 設備信息列表
lsscsi
[](https://github.com/onceupon/B... DNS 服務器教程

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B...

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B...

http://onceuponmine.blogspot....

[](https://github.com/onceupon/B... telnet 去測試開放的端口,測試是否能夠鏈接到服務器例如服務器(192.168.2.106) 端口(53)
telnet 192.168.2.106 53
[](https://github.com/onceupon/B... (mtu) (例如修改到9000)
ifconfig eth0 mtu 9000
[](https://github.com/onceupon/B... pid (例如 python)
pidof python

# 或者
ps aux|grep python
[](https://github.com/onceupon/B...
# Start ntp:
ntpd

# 檢查 ntp:
ntpq -p
[](https://github.com/onceupon/B...
sudo apt-get autoremove
sudo apt-get clean
sudo rm -rf ~/.cache/thumbnails/*

# 刪除舊的內核
sudo dpkg --list 'linux-image*'
sudo apt-get remove linux-image-OLDER_VERSION
[](https://github.com/onceupon/B... (根分區是 LVM 邏輯卷)
pvscan
lvextend -L +130G /dev/rhel/root -r
# 添加 -r 將在調整卷大小後增長到文件系統
[](https://github.com/onceupon/B...
建立UEFI可引導USB驅動器 (好比; /dev/sdc1)
sudo dd if=~/path/to/isofile.iso of=/dev/sdc1 oflag=direct bs=1048576
[](https://github.com/onceupon/B...
sudo dpkg -l | grep <package_name>
sudo dpkg --purge <package_name>
[](https://github.com/onceupon/B...
ssh -f -L 9000:targetservername:8088 root@192.168.14.72 -N
#-f: 在後臺運行; -L: 監聽; -N: 什麼也不作
# 你計算機的9000端口如今已經鏈接上了目標服務器名字192.168.14.72的8088端口
# 因此你如今能夠去打開瀏覽器輸入localhost:9000去查看你的目標計算機的8088端口了
[](https://github.com/onceupon/B... (好比: sublime_text)
#pidof 獲取進程id
pidof sublime_text

#pgrep, 你沒必要鍵入整個程序名
pgrep sublim

#pgrep, 若是找到進程,則返回1;若是沒有此類進程,則返回0

pgrep -q sublime_text && echo 1 || echo 0

#top, 須要更長的時間

top|grep sublime_text
[](https://github.com/onceupon/B...

aio-stress - AIO benchmark. bandwidth - memory bandwidth benchmark. bonnie++ - hard drive and file system performance benchmark. dbench - generate I/O workloads to either a filesystem or to a networked CIFS or NFS server. dnsperf - authorative and recursing DNS servers. filebench - model based file system workload generator. fio - I/O benchmark. fs_mark - synchronous/async file creation benchmark. httperf - measure web server performance. interbench - linux interactivity benchmark. ioblazer - multi-platform storage stack micro-benchmark. iozone - filesystem benchmark. iperf3 - measure TCP/UDP/SCTP performance. kcbench - kernel compile benchmark, compiles a kernel and measures the time it takes. lmbench - Suite of simple, portable benchmarks. netperf - measure network performance, test unidirectional throughput, and end-to-end latency. netpipe - network protocol independent performance evaluator. nfsometer - NFS performance framework. nuttcp - measure network performance. phoronix-test-suite - comprehensive automated testing and benchmarking platform. seeker - portable disk seek benchmark. siege - http load tester and benchmark. sockperf - network benchmarking utility over socket API. spew - measures I/O performance and/or generates I/O load. stress - workload generator for POSIX systems. sysbench - scriptable database and system performance benchmark. tiobench - threaded IO benchmark. unixbench - the original BYTE UNIX benchmark suite, provide a basic indicator of the performance of a Unix-like system. wrk - HTTP benchmark.

[](https://github.com/onceupon/B...
lastb
[](https://github.com/onceupon/B...,打印他們的信息
who
[](https://github.com/onceupon/B...
w
[](https://github.com/onceupon/B...
users
[](https://github.com/onceupon/B...
在終止程序上中止跟蹤一個文件
tail -f --pid=<PID> filename.txt
# 用程序的進程 ID 替換 <PID>
[](https://github.com/onceupon/B...
systemctl list-unit-files|grep enabled

[](https://github.com/onceupon/B...

[back to top]

[](https://github.com/onceupon/B...
lshw -json >report.json
# 其餘的選項: [ -html ]  [ -short ]  [ -xml ]  [ -json ]  [ -businfo ]  [ -sanitize ] ,etc
[](https://github.com/onceupon/B...
sudo dmidecode -t memory
[](https://github.com/onceupon/B... CPU 硬件細節
dmidecode -t 4
#          類型   信息
#          0   BIOS
#          1   系統
#          2   基板
#          3   機殼
#          4   處理器
#          5   內存控制器
#          6   內存模塊
#          7   緩存
#          8   端口鏈接器
#          9   系統槽
#         11   OEM 字符串
#         13   BIOS 語言
#         15   系統事件日誌
#         16   物理內存數組
#         17   存儲設備
#         18   32位內存錯誤
#         19   存儲映射地址
#         20   存儲設備映射地址
#         21   內置定位設備
#         22   便攜式電池
#         23   系統重置
#         24   硬件安全性
#         25   系統電源控制
#         26   電壓探頭
#         27   冷卻裝置
#         28   溫度探測器
#         29   電流探頭
#         30   待外遠程訪問
#         31   引導完整性服務
#         32   系統啓動
#         34   管理裝置
#         35   管理設備組件
#         36   管理設備閾值數據
#         37   內存通道
#         38   IPMI 設備
#         39   電力供應
[](https://github.com/onceupon/B...
lsscsi|grep SEAGATE|wc -l
# 或者
sg_map -i -x|grep SEAGATE|wc -l
[](https://github.com/onceupon/B...
或者硬盤的 UUID (例如 sdb)
blkid /dev/sdb
[](https://github.com/onceupon/B...
lsblk -io KNAME,TYPE,MODEL,VENDOR,SIZE,ROTA
#其中 ROTA 表示旋轉設備/旋轉硬盤 (1 爲真, 0 爲假)
[](https://github.com/onceupon/B... PCI (外圍設置互連) 設備
lspci
# 列出關於 NIC 信息
lspci | egrep -i --color 'network|ethernet'
[](https://github.com/onceupon/B... USB 設備
lsusb
[](https://github.com/onceupon/B... 模塊
# 顯示 Linux 內核中模塊狀態
lsmod

# 從 Linux 內核中增長或者移除模塊
modprobe

# 或者
# Remove a module
rmmod

# 插入模塊
insmod
[](https://github.com/onceupon/B... IPMI-enabled 設備 (e.g. BMC)
# 遠程查看服務器的電源狀態
ipmitool -U <bmc_username> -P <bmc_password> -I lanplus -H <bmc_ip_address> power status

# 遠程開啓服務器
ipmitool -U <bmc_username> -P <bmc_password> -I lanplus -H <bmc_ip_address> power on

# 打開面板識別燈(默認 15秒)
ipmitool chassis identify 255

#或者服務器傳感器溫度
ipmitool sensors |grep -i Temp

# 重置 BMC
ipmitool bmc reset cold

# Prnt BMC 網絡
ipmitool lan print 1

# 設置 BMC 網絡
ipmitool -I bmc lan set 1 ipaddr 192.168.0.55
ipmitool -I bmc lan set 1 netmask 255.255.255.0
ipmitool -I bmc lan set 1 defgw ipaddr 192.168.0.1

網絡

[回到頂部]

[](https://github.com/onceupon/B... IP 地址
ip a
[](https://github.com/onceupon/B...
ip r
[](https://github.com/onceupon/B... ARP 緩存 (ARP 緩存顯示你鏈接到的同一網絡設備的 MAC 地址)
ip n
[](https://github.com/onceupon/B... IP 地址 (重啓後重置) (例如 增長 192.168.140.3/24到 設備 eno16777736)
ip address add 192.168.140.3/24 dev eno16777736
[](https://github.com/onceupon/B...
sudo vi /etc/sysconfig/network-scripts/ifcfg-enoxxx
# 而後編輯字段: BOOTPROT, DEVICE, IPADDR, NETMASK, GATEWAY, DNS1 etc
[](https://github.com/onceupon/B... NetworkManager
sudo nmcli c reload
[](https://github.com/onceupon/B...
sudo systemctl restart network.service
[](https://github.com/onceupon/B... hostname, OS, kernal, architecture !
hostnamectl
[](https://github.com/onceupon/B... (一次設置全部臨時,靜態,漂亮的主機名)
hostnamectl set-hostname "mynode"

[](https://github.com/onceupon/B...

其餘

[回到頂部]

[](https://github.com/onceupon/B... 自動完成 (例如 當你輸入「dothis」,而後按下 「tab」,顯示「now tomorrow never」)

更多的案例

完成 -W  "now tomorrow never" dothis
# ~$ dothis  
# 從不     如今       明天
# 輸入「n」或者「t」以後,再次按「tab」 鍵以自動完成
[](https://github.com/onceupon/B... n 次(例如重複打印5次「hello world」)
printf 'hello world\n%.0s' {1..5}
[](https://github.com/onceupon/B... Base64 的字符串
echo test|base64
#dGVzdAo=
[](https://github.com/onceupon/B...
username=`echo -n "bashoneliner"`
[](https://github.com/onceupon/B...
dirname `pwd`
[](https://github.com/onceupon/B... (例如複製文件 A 到文件(B-D))
tee <fileA fileB fileC fileD >/dev/null
[](https://github.com/onceupon/B...
tr --delete '\n' <input.txt >output.txt
[](https://github.com/onceupon/B...
tr '\n' ' ' <filename
[](https://github.com/onceupon/B...
tr /a-z/ /A-Z/
[](https://github.com/onceupon/B... (例如 把 a-z 都轉換爲 a)
echo 'something' |tr a-z a
# aaaaaaaaa
[](https://github.com/onceupon/B... (例如 fileA, fileB)
diff fileA fileB
# a: 被增長; d:刪除; c:被修改

# 或者
sdiff fileA fileB
# 文件差別的並排合併
[](https://github.com/onceupon/B...
比較兩個文件, 刪除掉尾部回車(例如 fileA, fileB)
diff fileA fileB --strip-trailing-cr
[](https://github.com/onceupon/B... (例如給 fileA 編號)
nl fileA

#或者
nl -nrz fileA
# add leading zeros

#也能夠
nl -w1 -s ' '
# making it simple, blank separate
[](https://github.com/onceupon/B... tab 鍵按字段鏈接兩個文件 (默認鏈接按照文件的第一列鏈接, 默認分隔符是空格)
# 文件 A 和文件 B 應該有相同的行順序
join -t '\t' fileA fileB

# 使用指定字段加入 (例如 文件 A 的第三列和文件 B 的第五列)
join -1 3 -2 5 fileA fileB
[](https://github.com/onceupon/B... (例如 fileA, fileB, fileC)
paste fileA fileB fileC
# 默認選項分開
[](https://github.com/onceupon/B...
echo 12345| rev
[](https://github.com/onceupon/B... .gz 文件但不解壓
zmore filename

# 或者
zless filename
[](https://github.com/onceupon/B...,輸出錯誤文件
some_commands  &>log &

# 或者
some_commands 2>log &

# 或者
some_commands 2>&1| tee logfile

# 或者
some_commands |& tee logfile

# 還能夠
some_commands 2>&1 >>outfile
#0: 標準輸入; 1: 標準輸出; 2: 標準錯誤
[](https://github.com/onceupon/B...
# 按順序運行
(sleep 2; sleep 3) &

#並行運行
sleep 2 & sleep 3 &
[](https://github.com/onceupon/B...
即使註銷也能夠運行進程 (immune to hangups, with output to a non-tty)
# 例如即使註銷也會運行 myscript.sh 腳本
nohup bash myscript.sh
[](https://github.com/onceupon/B...
下面是郵件的內容 -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
# use -a flag to set send from (-a "From: some@mail.tld")
[](https://github.com/onceupon/B... .xls 轉換爲 csv
xls2csv filename
[](https://github.com/onceupon/B... (例如附加 hihi 內容到指定文件)
echo 'hihi' >>filename
[](https://github.com/onceupon/B... BEEP 的聲音
speaker-test -t sine -f 1000 -l1
[](https://github.com/onceupon/B... beep 聲音的持續時間
(speaker-test -t sine -f 1000) & pid=$!;sleep 0.1s;kill -9 $pid
[](https://github.com/onceupon/B... 編輯/刪除
~/.bash_history

#或者
history -d [line_number]
[](https://github.com/onceupon/B...
# list 5 previous command (similar to `history |tail -n 5` but wont print the history command itself)
fc -l -5
[](https://github.com/onceupon/B...
head !$
[](https://github.com/onceupon/B...:還挺好使)
clear

# 或者
Ctrl+l
[](https://github.com/onceupon/B...
cat /directory/to/file
echo 100>!$
[](https://github.com/onceupon/B... .xf
unxz filename.tar.xz
# 而後
tar -xf filename.tar
[](https://github.com/onceupon/B... python 包
pip install packagename
[](https://github.com/onceupon/B... bash 命令
Ctrl+U

# 或者
Ctrl+C

# 或者
Alt+Shift+#
# 成爲歷史
[](https://github.com/onceupon/B...
向歷史添加一些東西 (例如 「addmetohistory」)
# addmetodistory
# just add a "#" before~~
[](https://github.com/onceupon/B...
sleep 5;echo hi
[](https://github.com/onceupon/B... rsync 備份
rsync -av filename filename.bak
rsync -av directory directory.bak
rsync -av --ignore_existing directory/ directory.bak
rsync -av --update directory directory.bak

rsync -av directory user@ip_address:/path/to/directory.bak
#跳過接收器上更新的文件 (我更喜歡這個!)
[](https://github.com/onceupon/B...
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
# -p: 設置爲父目錄
# 這將製造 project/doc/html/; project/doc/info; project/lib/ext ,etc
[](https://github.com/onceupon/B...
cd tmp/ && tar xvf ~/a.tar
[](https://github.com/onceupon/B...
cd tmp/a/b/c ||mkdir -p tmp/a/b/c
[](https://github.com/onceupon/B...
tar xvf -C /path/to/directory filename.gz
[](https://github.com/onceupon/B... "" 來中斷長命令
cd tmp/a/b/c \
> || \
>mkdir -p tmp/a/b/c
[](https://github.com/onceupon/B... pwd
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
# PWD 必須是大寫字母
[](https://github.com/onceupon/B... (e.g. /tmp/)
file /tmp/
# tmp/: directory
[](https://github.com/onceupon/B... 腳本
#!/bin/bash
file=${1#*.}
# remove string before a "."
[](https://github.com/onceupon/B... 簡單的 HTTP 服務
python -m SimpleHTTPServer
# 或者你使用 python3 的時候:
python3 -m http.server
讀取用戶輸入
read input
echo $input
[](https://github.com/onceupon/B... 1-10
seq 10
[](https://github.com/onceupon/B...
i=`wc -l filename|cut -d ' ' -f1`; cat filename| echo "scale=2;(`paste -sd+`)/"$i|bc
[](https://github.com/onceupon/B... (e.g. 1,2)
echo {1,2}{1,2}
# 1 1, 1 2, 2 1, 2 2
[](https://github.com/onceupon/B... (e.g. A,T,C,G)
set = {A,T,C,G}
group= 5
for ((i=0; i<$group; i++));do
    repetition=$set$repetition;done
    bash -c "echo "$repetition""
[](https://github.com/onceupon/B...
foo=$(<test1)
[](https://github.com/onceupon/B...
echo ${#foo}
[](https://github.com/onceupon/B...
echo -e ' \t '
[](https://github.com/onceupon/B...
declare -a array=()

# 或者
declare array=()

# 或者關聯數組
declare -A array=()
[](https://github.com/onceupon/B...
scp -r directoryname user@ip:/path/to/send
[](https://github.com/onceupon/B...
# 按行分割 (e.g. 1000 lines/smallfile)
split -d -l 1000 largefile.txt

# 按字節分割而不會在文件間斷行
split -C 10 largefile.txt
[](https://github.com/onceupon/B... (e.g 100000 個文件, 10 字節分割):
#1\. 建立文件
dd if=/dev/zero of=bigfile bs=1 count=1000000

#2\. 將大文件拆分爲100000個10字節文件
 split -b 10 -a 10 bigfile
[](https://github.com/onceupon/B... (e.g. 將全部文件重命名爲ABC)
rename 's/ABC//' *.gz
[](https://github.com/onceupon/B... 刪除filename.gz的.gz)
basename filename.gz .gz

zcat filename.gz> $(basename filename.gz .gz).unpacked
[](https://github.com/onceupon/B...
叉炸彈(危險命令的意思)
# 不要在家嘗試這個
# 它是一個每次調用都會調用兩次的函數,直到系統資源耗盡爲止
# 爲了安全起見在前面加了一個 #,當你真正測試的時候,請移除它
# :(){:|:&};:
[](https://github.com/onceupon/B... .txt)
rename s/$/.txt/ *
# 你可使用重命名 -n s/$/.txt/ * 首先去檢查結果,若是它僅僅打印這些:
# rename(a, a.txt)
# rename(b, b.txt)
# rename(c, c.txt)
[](https://github.com/onceupon/B... (例如 /t/t --> /t)
tr -s "/t" < filename
[](https://github.com/onceupon/B... echo 打印 nextline
echo -e 'text here \c'
[](https://github.com/onceupon/B...
!$
[](https://github.com/onceupon/B...
echo $?
[](https://github.com/onceupon/B...
head -c 50 file
[](https://github.com/onceupon/B...
# 例如
# AAAA
# BBBB
# CCCC
# DDDD
cat filename|paste - -
# AAAABBBB
# CCCCDDDD
cat filename|paste - - - -
# AAAABBBBCCCCDDDD
[](https://github.com/onceupon/B... 轉 fasta
cat file.fastq | paste - - - - | sed 's/^@/>/g'| cut -f1-2 | tr '\t' '\n' >file.fa
[](https://github.com/onceupon/B...
cat file|rev | cut -d/ -f1 | rev
[](https://github.com/onceupon/B... i++ 中添加一個變數字量(例如 $val)
((var++))
# 或者
var=$((var+1))
[](https://github.com/onceupon/B... (好比 filename)
>filename
[](https://github.com/onceupon/B... tar.bz2 文件 (例如解壓文件 file.tar.bz2)
tar xvfj file.tar.bz2
[](https://github.com/onceupon/B... tar.xz 文件 (例如解壓 file.tar.xz)
unxz file.tar.xz
tar xopf file.tar
[](https://github.com/onceupon/B... y/n 直到終止
# 'y':
yes

# 或者 'n':
yes n

# 或者 'anything':
yes anything

# 例如:

yes | rm -r large_directory

##### [](https://github.com/onceupon/Bash-OneLiner#create-dummy-file-of-certain-size-instantly-eg-200mb)



##### 當即建立必定大小的虛擬文件 (例如 200mb)

dd if=/dev/zero of=//dev/shm/200m bs=1024k count=200

或者

dd if=/dev/zero of=//dev/shm/200m bs=1M count=200

標準輸出:

200+0 條記錄

200+0 記錄出來

209715200 bytes (210 MB) copied, 0.0955679 s, 2.2 GB/s

##### [](https://github.com/onceupon/Bash-OneLiner#cat-to-a-file)把文件歸檔

cat >myfile
讓我在這補充一下
exit by control + c
^C

##### [](https://github.com/onceupon/Bash-OneLiner#keep-repeatedly-executing-the-same-command-eg-repeat-wc--l-filename-every-1-second)保持重複執行同一個命令 (例如 每一秒重複一次  'wc -l filename' )

watch -n 1 wc -l filename

##### [](https://github.com/onceupon/Bash-OneLiner#print-commands-and-their-arguments-when-execute-eg-echo-expr-10--20-)執行時打印命令以及其參數(例如 echo `expr 10 + 20 `)

set -x; echo expr 10 + 20

##### [](https://github.com/onceupon/Bash-OneLiner#print-some-meaningful-sentences-to-you-install-fortune-first)爲你打印一些有意義的句子(首先安裝 fortune )

fortune

##### [](https://github.com/onceupon/Bash-OneLiner#colorful-and-useful-version-of-top-install-htop-first)豐富多彩的 (有用的) top 版本(首先安裝 htop)

htop

##### [](https://github.com/onceupon/Bash-OneLiner#press-any-key-to-continue)按任意鍵繼續

read -rsp $'Press any key to continue...n' -n1 key

##### [](https://github.com/onceupon/Bash-OneLiner#run-sql-like-command-on-files-from-terminal)從終端中運行相似 sql 的命令

下載:

https://github.com/harelba/q

例如:

q -d "," "select c3,c4,c5 from /path/to/file.txt where c3='foo' and c5='boo'"

##### [](https://github.com/onceupon/Bash-OneLiner#using-screen-for-multiple-terminal-sessions)將 Screen 用於多個終端回話

建立會話並附加:

screen

建立分離的會話 foo:

screen -S foo -d -m

獨立會話 foo:

screen: ^a^d

會話列表:

screen -ls

附加到上一個會話:

screen -r

附加到會話 foo:

screen -r foo

殺掉會話 foo:

screen -r foo -X quit

滾動:

點擊屏幕前綴組合 (C-a / control+A),而後按下 Escape.
上下移動方向鍵(↑ and ↓).

重定向屏幕中已經運行進程的輸出:

(C-a / control+A), then hit 'H'

存儲屏幕的屏幕實處:

Ctrl+A, Shift+H

而後再當前的目錄下找到 screen.log 文件

##### [](https://github.com/onceupon/Bash-OneLiner#using-tmux-for-multiple-terminal-sessions)使用 


##### 將 Tmux用於多個終端回話

建立會話並附加:

tmux

附加到會話 foo:

tmux attach -t foo

分離的會話 foo:

^bd

會話列表:

tmux ls

附加上一個會話:

tmux attach

殺死會話 foo:

tmux kill-session -t foo

建立獨立會話 foo:

tmux new -s foo -d

將命令發送到 tmux 的全部窗格:

Ctrl-B
:setw synchronize-panes

一些 tmux 窗格控制的命令:

Ctrl-B

窗格 (分割), 按下 Ctrl+B,而後輸入如下字符:

% horizontal split

" vertical split

o swap panes

q show pane numbers

x kill pane

空間 - 在佈局之間進行切換

垂直分佈 (行):

select-layout even-vertical

或者

Ctrl+b, Alt+2

垂直分佈 (列):

select-layout even-horizontal

或者

Ctrl+b, Alt+1

Scroll

Ctrl-b 而後 [ 而後你可使用你的正常方向鍵來滾動
Press q to quit scroll mode.

##### [](https://github.com/onceupon/Bash-OneLiner#cut-the-last-column)剪切最後一行

cat filename|rev|cut -f1|rev

##### [](https://github.com/onceupon/Bash-OneLiner#pass-password-to-ssh)將密碼傳輸給 ssh

sshpass -p mypassword ssh root@10.102.14.88 "df -h"

##### [](https://github.com/onceupon/Bash-OneLiner#wait-for-a-pid-job-to-complete)等待一個  pid (任務)完成

wait %1

或者

wait $PID
wait ${!}

wait ${!} 要等待最後一個後臺進程 ($! 爲最後一個後臺進程的 PIID)

##### [](https://github.com/onceupon/Bash-OneLiner#convert-pdf-to-txt)將 pdf 轉換爲 txt

sudo apt-get install poppler-utils
pdftotext example.pdf example.txt

##### [](https://github.com/onceupon/Bash-OneLiner#list-only-directory)只列出目錄

ls -ld -- */

##### [](https://github.com/onceupon/Bash-OneLiner#capturerecordsave-terminal-output-capture-everything-you-type-and-output)捕獲/j記錄/保存終端輸出(捕獲你輸入和輸出的全部內容)

script output.txt

開始使用終端l

退出屏幕會話 (中止保存內容), 退出.

##### 以樹狀格式列出目錄的內容。

tree

轉到要列出的目錄,而後鍵入 tree (sudo apt-get install tree)

output:

home/

└── project

├── 1

├── 2

├── 3

├── 4

└── 5

設置目錄深度等級(例如1級)

tree -L 1

home/

└── project

##### [](https://github.com/onceupon/Bash-OneLiner#set-up-virtualenvsandbox-for-python)爲 Python設置 virtualenv(sandbox)

1. 安裝 virtualenv.

sudo apt-get install virtualenv

2. 爲新的隔離環境創建目錄(將其命名爲 .venv 或您想要的任何名稱)。

virtualenv .venv

3. 導入 virtual 執行目錄

source .venv/bin/activate

4. 您能夠檢查一下是否如今在沙盒中

type pip

5. 如今您能夠安裝您的 pip 包, 這裏的 requirements.txt 只是一個包含您想要的全部軟件包的 txt 文件 (例如 tornado==4.5.3)。

pip install -r requirements.txt

##### [](https://github.com/onceupon/Bash-OneLiner#working-with-json-data)使用 json 數據

安裝很是有用的 jq 包

sudo apt-get install jq

e.g. to get all the values of the 'url' key, simply pipe the json to the following jq command(you can use .[]. to select inner json, i.e jq '.[].url')

例如,要獲取 url 鍵的全部值,只需將 json 經過管道傳遞給如下 jq 命令(您可使用 [] 選擇內部 json,即 jq '[].url')

jq '.url'

##### [](https://github.com/onceupon/Bash-OneLiner#editing-your-history)編輯 history

history -w
vi ~/.bash_history
history -r

##### [](https://github.com/onceupon/Bash-OneLiner#decimal-to-binary-eg-get-binary-of-5)十進制轉換爲二進制(例如,獲取 5 的二進制 )

D2B=({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1})
echo -e ${D2B[5]}

00000101

echo -e ${D2B[255]}

11111111

##### [](https://github.com/onceupon/Bash-OneLiner#wrap-each-input-line-to-fit-in-specified-width-eg-4-integers-per-line)把輸入的行換行以適應指定的寬度(例如每行 4 個整數)

echo "00110010101110001101" | fold -w4

0011

0010

1011

1000

1101

##### [](https://github.com/onceupon/Bash-OneLiner#sort-a-file-by-column-and-keep-the-original-order)按列對文件進行排序,並保持原始順序

sort -k3,3 -s

##### [](https://github.com/onceupon/Bash-OneLiner#right-align-a-column-right-align-the-2nd-column)列右對齊(第二列右對齊)

cat file.txt|rev|column -t|rev

##### [](https://github.com/onceupon/Bash-OneLiner#to-both-view-and-store-the-output)查看和存儲輸出

echo 'hihihihi' | tee outputfile.txt

tee 帶 「-a」 能夠附加到文件中

##### [](https://github.com/onceupon/Bash-OneLiner#show-non-printing-ctrl-characters-with-cat)使用 cat 顯示非打印(Ctrl)字符

cat -v filename

##### [](https://github.com/onceupon/Bash-OneLiner#convert-tab-to-space)將製表符轉換爲空格

expand filename

##### [](https://github.com/onceupon/Bash-OneLiner#convert-space-to-tab)將空格轉換爲製表符

unexpand filename

##### [](https://github.com/onceupon/Bash-OneLiner#display-file-in-octal--you-can-also-use-od-to-display-hexadecimal-decimal-etc)以八進制顯示文件(您也可使用 od 顯示十六進制,十進制等)

od filename

##### [](https://github.com/onceupon/Bash-OneLiner#reverse-cat-a-file)反轉 `cat` 的結果

tac filename

##### [](https://github.com/onceupon/Bash-OneLiner#reverse-the-result-from-uniq--c)反轉 `uniq -c` 的結果

while read a b; do yes $b |head -n $a ;done <test.txt

> 將來還有更多!
相關文章
相關標籤/搜索