Linux 常用命令速查手册
Linux 是服务器运维必备技能。本文提供 Linux 常用命令的速查指南,帮助你快速完成日常运维任务。
一、文件与目录管理
1.1 列出文件
ls
ls -la
ls -lh
ls -R
ls -lt
ls -d */
ls -lS
|
1.2 创建目录
mkdir mydir
mkdir -p a/b/c/d
mkdir dir1 dir2 dir3
|
1.3 删除文件和目录
rm file.txt
rm -r mydir
rm -f file.txt rm -rf mydir
rm -rf mydir/*
|
1.4 复制文件
cp file1.txt file2.txt
cp -r dir1 dir2
cp -p file1.txt file2.txt
cp -f file1.txt file2.txt
cp -v file1.txt file2.txt
|
1.5 移动文件
mv file.txt /path/to/destination/
mv oldname.txt newname.txt
mv -r dir1 dir2
mv -v file.txt /path/to/destination/
|
1.6 查找文件
find . -name "file.txt"
find . -iname "file.txt"
find . -name "*.log"
find . -size +100M find . -size -1M
find . -mtime -7 find . -mtime +7 find . -ctime -1 find . -atime -1
find . -type f find . -type d find . -type l find . -type s
find /var -name "*.log" -type f -size +10M
|
1.7 搜索文件内容
grep "pattern" file.txt
grep "pattern" .
grep -n "pattern" file.txt
grep -i "pattern" file.txt
grep -v "pattern" file.txt
grep -c "pattern" file.txt
grep -A 2 -B 2 "pattern" file.txt
|
二、网络操作
2.1 网络连接
netstat -tuln
ss -tuln
netstat -an
netstat -ant
netstat -tulnp ss -tulnp
|
2.2 测试网络
ping google.com ping -c 4 8.8.8.8
telnet google.com 80
nc -zv google.com 80
nslookup google.com dig google.com
curl https://www.google.com curl -I https://www.google.com
|
2.3 IP 配置
ip addr
ifconfig
ip route
cat /etc/resolv.conf
ip addr add 192.168.1.100/24 dev eth0
|
2.4 网络下载
wget https://example.com/file.zip
wget -b https://example.com/file.zip
wget -O file.zip https://example.com/file.zip
wget -r -np -k https://example.com/
|
curl -O https://example.com/file.zip
curl -O https://example.com/file.zip &
curl -o file.zip https://example.com/file.zip
|
三、进程管理
3.1 查看进程
ps aux
pstree
ps -p 1234
ps -u root
ps -ef
ps -ef --forest
top
htop
pgrep nginx
|
3.2 进程操作
kill 1234
kill -9 1234
killall nginx
pkill nginx
cat /proc/[pid]/status
|
3.3 服务管理
systemctl start nginx systemctl stop nginx systemctl restart nginx systemctl status nginx systemctl enable nginx systemctl disable nginx
service nginx start service nginx stop service nginx restart service nginx status
journalctl -u nginx journalctl -u nginx -f
|
3.4 定时任务
crontab -l
crontab -e
crontab -r
cat /etc/crontab
|
* * * * * command
0 * * * * command
0 1 * * * command
0 0 * * 0 command
0 0 1 * * command
|
四、系统监控
4.1 内存使用
free -h
cat /proc/meminfo
vmstat
|
4.2 磁盘使用
df -h
du -sh .
du -h --max-depth=1
du -sh /path/to/directory
|
4.3 系统信息
uname -a
uname -r
cat /etc/os-release
uptime
date
date -s "2024-12-15 14:30:00"
lscpu lsblk
|
4.4 系统日志
dmesg
tail -f /var/log/syslog
tail -f /var/log/error.log
journalctl -k
journalctl --since today
|
4.5 CPU 使用
top
top -p 1234
ps aux --sort=-%cpu | head
|
五、用户管理
5.1 用户管理
whoami
cat /etc/passwd
who
w
|
5.2 用户创建
useradd username
useradd -m username passwd username
useradd -u 1000 username
useradd -d /home/username -m username
|
5.3 用户删除
userdel username
userdel -r username
|
5.4 权限管理
ls -l
chmod 755 file.txt chmod u+rwx,g+rx,o+rx file.txt
chown newuser file.txt
chown newuser:newgroup file.txt
chmod -R 755 /path/to/directory chown -R newuser:newgroup /path/to/directory
|
5.5 用户组
cat /etc/group
groupadd groupname
groupdel groupname
usermod -aG groupname username
groups username
|
六、压缩和解压
6.1 tar
tar -cvf archive.tar file1 file2
tar -czvf archive.tar.gz file1 file2
tar -xvf archive.tar
tar -xzvf archive.tar.gz
tar -tvf archive.tar
|
6.2 zip
zip archive.zip file1 file2
zip -r archive.zip directory/
unzip archive.zip
unzip archive.zip -d /path/to/destination/
unzip -l archive.zip
|
6.3 gzip
gzip file.txt
gzip -k file.txt
gunzip file.txt.gz
gzip file1.txt file2.txt
|
6.4 其他压缩工具
tar -cJf archive.tar.xz directory/
tar -xJf archive.tar.xz
tar -cjf archive.tar.bz2 directory/
tar -xjf archive.tar.bz2
|
七、管道和重定向
7.1 管道
ps aux | grep nginx
ls | grep "\.txt" | wc -l
command1 & command2
|
7.2 重定向
echo "hello" > file.txt echo "world" >> file.txt
command 2> error.log
command > output.log 2>&1
command > /dev/null 2>&1
|
八、编辑器
8.1 Vim
vim file.txt
i Esc :q :w :q! :wq
/word n N
|
8.2 Nano
nano file.txt
Ctrl+O Ctrl+X Ctrl+K Ctrl+U
|
九、脚本编程
9.1 Shell 脚本
#!/bin/bash
name="张三" echo "欢迎, $name"
if [ $name = "张三" ]; then echo "正确" else echo "错误" fi
for i in {1..5} do echo $i done
function greet() { echo "Hello, $1" }
greet "张三"
|
9.2 自动化任务
#!/bin/bash
echo "开始执行..."
if systemctl is-active --quiet nginx; then echo "Nginx 运行正常" else echo "Nginx 未运行" fi
tar -czf backup-$(date +%Y%m%d).tar.gz /path/to/data
echo "备份完成" | mail -s "备份通知" admin@example.com
|
十、总结
10.1 常用命令速查
ls, cd, pwd, mkdir, rmdir, rm, cp, mv find, grep
top, free, df, du, ps, kill, systemctl
ping, curl, wget, netstat, ss
useradd, userdel, passwd chmod, chown, chgrp
tar, zip, unzip, gzip
|
10.2 最佳实践
- 使用相对路径:减少错误
- 使用引号:保护特殊字符
- 添加错误处理:提高健壮性
- 定期备份:保护数据
- 使用脚本:提高效率
掌握 Linux,高效管理服务器!