// 文件 · 权限 · 进程 · 网络 · 文本处理 · 系统管理
pwd # 当前目录
cd /path/to/dir
cd ~ # HOME目录
cd - # 上一个目录
cd .. # 上级目录
ls
ls -la # 详细列表含隐藏
ls -lh # 人类可读大小
ls -lt # 按时间排序
ls -lS # 按大小排序
ls -R # 递归列出
tree # 树形显示
tree -L 2 # 限制深度
tree -a # 含隐藏文件
# 创建
touch file.txt
mkdir dir
mkdir -p a/b/c # 创建多层目录
# 复制
cp src dst
cp -r src/ dst/ # 递归复制目录
cp -p src dst # 保留属性
# 移动/重命名
mv src dst
mv *.txt dir/
# 删除
rm file.txt
rm -r dir/ # 递归删除
rm -rf dir/ # 强制递归删除(慎用!)
rmdir empty_dir # 仅删除空目录
# 查找
find . -name "*.log"
find /var -type f -mtime +7 # 7天前
find . -size +10M
find . -name "*.py" -exec rm {} +
cat file.txt
cat -n file.txt # 带行号
less file.txt # 分页查看(推荐)
more file.txt # 向前分页
head -n 20 file.txt # 前20行
tail -n 50 file.txt # 后50行
tail -f file.log # 实时追踪日志
wc -l file.txt # 行数
wc -w file.txt # 单词数
wc -c file.txt # 字节数
file binary # 判断文件类型
stat file.txt # 详细元信息
xxd file # hex dump
# chmod 数字模式
# r=4, w=2, x=1
# rwxr-xr-- = 754
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 .ssh/key # rw-------
chmod -R 755 dir/ # 递归
# chmod 符号模式
chmod +x script.sh # 加执行权限
chmod u+w file # 用户加写权限
chmod g-w file # 组减写权限
chmod o=r file # 其他只读
chmod a+x file # 所有人加执行
# chown 修改所有权
chown user file
chown user:group file
chown -R user:group dir/
# 特殊权限
chmod u+s file # SUID
chmod g+s dir # SGID
chmod +t /tmp # Sticky bit
# 用户信息
whoami
id
id username
groups
# 用户管理
useradd -m newuser # 创建用户+主目录
useradd -m -s /bin/bash -G sudo user
usermod -aG sudo user # 加入sudo组
userdel -r user # 删除用户和主目录
passwd user # 修改密码
# sudo
sudo command
sudo -u otheruser cmd
sudo -i # 切换为root shell
su - user # 切换用户
visudo # 编辑sudoers
# 查看进程
ps aux # 所有进程
ps -ef # 完整格式
ps aux | grep nginx
top # 实时进程(q退出)
htop # 交互式进程查看
# 进程树
pstree
pstree -p
# 查找进程PID
pgrep nginx
pidof sshd
# 发送信号 / 结束进程
kill PID # SIGTERM (优雅)
kill -9 PID # SIGKILL (强制)
killall nginx
pkill -f pattern
# 后台运行
command & # 后台运行
jobs # 查看后台任务
fg %1 # 调回前台
bg %1 # 继续在后台
nohup cmd & # 挂起免疫
disown # 从shell分离
# 服务控制
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx # 重载配置
systemctl status nginx
# 开机启动
systemctl enable nginx
systemctl disable nginx
systemctl enable --now nginx # 启用并立即启动
# 查看
systemctl list-units
systemctl list-units --failed
systemctl is-active nginx
# 日志
journalctl -u nginx
journalctl -u nginx -f # 追踪
journalctl --since "1h ago"
journalctl -n 100 # 最近100条
# 连通性
ping -c 4 google.com
traceroute google.com
mtr google.com # 综合traceroute
# DNS
nslookup domain.com
dig domain.com
dig domain.com MX
host domain.com
# 网卡 & IP
ip addr # 或 ip a
ip link
ip route
ifconfig # 旧方式
# 端口 & 连接
ss -tlnp # 监听中的TCP端口
ss -anp # 所有连接
netstat -tlnp # 旧方式
lsof -i :80 # 使用80端口的进程
lsof -i tcp
# curl
curl https://example.com
curl -o file.txt url # 下载
curl -I url # 只看header
curl -X POST -H "Content-Type: application/json" \
-d '{"key":"val"}' url
curl -u user:pass url # 基本认证
curl -L url # 跟随重定向
# wget
wget url
wget -O file url
wget -r -np url # 递归下载
# SSH
ssh user@host
ssh -p 2222 user@host
ssh -i ~/.ssh/key.pem user@host
ssh -L 8080:localhost:80 host # 本地转发
ssh -R 80:localhost:8080 host # 远程转发
scp file user@host:/path
rsync -avz src/ user@host:/dst
grep "pattern" file
grep -r "pattern" dir/ # 递归
grep -i "pattern" # 忽略大小写
grep -n "pattern" # 显示行号
grep -v "pattern" # 反向匹配
grep -c "pattern" # 统计行数
grep -l "pattern" *.txt # 只显示文件名
grep -A 3 -B 3 "pat" # 上下文各3行
grep -E "pat1|pat2" # 扩展正则
grep -P "\d+" # Perl正则
grep -w "word" # 整词匹配
grep -o "pat" # 只输出匹配部分
# 打印列
awk '{print $1}' file # 第1列
awk '{print $1, $3}' file # 第1和3列
awk '{print NF}' # 列数
awk '{print NR, $0}' # 行号+内容
# 分隔符
awk -F: '{print $1}' /etc/passwd
awk -F',' '{print $2}' csv
# 条件过滤
awk '$3 > 100' file
awk '/error/{print $0}' log
awk 'NR==5' file # 第5行
awk 'NR>=5 && NR<=10' # 5-10行
# 求和
awk '{sum+=$1} END{print sum}'
# 替换
sed 's/old/new/' file # 每行首次
sed 's/old/new/g' file # 全部替换
sed -i 's/foo/bar/g' file # 直接修改文件
sed -i.bak 's/foo/bar/g' f # 备份后修改
# 删除行
sed '5d' file # 删第5行
sed '/pattern/d' file # 删匹配行
sed '/^$/d' file # 删空行
# 打印指定行
sed -n '5p' file # 第5行
sed -n '5,10p' file # 5-10行
# 插入/追加
sed '2i\新行内容' file # 第2行前插入
sed '2a\追加内容' file # 第2行后追加
# 重定向
cmd > file # stdout覆盖写
cmd >> file # stdout追加
cmd 2> err.log # stderr到文件
cmd 2>&1 # stderr合并到stdout
cmd &> file # 两者都到文件
cmd < file # stdin从文件
# 管道
cmd1 | cmd2
ls -la | grep ".py" | sort
ps aux | grep nginx | awk '{print $2}'
# 常用组合
cat file | sort | uniq -c | sort -rn
find . -name "*.log" | xargs rm
cat /etc/passwd | cut -d: -f1 | sort
command | tee file.txt # 同时输出和保存
# 磁盘使用
df -h # 磁盘空间
df -hT # 含文件系统类型
du -sh dir/ # 目录大小
du -h --max-depth=1 # 一级子目录大小
du -sh * | sort -rh # 按大小排序
lsblk # 块设备树
# 内存
free -h
vmstat 1 # 每1秒刷新
# CPU & 系统
uname -a # 内核信息
uptime
lscpu # CPU信息
nproc # CPU核心数
cat /etc/os-release # 系统版本
hostname
date # 当前时间
timedatectl # 时区管理
# tar
tar -czf archive.tar.gz dir/ # 压缩
tar -xzf archive.tar.gz # 解压
tar -xzf archive.tar.gz -C /dst
tar -tzf archive.tar.gz # 查看内容
tar -cjf archive.tar.bz2 dir/ # bzip2压缩
tar -cJf archive.tar.xz dir/ # xz压缩
# gzip/bzip2
gzip file # 压缩为.gz
gzip -d file.gz # 解压
gunzip file.gz
# zip
zip -r archive.zip dir/
unzip archive.zip
unzip -l archive.zip # 查看内容
unzip archive.zip -d /dst
# 编辑 crontab
crontab -e # 编辑
crontab -l # 查看
crontab -r # 删除
# 格式:分 时 日 月 周 命令
# * * * * *
*/5 * * * * /script.sh # 每5分钟
0 3 * * * /backup.sh # 每天3点
0 */2 * * * /cmd # 每2小时
0 9 * * 1 /cmd # 周一9点
@reboot /cmd # 开机运行
@daily /cmd # 每天
@weekly /cmd # 每周