389 字
2 分钟
Linux 常用命令速查
这份速查表以 GNU/Linux 常见工具为主。macOS 的部分参数不同,执行破坏性命令前应先阅读 man 页面。
文件与目录
pwd # 当前目录ls -lah # 包含隐藏文件和可读大小cd /path/to/dirmkdir -p app/logscp -a source target # 尽量保留属性mv old newrm -i file # 删除前确认find . -type f -name '*.log'WARNING
rm -rf不经过回收站。变量为空、路径拼错或当前目录判断错误都可能造成严重数据损失。
查看文件
cat file.txtless file.txthead -n 20 file.txttail -n 100 file.txttail -f app.logwc -l file.txtfile archive.tar.gz文本搜索与处理
grep -Rni 'error' ./logsgrep -v '^#' app.confcut -d: -f1 /etc/passwdsort names.txt | uniq -c | sort -nrsed -n '10,30p' file.txtawk '{print $1, $NF}' access.log组合命令时,先逐段确认输出,再用管道连接:
journalctl -u nginx --since today | grep -i error | tail -n 50压缩与归档
tar -czf backup.tar.gz directory/tar -xzf backup.tar.gzzip -r backup.zip directory/unzip backup.zip系统信息
uname -acat /etc/os-releasehostnamectluptimedatetimedatectlfree -hlscpulsblk进程与资源
ps auxps -ef | grep process-namepgrep -af process-nametopkill PIDkill -TERM PID优先发送 TERM 让程序清理资源;只有进程无法响应时才考虑 KILL:
kill -KILL PID网络基础
ip addressip routess -lntupping -c 4 example.comcurl -I https://example.comcurl -v https://example.comgetent hosts example.com权限和用户
idwhoamisudo -lchmod u+x script.shchown user:group file获取帮助
man commandcommand --helpapropos keywordtype commandtype 可以确认一个名称究竟是外部程序、Shell 内建命令、函数还是别名。