#Linux&Shell# 速查手册:常用 Linux&Shell 命令

#Linux&Shell# 速查手册:常用 Linux&Shell 命令

覆盖在学习和工作中的常用 Linux 命令:管理 / 连接 / 网络 / 进程 / 文件 / 文本


目录 Table of Contents


管理

列举

1
2
3
4
5
6
# Ubuntu
apt list --installed
# CentOS
yum list installed
# Alpine
apk info

更新

1
2
3
4
5
6
7
8
# Ubuntu
apt-get update
apt-get -y upgrade
# CentOS
yum update
# Alpine
apk update
apk upgrade

安装

1
2
3
4
5
6
# Ubuntu
apt-get install -y ${package}
# CentOS
yum install -y ${package}
# Alpine
apk add --upgrade ${package}

卸载

1
2
3
4
5
6
# Ubuntu
apt-get --purge remove ${package}
# CentOS
yum remove ${package}
# Alpine
apk del ${package}

连接

ssh

1
2
ssh -p ${port} ${username}@${host}
sshpass -p ${password} ssh -p ${port} ${username}@${host}

telnet

1
telnet ${host} ${port}

ping

1
ping ${host}

nslookup

1
nslookup ${domain}

curl

1
2
3
4
5
curl \
-X ${method} \
-H "${header_key}: ${"header_val"}" \
-d "${json_data}" \
${api_url} \

mysql

1
mysql -h ${host} -P ${port} -u ${username} -p

mongo

1
mongo -u ${username} -p ${password} ${host}:${port}/${database}

redis

1
redis-cli -h ${host} -p ${port} -a ${password}

网络

查看 ip

1
ip a |grep eth

查看 dns

1
cat /etc/resolv.conf |grep nameserver

进程

查询 proc

1
ps -ef |grep ${proc_name}

查询 port

1
lsof -i:${port}

从 proc 查 port / 从 port 查 proc

1
2
netstat -tunlp |grep ${proc_name}
netstat -tunlp |grep ${port}

杀死进程

1
2
3
4
5
6
7
8
ps -ef | grep ${including_keyword} | grep -v ${excluding_keyword}
kill -s 9 ${pid}

ps -ef | grep ${including_keyword} | grep -v ${excluding_keyword} | awk '{print $2}' | xargs kill -s 9

kill -s 9 `pgrep ${keyword}`

pkill -9 ${keyword}

后台任务

1
2
3
4
5
6
7
8
9
# 启动
nohup ${command} >> ${log_file} 2>&1 &
# 查看
jobs -l
lsof -i:${job_port}
ps -ef |grep ${job_cmd}
# 杀死
kill -9 ${job_pid}
kill -9 ${proc_pid}

文件

从服务器下载到本机

1
2
3
4
scp -r -P ${port} ${username}@${host}:/path/to/remote /path/to/local # 包括文件
rsync -a -e ssh --exclude="${pattern}" ${username}@${host}:/path/to/remote /path/to/local # 排除文件

sz /path/to/server # 从server下载文件(可视化的)

从本机上传到服务器

1
2
3
4
scp -r /path/to/local -P ${port} ${username}@${host}:/path/to/remote # 包括文件
rsync -a -e ssh --exclude="${pattern}" /path/to/local ${username}@${host}:/path/to/remote # 排除文件

rz # 从local上传文件(可视化的)

解压

zip

1
unzip -d /path/to/unzip ${pkg_to_unzip}

tar

1
2
3
tar -xvf ${pkg_to_untar} # 解压 tar 包
tar -zxvf ${pkg_to_untar} # 解压 tar.gz 包
tar -jxvf ${pkg_to_untar} # 解压 tar.bz2 包

加压

zip

1
2
3
zip -r /path/to/zip ${pkg_to_zip}
zip -d /path/to/zip ${file_to_delete} # 从压缩包中删除文件
zip -m /path/to/zip ${file_to_append} # 向压缩包中添加文件

tar

1
2
3
tar -cvf ${pkg_to_tar} /path/to/tar # 加压 tar 包
tar -zcvf ${pkg_to_tar} /path/to/tar # 加压 tar.gz 包
tar -zjvf ${pkg_to_tar} /path/to/tar # 加压 tar.bz2 包

文本

查看

json

1
cat ${file} | python -m json.tool

log

1
2
3
4
less ${log_file}
vim ${log_file}
tail -n ${number} ${log_file}
tail -f ${log_file}

jq

1
2
3
cat ${file} | jq xxx
# ref: https://chengqian90.com/Linux%E5%B7%A5%E5%85%B7/Linux%20shell%20json%E8%A7%A3%E6%9E%90%E5%B7%A5%E5%85%B7%E2%80%94%E2%80%94jq.html
# ref: https://mozillazg.com/2018/01/jq-use-examples-cookbook.html

编辑

复制

1
ctrl + Insert

粘贴

1
shift + Insert

移动

1
2
3
4
gg # 首行
G # 末行
shift + ^ # 行首
shift + $ # 行末

搜索

1
2
3
4
/pattern # 向前搜索
?pattern # 向后搜索
n # 上一个
N # 下一个

查看缩进和行尾

1
: set list

处理

分隔

1
awk -F:"${seperator}" '/${pattern}/${command}' ${file}

替换

1
sed "s/${pattern}/${substr}/g"

权限

1
2
chown -R ${user}:${group} ${file}
chmod 777 ${file}

工具

统计代码

1
2
sudo apt install -y cloc
cloc ${include_dir} --exclude-dir=${exclude_dir}

压力测试

1
wrk -t${threads} -c${connections} -d${duration} --latency ${url}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×