覆盖在学习和工作中的常用 Linux 命令:管理 / 连接 / 网络 / 进程 / 文件 / 文本
目录 Table of Contents
管理
列举
1 2 3 4 5 6
| apt list --installed
yum list installed
apk info
|
更新
1 2 3 4 5 6 7 8
| apt-get update apt-get -y upgrade
yum update
apk update apk upgrade
|
安装
1 2 3 4 5 6
| apt-get install -y ${package}
yum install -y ${package}
apk add --upgrade ${package}
|
卸载
1 2 3 4 5 6
| apt-get --purge remove ${package}
yum remove ${package}
apk del ${package}
|
连接
ssh
1 2
| ssh -p ${port} ${username}@${host} sshpass -p ${password} ssh -p ${port} ${username}@${host}
|
telnet
ping
nslookup
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
查看 dns
1
| cat /etc/resolv.conf |grep nameserver
|
进程
查询 proc
1
| ps -ef |grep ${proc_name}
|
查询 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
|
从本机上传到服务器
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
|
解压
zip
1
| unzip -d /path/to/unzip ${pkg_to_unzip}
|
tar
1 2 3
| tar -xvf ${pkg_to_untar} tar -zxvf ${pkg_to_untar} tar -jxvf ${pkg_to_untar}
|
加压
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 -zcvf ${pkg_to_tar} /path/to/tar tar -zjvf ${pkg_to_tar} /path/to/tar
|
文本
查看
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 4
| gg G shift + ^ shift + $
|
搜索
1 2 3 4
| /pattern ?pattern n N
|
查看缩进和行尾
处理
分隔
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}
|