本文档简要介绍 Linux 系统中常用的 shell 命令,包括文本处理、网络、文件操作、系统信息等类别。
文本处理命令 grep - 文本搜索 在文件中搜索匹配模式的行。
1 2 3 4 5 6 7 8 9 10 11 grep "pattern" file.txt grep -i "pattern" file.txt grep -r "pattern" dir / grep -n "pattern" file.txt grep -v "pattern" file.txt grep -E "pattern" file.txt grep -l "pattern" *.txt grep -c "pattern" file.txt
sed - 流编辑器 对文本进行流式编辑,支持查找替换、删除、插入等操作。
1 2 3 4 5 6 7 8 9 sed 's/old/new/g' file.txt sed -i 's/old/new/g' file.txt sed -n '10,20p' file.txt sed '/pattern/d' file.txt sed '1d' file.txt sed '$d' file.txt sed 's/^/prefix/' file.txt sed 's/$/suffix/' file.txt
awk - 文本处理工具 强大的文本分析和处理工具,支持按列处理。
1 2 3 4 5 6 7 8 awk '{print $1}' file.txt awk '{print $1, $3}' file.txt awk '{print $NF}' file.txt awk '/pattern/ {print}' file.txt awk -F: '{print $1}' /etc/passwd awk '{sum+=$1} END {print sum}' file.txt awk 'NR==1 || NR==10' file.txt
cut - 列提取 从文件中提取列。
1 2 3 4 5 cut -d: -f1 /etc/passwd cut -d' ' -f1,3 file.txt cut -c1-10 file.txt cut -c1,3,5 file.txt
sort - 排序 对文本行进行排序。
1 2 3 4 5 6 7 sort file.txt sort -n file.txt sort -r file.txt sort -u file.txt sort -k2 file.txt sort -t: -k3 -n /etc/passwd
uniq - 去重 去除相邻的重复行。
1 2 3 4 5 6 uniq file.txt sort file.txt | uniq uniq -c file.txt uniq -d file.txt uniq -u file.txt
wc - 统计 统计文件的行数、单词数、字符数。
1 2 3 4 5 6 wc file.txt wc -l file.txt wc -w file.txt wc -c file.txt wc -m file.txt
head/tail - 查看文件头尾 查看文件的开头或结尾部分。
1 2 3 4 5 6 7 8 9 10 head file.txt head -n 20 file.txt head -c 100 file.txt tail file.txt tail -n 20 file.txt tail -f log.txt tail -F log.txt
cat/less/more - 查看文件 查看文件内容。
1 2 3 4 5 6 7 8 9 10 11 12 cat file.txt cat file1.txt file2.txt cat -n file.txt cat -A file.txt less file.txt more file.txt
tr - 字符转换 转换或删除字符。
1 2 3 4 5 tr 'a-z' 'A-Z' < file.txt tr ' ' '\n' < file.txt tr -d '0-9' < file.txt tr -s ' ' < file.txt
paste - 合并文件 按列合并文件。
1 2 3 4 paste file1.txt file2.txt paste -d: file1.txt file2.txt paste -s file.txt
join - 连接文件 基于共同字段连接两个文件。
1 2 3 4 join file1.txt file2.txt join -1 2 -2 1 file1.txt file2.txt join -t: file1.txt file2.txt
网络命令 curl - 网络请求 发送 HTTP 请求,下载文件等。
1 2 3 4 5 6 7 8 9 10 curl http://example.com curl -O http://example.com/file.zip curl -o output.txt http://example.com curl -L http://example.com curl -I http://example.com curl -X POST -d "data" http://example.com curl -H "Content-Type: application/json" -d '{"key":"value"}' http://example.com curl -u user:pass http://example.com curl -v http://example.com
wget - 下载工具 从网络下载文件。
1 2 3 4 5 6 7 wget http://example.com/file.zip wget -O output.zip http://example.com/file.zip wget -c http://example.com/file.zip wget -r http://example.com wget -P /path/to/save http://example.com/file.zip wget --limit-rate=200k http://example.com/file.zip
ping - 网络连通性测试 测试网络连通性和延迟。
1 2 3 4 5 ping example.com ping -c 4 example.com ping -i 2 example.com ping -s 1024 example.com
netstat - 网络连接信息 显示网络连接、路由表、接口统计等。
1 2 3 4 5 6 7 netstat -an netstat -tuln netstat -rn netstat -i netstat -p netstat -s
ss - Socket 统计(netstat 的替代) 显示 socket 统计信息,比 netstat 更快。
1 2 3 4 5 6 ss -an ss -tuln ss -tulnp ss -s ss -o state established
ifconfig - 网络接口配置 配置和显示网络接口信息。
1 2 3 4 5 6 7 ifconfig ifconfig eth0 ifconfig eth0 up ifconfig eth0 down ifconfig eth0 192.168.1.100 ifconfig eth0 netmask 255.255.255.0
ip - 网络配置(ifconfig 的替代) 现代的网络配置工具,功能更强大。
1 2 3 4 5 6 7 ip addr show ip link show ip route show ip addr add 192.168.1.100/24 dev eth0 ip link set eth0 up ip route add default via 192.168.1.1
telnet - 远程登录 测试端口连通性和远程登录。
1 2 3 telnet example.com 80 telnet example.com
nc (netcat) - 网络工具 网络调试和数据传输工具。
1 2 3 4 5 nc -l 8080 nc example.com 80 nc -zv example.com 80 nc -l -p 8080 -e /bin/bash
dig - DNS 查询 DNS 查询工具。
1 2 3 4 5 dig example.com dig example.com MX dig @8.8.8.8 example.com dig -x 8.8.8.8
nslookup - DNS 查询 另一个 DNS 查询工具。
1 2 3 nslookup example.com nslookup 8.8.8.8
traceroute - 路由追踪 追踪数据包到达目标主机的路径。
1 2 3 4 traceroute example.com traceroute -n example.com traceroute -m 30 example.com
tcpdump - 网络抓包 捕获和分析网络数据包。
1 2 3 4 5 6 7 tcpdump -i eth0 tcpdump port 80 tcpdump host 192.168.1.1 tcpdump -w capture.pcap tcpdump -r capture.pcap tcpdump -n
文件操作命令 ls - 列出文件 列出目录内容。
1 2 3 4 5 6 7 8 9 ls ls -l ls -a ls -h ls -t ls -S ls -R ls -lh
cd - 切换目录 切换工作目录。
1 2 3 4 5 cd /path/to/dir cd ~ cd - cd ..
pwd - 显示当前目录 显示当前工作目录的路径。
mkdir - 创建目录 创建新目录。
1 2 3 4 mkdir dirname mkdir -p dir1/dir2/dir3 mkdir -m 755 dirname
rm - 删除文件/目录 删除文件或目录。
1 2 3 4 5 6 rm file.txt rm -r dir / rm -f file.txt rm -rf dir / rm -i file.txt
cp - 复制文件 复制文件或目录。
1 2 3 4 5 6 cp file1.txt file2.txt cp -r dir1/ dir2/ cp -p file1.txt file2.txt cp -a dir1/ dir2/ cp -u file1.txt file2.txt
mv - 移动/重命名 移动或重命名文件/目录。
1 2 3 4 5 mv file1.txt file2.txt mv file.txt /path/to/dir/ mv -i file1.txt file2.txt mv -u file1.txt file2.txt
find - 查找文件 查找文件和目录。
1 2 3 4 5 6 7 8 find /path -name "*.txt" find /path -type f find /path -type d find /path -mtime -7 find /path -size +100M find /path -exec ls -l {} \; find /path -name "*.txt" -delete
chmod - 修改权限 修改文件或目录的权限。
1 2 3 4 5 6 chmod 755 file.txt chmod u+x file.txt chmod g-w file.txt chmod a+r file.txt chmod -R 755 dir /
chown - 修改所有者 修改文件或目录的所有者。
1 2 3 4 chown user:group file.txt chown user file.txt chown -R user:group dir /
tar - 归档工具 创建和解压归档文件。
1 2 3 4 5 6 7 tar -czf archive.tar.gz dir / tar -xzf archive.tar.gz tar -cjf archive.tar.bz2 dir / tar -xjf archive.tar.bz2 tar -tf archive.tar.gz tar -xzf archive.tar.gz -C /path/
zip/unzip - 压缩工具 创建和解压 zip 文件。
1 2 3 4 5 6 7 8 zip -r archive.zip dir / zip -9 archive.zip file.txt unzip archive.zip unzip -l archive.zip unzip -d /path/ archive.zip
系统信息命令 ps - 进程信息 显示进程信息。
1 2 3 4 5 6 ps ps aux ps -ef ps aux | grep nginx ps -p 1234
top/htop - 进程监控 实时显示进程和系统资源使用情况。
1 2 3 4 5 6 7 top top -u username top -p 1234 htop
kill - 终止进程 终止进程。
1 2 3 4 5 6 kill 1234 kill -9 1234 kill -HUP 1234 killall nginx pkill nginx
df - 磁盘空间 显示文件系统磁盘空间使用情况。
1 2 3 4 5 df df -h df -T df -i
du - 目录大小 显示目录或文件的磁盘使用情况。
1 2 3 4 5 6 du du -h du -sh dir / du -h --max-depth=1 du -ah
free - 内存信息 显示内存使用情况。
1 2 3 4 5 6 free free -h free -m free -g free -s 5
uname - 系统信息 显示系统信息。
1 2 3 4 5 uname uname -a uname -r uname -m
uptime - 运行时间 显示系统运行时间和负载。
who/w - 登录用户 显示当前登录的用户。
date - 日期时间 显示或设置系统日期和时间。
1 2 3 4 5 date date +%Y-%m-%d date +%H:%M:%S date -s "2024-01-01 12:00:00"
其他常用命令 history - 命令历史 显示命令历史记录。
1 2 3 4 5 6 history history | grep "pattern" !123 !! !pattern
alias - 命令别名 创建命令别名。
1 2 3 4 5 alias ll='ls -alF' alias grep='grep --color=auto' unalias ll alias
which/whereis - 查找命令 查找命令的位置。
1 2 3 4 5 6 7 which ls which -a python whereis ls whereis -b ls
man - 帮助手册 查看命令的帮助手册。
1 2 3 4 man ls man -k keyword man -f command
nohup - 后台运行 在后台运行命令,即使终端关闭也不中断。
1 2 3 nohup command & nohup command > output.log 2>&1 &
screen/tmux - 终端复用 终端复用工具,可以创建多个会话。
1 2 3 4 5 6 7 8 9 10 11 12 13 screen screen -S name screen -r name screen -ls tmux tmux new -s name tmux attach -t name tmux ls
watch - 定期执行 定期执行命令并显示输出。
1 2 3 4 watch -n 1 'ps aux | grep nginx' watch -d 'ls -l' watch -n 5 'df -h'
xargs - 参数传递 从标准输入读取参数并执行命令。
1 2 3 4 5 find . -name "*.txt" | xargs rm find . -name "*.txt" | xargs grep "pattern" echo "file1 file2" | xargs ls -l find . -name "*.txt" -print0 | xargs -0 rm
tee - 分流输出 将输出同时写入文件和标准输出。
1 2 3 4 ls -l | tee output.txt ls -l | tee -a output.txt command 2>&1 | tee output.log
diff - 文件比较 比较两个文件的差异。
1 2 3 4 diff file1.txt file2.txt diff -u file1.txt file2.txt diff -r dir1/ dir2/
patch - 应用补丁 应用补丁文件。
1 2 3 patch < patchfile patch -p1 < patchfile
管道和重定向 管道 (|) 将一个命令的输出作为另一个命令的输入。
1 2 3 4 ls -l | grep "txt" ps aux | grep nginx cat file.txt | sort | uniq
重定向 1 2 3 4 5 6 7 8 9 10 11 12 command > file.txt command >> file.txt command 2> error.log command > output.log 2>&1 command > /dev/null 2>&1 command < file.txt command << EOF # Here document content EOF
组合使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 find . -name "*.log" | xargs wc -l tail -f access.log | grep "ERROR" | awk '{print $1}' | sort | uniq -cwatch -n 1 'ps aux | grep nginx | grep -v grep' find . -name "*.txt" -exec sed -i 's/old/new/g' {} \; ping -c 4 example.com && curl -I http://example.com tar -czf backup.tar.gz $(find . -name "*.log" ) && rm $(find . -name "*.log" )
参考文献