文章转发自专业的Laravel开发者社区,原始链接: https://learnku.com/laravel/t...
Bash 集合
我很高兴你在这里!几年前我从事生物信息学方面的研究工作。对那些简单的 bash 命令感到惊讶,他们比我的枯燥脚本快很多。通过学习命令行的快捷方式和脚本帮助我节省了很多时间。近年来,我从事云计算相关的工作,并在这里继续记录那些有用的命令。并且我在努力的使他们简短而且迅速。我主要使用 Ubuntu,RedHat ,Linux Mint 以及 CentOS 系统,如果命令在您的系统上不生效,那么我很抱歉。
该博客将重点介绍我从工作以及 LPIC 的考试中获得的用于解析数据和 Linux 系统维护的简单命令,但是他们可能来自于亲爱的 Google 和 Stackoverflow。
英语和 bash 并不是我的母语,请随时纠正我,谢谢。如果你知道其他有趣的命令,请教教我。
这是更新潮的版本Bash-Oneliner~
[](https://github.com/onceupon/B... bash 一行命令
- 终端技巧
- 变量
- Grep命令
- Sed命令
- Awk命令
- Xargs命令
- Find命令
- 条件和循环
- 数学
- 时间
- 下载
- 随机
- Xwindow工具
- 系统
- 硬件
- 联网
- 其他
[](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 当前主机名
筛选
[返回顶部]
[](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/*
流编辑器
[返回顶部]
[](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'
[](https://github.com/onceupon/B...
sed 's=/=\\/=g'
[](https://github.com/onceupon/B... (例如: A-1-e
、 A-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
[返回顶部]
[](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
[回到顶部]
[](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 (条件与循环)
[返回顶部]
[](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
[](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> # 按 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 -f
[](https://github.com/onceupon/B...
# 僵尸进程已经死了,所以你不能杀死它。您可以消除通过杀死其父进程。
# 首先,找到僵尸进程的PID
ps aux| grep 'Z'
# 接下来发现僵尸的父进程的PID
pstree -p -s
# 然后你可以杀死它的父进程,你会发现僵尸进程已经不见了。
sudo kill 9
[](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
sudo dpkg --purge
[](https://github.com/onceupon/B...
ssh -f -L 9000:targetservername:8088 [email protected] -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= filename.txt
# 用程序的进程 ID 替换
[](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 -P -I lanplus -H power status
# 远程开启服务器
ipmitool -U -P -I lanplus -H 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 /dev/null
[](https://github.com/onceupon/B...
tr --delete '\n' output.txt
[](https://github.com/onceupon/B...
tr '\n' ' '
[](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' [email protected]
# use -a flag to set send from (-a "From: [email protected]")
[](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=$(
[](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 [email protected] "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
> 未来还有更多!