vim


set expandtab
set tabstop=4
set shiftwidth=4
set autoindent
set smartindent
set nu

git config credential.helper store

linux


adduser: 会自动为创建的用户指定主目录、系统shell版本,会在创建时输入用户密码。

useradd:需要使用参数选项指定上述基本设置,如果不使用任何参数,则创建的用户无密码、无主目录、没有指定shell版本。

visudo
shock ALL=(ALL:ALL) ALL

 

#! /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 # if multiple key is used, -k start,end must be used
# rank the output from shell
cat t.tmp | grep “^17” | sort -k 3 (ordered by colum 3) -n (odered by numberic) -u (no duplicate)

 

cut -f 2,3 -d: /etc/passwd

*:254
*:257
*:258

 

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 &

 

xargs:

covert all webp files to png:

find ./ -name “*.webp” | sed ‘s/.webp$//g’ | xargs -i -t -p dwebp {}.webp -o {}.png

上面命令中xargs参数的说明:
-i 表示使用在xargs中执行命令中,使用{}表示传入的参数

-t 表示将xargs后面执行命令的结果显示到标准输出设备,也就是输出到屏幕

-p 表示每次执行的命令都需要确认

 

find:

find ./ -name “*.webp” -exec rm -rf “{}”\;

find ./ -name “*.webp” | xargs rm -rf

alias gcpp=’find ./ -name “*.cpp” -o -name “*.h” | xargs grep -n’

alias gpy=’find ./ -name “*.py” | xargs grep -n’

 

 

python


conda create envrionment

conda create -n py27-tf11 -m python=3.5 tensorflow=1.1.0 opencv3=3.1.0 -c https://conda.anaconda.org/menpo  –clone

pip install –upgrade –ignore-installed tensorflow-gpu # which install latest tensorflow-gpu

conda install -c https://conda.anaconda.org/menpo opencv3

conda search tensorflow # for different versions

conda env remove -n EVN_NAME

conda env list

conda create --name myclone --clone myenv

or:
python -m pip install opencv-python
python -m pip install opencv-contrib-python

 

Shell


ddiff: compare file list in two directories (but not the file content, use diff/icdiff instead in this case)

if [ $# -lt 2 ]; then
echo “2 parameters are required: $#”
else
p1=$(echo $1 | sed ‘s:/*$::’) # remove trailing slashes
p2=$(echo $2 | sed ‘s:/*$::’)

rid=”$RANDOM”
echo “rid: $rid”
f1=$(echo $p1 | sed ‘s;/;-;g’) # replace all slashes to –
f2=$(echo $p2 | sed ‘s;/;-;g’)
f1=”/tmp/$f1-$rid.list”
f2=”/tmp/$f2-$rid.list”

ls -R $p1 > $f1
ls -R $p2 > $f2
diff -W $(( $(tput cols) – 2 )) $f1 $f2 –side-by-side
echo “——————————”
echo “tmp1: $f1”
echo “tmp2: $f2”

fi


put this to .bashrc:

run() {
number=$1
shift
for i in `seq $number`; do
$@
done
}