一、文件与目录操作
基础操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| ls -la ls -lhS ls -lht
cd /path/to/dir cd ~ cd - cd ..
mkdir -p /path/to/dir
touch file.txt
cp file.txt backup.txt cp -r dir1/ dir2/ cp -a source/ dest/
mv old.txt new.txt mv file.txt /path/to/
rm file.txt rm -rf directory/
|
文件查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| find / -name "*.log" find /var -size +100M find /tmp -mtime -7 find . -type f -name "*.java" -exec grep -l "TODO" {} \; find . -empty -type d
locate nginx.conf sudo updatedb
which java whereis nginx
|
二、文本处理
查看文件
1 2 3 4 5 6
| cat file.txt less file.txt head -n 20 file.txt tail -n 20 file.txt tail -f /var/log/syslog wc -l file.txt
|
文本搜索(grep)
1 2 3 4 5 6 7 8
| grep "error" log.txt grep -i "error" log.txt grep -r "TODO" ./src/ grep -n "function" app.js grep -c "404" access.log grep -v "debug" log.txt grep -A 3 -B 2 "Exception" log.txt grep -E "error|warn|fatal" log.txt
|
文本处理(sed / awk)
1 2 3 4 5 6 7 8 9 10 11
| sed -i 's/old/new/g' file.txt sed -i '5d' file.txt sed -n '10,20p' file.txt sed '/^#/d' config.txt
awk '{print $1, $3}' file.txt awk -F: '{print $1}' /etc/passwd awk '$3 > 100' data.txt awk '{sum+=$1} END{print sum}' nums
|
排序与去重
1 2 3 4 5
| sort file.txt sort -n file.txt sort -k2 -t: file.txt uniq file.txt sort file.txt | uniq -c | sort -rn
|
三、系统信息
系统状态
1 2 3 4 5 6
| uname -a cat /etc/os-release uptime hostname date timedatectl
|
硬件信息
1 2 3 4 5 6 7
| lscpu free -h df -h du -sh /path/ lsblk lspci lsusb
|
进程管理
1 2 3 4 5 6 7 8
| ps aux ps aux | grep java top htop kill PID kill -9 PID pkill -f "python app.py" nohup command &
|
四、网络操作
网络配置
1 2 3 4 5
| ip addr ip route ss -tlnp ss -s netstat -tlnp
|
网络诊断
1 2 3 4 5 6 7
| ping google.com traceroute google.com dig example.com nslookup example.com curl -I https://example.com curl -o file.zip URL wget URL
|
SSH
1 2 3 4 5
| ssh user@host ssh -p 2222 user@host scp file.txt user@host:/path/ scp -r dir/ user@host:/path/ rsync -avz source/ user@host:/dest/
|
五、权限管理
文件权限
1 2 3 4 5
| chmod 755 file.txt chmod u+x script.sh chmod -R 644 /path/ chown user:group file.txt chown -R user:group /path/
|
权限说明
1 2 3 4 5 6 7
| r (4) = 读 w (2) = 写 x (1) = 执行
所有者 | 用户组 | 其他用户 rwx r-x r-x 7 5 5
|
六、压缩与解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| tar -czf archive.tar.gz dir/ tar -cjf archive.tar.bz2 dir/ tar -xzf archive.tar.gz tar -xzf archive.tar.gz -C /path/ tar -tzf archive.tar.gz
zip -r archive.zip dir/ unzip archive.zip unzip archive.zip -d /path/
gzip file.txt gunzip file.txt.gz
|
七、用户管理
1 2 3 4 5 6 7
| useradd -m username passwd username userdel -r username usermod -aG sudo username groups username whoami id username
|
八、系统服务(systemd)
1 2 3 4 5 6 7 8 9
| systemctl start service systemctl stop service systemctl restart service systemctl reload service systemctl status service systemctl enable service systemctl disable service systemctl list-units --type=service journalctl -u service -f
|
九、磁盘管理
1 2 3 4 5 6 7 8 9 10 11 12
| fdisk -l mkfs.ext4 /dev/sdb1
mount /dev/sdb1 /mnt/data umount /mnt/data
df -hT du -sh * ncdu /
|
十、定时任务(Cron)
1 2 3
| crontab -e crontab -l crontab -r
|
Cron 表达式
1 2 3 4 5 6 7 8
| 分 时 日 月 周 命令 * * * * * command
# 示例 0 2 * * * /backup.sh # 每天凌晨2点 */5 * * * * /check.sh # 每5分钟 0 9 * * 1 /report.sh # 每周一上午9点 0 0 1 * * /cleanup.sh # 每月1号零点
|
十一、实用技巧
管道与重定向
1 2 3 4 5
| command1 | command2 command > file.txt command >> file.txt command 2>&1 command &> /dev/null
|
历史命令
1 2 3 4
| history !100 !! Ctrl+r
|
后台任务
1 2 3 4
| command & jobs fg %1 bg %1
|
总结
熟练掌握 Linux 命令是后端开发和运维的基本功。建议在日常工作中多用命令行,逐步积累。记住:不确定的命令先用 man command 或 command --help 查看文档,危险操作(尤其是 rm -rf)务必三思。