Linux Notes
Linux bash commond
Typing Commond
# delete the content from current cursor back to the start
ctrl u
# kill to the end of the line
ctrl k
# search through command history
ctrl r
# move cursor to beginning of line
ctrl a
# move cursor to end of line
ctrl e
# stop process
ctrl z
# terminate process
ctrl c
# new terminal page
ctrl l
# sudo excute last command
sudo !!
# multiple command
';' '&'
man command
curl cht.sh/command
# back to the previous working directory
cd -
# insert args
echo a | xargs -I{} echo "d{}d"
# grep multiple lines
# -A5 after 5 lines
# -B before 5 lines
grep -A5 'eno'
grep -B5 'eno'
# show timestamp or datetime
date +%s
date -u +%Y-%m-%d\ %H:%M:%S
File Content
# rename
$ mv a b
# copy dir
# get /home/yuan/hejtao
cp -a /home/hejtao /home/yuan
# copy file under dir
cp -a /home/hejtao/* /home/yuan
# rm all content under hejtao
rm -rf /home/hejtao/*
# rm dir
# -r recurse -f force
rm -rf /home/hejtao
rm -rf /home/hejtao/
# overide with line number
cat -n a.txt > b.txt
# append
cat a.txt >> b.txt
# remote copy
scp ./exe root@10.10.0.1:/root/
rsync -e "ssh -i ~/.ssh/priKey.pem" -a ./exe root@10.10.0.243:/root/
File Permission
# change ownership
# -R Recursive
chown -R $(whoami) /usr/local/Cellar
chown root:hejtao file.txt
chown hejtao:hejao file.txt
# r=4(可读),w=2(可写),x=1(可执行)
# u(user or owner) g(group) o(other)
# only owner can read
chmod 400 file.txt
# user can read write
chmod u=rw file.txt
# user also can excute
chmod u+x file.txt
chmod +x file.txt
# Set-User-ID, anyone access the file as if the owner
chmod u+s file.txt
Show File
# show row count of a file
wc -l file.txt
# show last 1000 rows
tail -n 1000 file.txt
# show [1000, inf)
tail -n +1000 file.txt
# show [1, 1000]
head -n 1000 file.txt
# show [56, 65]
head -n 65 file.txt | tail -n +56
# show 10 most frequent IP from log
awk '{ print $1}' access.log | uniq -c | sort -nr | head -n 10
# disable row wrap, exit with q button
less -S
#
less +F
Search File
# search current dir for regular file updated during 10 min ago
find ./ -type f -mmin -10
# search file with size less than 1M
find ./ -size -1M
# insensitive match
find ./ -iname '*.py' -ls
# search dir
find ./ -type d -not -iname '*z'
Network and Device
dmidecode -t memory
lscpu
du -hs /var/lib/docker/* | sort -nr
du -sh *
# unix name
uname -a
fdisk -l
df -h
# network interface
ifconfig
# list tcp on listening port
netstat -lntp
# list network routing
netstat -rn
# show fingerprint
ssh-keygen -l -f ~/.ssh/id_rsa
# gen rsa key pair in pem format
ssh-keygen -t rsa -m PEM -C hejtao@outlook.com
# append id_rsa.pub to known_hosts of remote server
ssh-copy-id -fo "IdentityFile ./.ssh/priKey.pem" ubuntu@69.231.139.220
dig
route -L
# insert rule at line 1, the rule accept all traffic from the wg0 network interface
iptables -I INPUT 1 -i wg0 -j ACCEPT
lsof -i:3306
lsof -i4 | grep -i 'listen'
# add route
networksetup -setadditionalroutes vpn_name target_ip mask vpn_server_ip
networksetup -setadditionalroutes pi 1.1.1.1 255.255.255.0 12.12.12.12
networksetup -setadditionalroutes pi 1.1.1.1 255.255.255.0 12.12.12.12 2.2.2.2 255.255.255.0 12.12.12.12
# proxy
ssh -D 1080 -R 1080:localhost:1080 root@1.1.1.1
Process
# search process name contains ipfs
ps -aux | grep ipfs
jobs
bg 1
fg 1
kill %2
kill %
Vim
Basic
:w save
:q quit
:q! not save and quit
:qa! 强行退出所有文件
ZQ not save and quit
ZZ :wq save and quit
dd copy and delete current row
yy copy current row
p paste on next row
P paste on current row
Ctrl r redo
u undo
x delete current character
:e <path/to/file> open file
:saveas <path/to/file> save as
:bn :bp swich to next or previous openning file
Move cusor
gg jump to first not blank charactor of top row
G jump to first not blank charactor of bottom row
V Visual
ggVG select all d delete selected y copy selected
h left
j down
k up
l right
0 row head
^ jump to the first not blank character of current row
$ row end
g_ jump to the last not blank character of current row
/<pattern> search txt matching pattern, press ’n’ for the next result
w jump forward a word
b jump back a word
Ctrl f new page view
Ctrl b back page view
Insert
a insert at after cusor
o insert at next row
O insert at current row
cw replace from current position to the word end
Window
:split filename herizontally split
:new filename herizontally split
:vsplit filename vertically split
ctrl + w switch among windows
ctrl + <arrow key> switch to window in arrow directon
Tmux
tmux ls
tmux new -s <session-name>
# detatch session
Ctrl b d
# attatch session
tmux attach -t <session-name>
tmux a -t <session-name>
tmux kill-session -t <session-name>
tmux switch -t <session-name>
# auto script
'cd ~/go/src/app && tmux new -d -s app && tmux send-keys -t app.0 "go run main.go" ENTER'
ssh -i ~/.ssh/remote.pem root@12.12.12.12 "tmux send-keys -t pool.0 C-c && tmux send-keys -t pool.0 './exe' ENTER"
SQL
UPDATE account SET grade = replace(grade,'級','级');
-- tabs
char(9)
-- line break
char(10)
-- enter
char(13)
-- rm enter
UPDATE account SET grade = replace(grade, char(13), '') WHERE grade REGEXP char(13);
-- time concat
LEFT(CAST(NOW() AS CHAR), 18)
DATE_FORMAT(NOW(),'%Y-%m-%d')
-- start with one of 'aeiou' or end with 'ok'
SELECT name FROM person WHERE name REGEXP '^[aeiou]|ok$'; -- aeiou开头,或ok结尾
SET @@global.max_allowed_packet = 1024*1024*1024;
SET @@global.sql_mode ='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
SET @@sql_mode ='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SELECT * FROM performance_schema.data_locks\G;
Ssh
# 登陆服务器
spawn ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=10 -i ~/.ssh/login.pem -p 36366 root@服务器IP
expect "*passphrase*"
send "xxx\r"
interact
expect eof
# 通过Jumpserver堡垒机
spawn ssh -p 2222 user@堡垒机IP
expect "*password*"
send "xxx\r"
expect "*Opt*"
send "需要登陆的服务器IP\r"
expect "*ID*"
send "2\r"
expect "*Last login*"
send "sudo su\r"
expect "*root*"
send "cd ~\r"
interact
expect eof