美宝的笔记

碎片

零散的记录,每条一句话,共 18 条。

ss 比 netstat 快得多

ss -tnp state established 直接按状态过滤,不用 grep。

查进程占用的端口

lsof -i :8080 或 ss -ltnp | grep 8080,后者不需要装 lsof。

不换行看长 JSON

python3 -m json.tool 或 jq .,比肉眼找括号靠谱。

diff 两个目录

diff -rq dir1 dir2,只列出不同的文件,不打印内容。

递归查找最近改动的文件

find . -type f -mmin -30,排查「刚刚谁动了什么」很好用。

tar 排除目录

tar --exclude='./node_modules' -czf x.tgz .,--exclude 要写在源路径前。

看某个端口被哪个容器占了

docker ps --format '{{.Ports}} {{.Names}}' | grep 8080。

批量改文件权限

find . -type d -exec chmod 755 {} +,用 + 比 \; 快很多。

查 TLS 证书到期时间

openssl s_client -connect host:443 -servername host 2>/dev/null | openssl x509 -noout -dates。

让命令输出同时进文件和屏幕

cmd 2>&1 | tee -a out.log,排查时别只重定向。

看内存被谁吃了

ps aux --sort=-%mem | head,比 free 直观。

curl 只看头不看体

curl -sI https://example.com/,比 -v 干净。

快速起一个静态服务

python3 -m http.server 8000,临时分享文件够用。

查磁盘 IO 谁在跑

iotop -oPa,只看有实际读写的进程。

systemd 重载配置

改完 unit 文件要 systemctl daemon-reload,只 reload 服务不生效。

递归解压前先看内容

tar -tzf x.tgz | head -30,避免解压出一堆东西。

查历史命令里用过什么

history | grep ssh,比翻 shell 记录快。

安全删除大文件前先确认

ls -lh 文件名 + file 文件名,两次确认再动手。