Diff:
echo “side-by-side display”
diff -y a.txt b.txt

echo “ignore case”
diff -yi a.txt b.txt

echo “ignore whitespace”
diff -yiw a.txt b.txt

echo “supress same line”
diff -yiw –suppress-common-line a.txt b.txt

内核版本
uname -r

ubuntu 版本号与名字
cat /etc/issue

 

useradd -m -G adm,root shock
-m 创建主目录
adduser shock sudo
把用户shock添加到sudo组
or visudo
add: shock ALL=(ALL) ALL (after root ALL=(ALL) ALL)

rsync -aXS /var/lib/docker/. /data/docker/ 同步文件

#! /bin/sh
get file dir:
sh file: LOCAL_DIR=$(cd $(dirname $0); pwd) (这个值不因为当前执行目录的改变而改变)
conf file: set $LOCAL_DIR = $(cd $(dirname $0); pwd)
rsync -e ‘ssh -p 30000’ -avl –delete –stats –progress –exclude ‘sources’ –exclude ‘public_html/database.txt’ demo@123.45.67.890:/home/demo /backup/

 

find . -type f -exec sed -i.orig ‘s/\t/ /g’ {} +

KEYWORD=iperf3
CMD=”iperf3 -s -p 5201 -d >> iperf3daemon.log 2>&1″
CMD2=”echo more than 1 ${KEYWORD} found”
/bin/sleep 10
/usr/bin/killall ${KEYWORD}
/bin/sleep 1
/usr/bin/killall -9 ${KEYWORD}
/bin/sleep 1
if [ `ps -C ${KEYWORD} | wc -l` -ge “1” ] ; then
if [ `ps -C ${KEYWORD} | wc -l` -gt “1” ] ; then
if [ `ps -C ${KEYWORD} | wc -l` = “1” ] ; then
${CMD}
else
${CMD2}
fi

 

pkill -f xxx.py

find . -type f -exec ls -l {} \;
find ./ -name “*.mp4” -exec ffprobe {} 2>&1 \;|grep h264|wc -l

ps aux | grep ffmpeg | awk ‘{print $2}’ | xargs kill -9

 

last command:
arrow up
!!
!-1
Ctrl-P

history to list all histor comands, then “!10” to execute the command with index 10
!ps to run the history command starting with “ps”

inherit the last parameters from last command:
vim !!:$
or specify the index (from 1) of parameter
vim !!:1

 

make with 8 threads:
make -j8

#auto-start app for ubuntu
sudo update-rc.d -f slapd remove

#check file size
du -sh FILE

# check the status of specific port, lsof stands for ls open files
sudo lsof -i:80
lsof -u shock
lsof -c /bin/vim # show all files opened by vim

# check status of all port
sudo lsof -Pn

# check all the connection
netstat -an

# check connection to specific IP
sudo lsof -Pn|grep IP
netstat -P (linux) or netstat -o (windows)

# copy
cp tmp.txt{,.bak}

# set hidden flag
chflags hidden FILE
chflags nohidden FILE

 

清单 7. 查找大于 10MB 的所有文件

$ find / -size +10000k –xdev –exec ls –lh {}\;

 

# find file with name
find ./ -name “*.cc”
find ./ -name “*.cc” -exec ls {} \; (\; is required at the end of the command line)

 

# grep recursively
grep “WORD” -r ./

grep -c “John” ./ (count number)
grep -v “John” ./ (does not contain John)

“:” as delimiter, keyword is the colum 3 as a integer (index start from 0)
sort -t: -u -k 3n /etc/passwrd
sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
# rank the output from shell
cat t.tmp | grep “^17” | sort -k 3 (ordered by colum 3) -n (odered by numberic) -u (no duplicate)

 

ls -l | awk ‘$6 == “Dec”‘
ls -l | awk ‘$5 = 686′

cut -d: -f 1,3 /etc/passwd
cut -d: -f 1-3 /etc/passwd (first 1-8 chars)

sed -i -e “s;/mysite/v2/user/;/user/;g” *.html

 

echo “side-by-side display”
diff -y a.txt b.txt

echo “ignore case”
diff -yi a.txt b.txt

echo “ignore whitespace”
diff -yiw a.txt b.txt

echo “supress same line”
diff -yiw –suppress-common-line a.txt b.txt

 

tar cvf a.tar ./a/
tar xvf a.tar
zip a.zip ./a/
unzip a.zip

# set python with port
port select –list python
port select -set python python 27

# shell hint message, in /etc/bashrc
PS1=’\h:\W \u\$’
PS2=”continue-> ”

#check the user of the directory or file
fuser -v .
fuser -vm tunnel.sh
ps auxf # show the process tree
ps U shock uxf

 

vmstat 1 100 #show system performance

nohup CMD & #run the command after logout
or nohup sh CMD > custom-out.log &

uname -a #show system info
df
du
# create account
sudo useradd -s /bin/bash -mr <USERNAME>
sudo adduser <USERNAME> sudo

type which whereis locate

 

grep -E ‘aaa|bbbb’

crontab时间格式范例
1-3表示123
1-9/2表示13579
crontab范例
每五分钟执行 */5 * * * *

每小时执行 0 * * * *

每天执行 0 0 * * *

每周执行 0 0 * * 0

每月执行 0 0 1 * *

每年执行 0 0 1 1 *SRC=/data/wwwroot/web/ #代码发布服务器目录

DST=/data/wwwroot/web/ #目标服务器目录
IP=”192.168.20.7″ #目标服务器IP,多个以空格隔开
USER=www
INOTIFY_EXCLUDE=”–fromfile /data/conf/shell/inotify_exclude.list”
RSYNC_EXCLUDE=”–include-from=/data/conf/shell/rsync_include.list –exclude-from=/data/conf/shell/rsync_exclude.list”

#su – $USER
inotifywait -mrq –exclude “(.swp|.inc|.svn|.rar|.tar.gz|.gz|.txt|.zip|.bak)” -e modify,delete,create,close_write,attrib $INOTIFY_EXCLUDE | while read D E F
do
for i in $IP
do
/usr/bin/rsync -e ‘ssh -p 5000’ -ahqzt $RSYNC_EXCLUDE –delete $SRC $USER@$i:$DST
done
done

 

inotify_exclude.list:
/tmp/inotify-test-dir
@/tmp/inotify-test-dir/cache

 

shell command:
#! /bin/sh
FILE_PATH=file.txt
DIR_PATH=dir
STR=0

if type python 2>/dev/null;
then
echo “yes”
else
echo “no”
fi

if [ -a ${FILE_PATH} ] ; then
echo “file: ${FILE_PATH} exists”
else
echo “file: ${FILE_PATH} does not exist”
fi

if [ -d ${DIR_PATH} ] ; then
echo “file: ${DIR_PATH} exists”
else
echo “file: ${DIR_PATH} does not exist”
fi

 

if [ “${STR}”x != “0”x ] ;
then
echo “not 0”
else
echo “be 0”
fi

 

ls

if [ ! $? -eq 0 ]; then
echo “last command executed successfully”
fi

function func_cmp() {
if [ $1 -eq 0 ] ; then
echo “equal 0”
else
echo “not equal 0″
fi
}

function func_add() {
a1=1
a2=10
a3=100
a4=-1
r1=$[$a1+-1]
r2=$[$a2+$a2]
r3=$[$a3+$a4]

echo $r1, $r2, $r3
}

function split_str() {
echo $1
tail -n 2 $1 | while read LINE
do
echo $LINE
done

OLD_IFS=”$IFS”
IFS=” ”
arr=($LINE)
IFS=”$OLD_IFS”
}