技术弄巷
读完仅需
记得星标公众号

在Linux运维工作中,文本处理、管道、重定向、定时任务、格式化输出等基础命令是日常效率的核心。无论是日志排查、数据提取、批量处理还是脚本编写,熟练掌握这些命令都能大幅提升工作效率,避免重复劳动。
本文整理了运维最常用的文本查看、查找、替换、排序、统计、管道、重定向、计划任务、后台执行等高频命令,全部附带实战示例,适合作为学习手册与速查工具。

# 查看文件并显示行号
cat -n /etc/passwd
# 查看前20行
head -20 /etc/passwd
# 查看后10行
tail -10 /etc/passwd
# 取第20行内容(经典组合)
cat -n /etc/passwd | head -20 | tail -1
revtacrev 文件名
tac 文件名
-name-type-user-nouser:按属主-group-nogroup:按属组-mtime-size# 查找并复制
find /etc/ -name "hosts" -execcp {} /tmp/ \;
# 查找7天前文件并删除
find /var/log -type f -mtime +7 -execrm {} \;
解决 exec参数过长溢出问题:
find / -name "core" -exec file {} \;
find / -name "core" | xargs file
列转行、批量创建目录:
cat /etc/passwd | grep ^abc | cut -d: -f1 | xargs mkdir
# 每10分钟执行一次
*/10 * * * * 命令或脚本
xclock -update 1 &
nohupcommand &
nohup xclock -update 1 &
*?[a-z][0-9]、[!a-z]{1..10}# 支持转义
echo -e "a\tb\nc"
# 不换行
echo -n "hello"
# 字体颜色 30~37
echo -e "\033[31m 红色字 \033[0m"
# 背景色 40~47
echo -e "\033[42;37m 绿底白字 \033[0m"
# 系统铃声
echo -e "\007 bell"
printf"hello\n"
printf"%s,%s,%d\n" hello world 123
%s%d%.2f# 单变量
read name
echo$name
# 多变量
read a b c
# 带提示
read -p "input a num:" var
管道作用:
注意:管道中 read 无法赋值
echo 123 | read a
echo$a# 为空
# 覆盖
ls > file.log
# 追加
ls >> file.log
# 正确+错误一起输出
ls > file.log 2>&1
ls &> file.log
# 直到 EOF 结束
cat > file <<EOF
aaa
bbb
EOF
# 直接字符串输入
cat > file <<< "hello"
屏幕输出 + 写入文件:
who | tee who.log
find /etc -name hosts | tee aa.txt
sort file # 默认字典序
sort -n file # 按数字排序
sort -nr file # 数字倒序
sort -u file # 去重
sort -t: -k3nr file # 以:分隔,按第3列数字倒序
uniq file # 去连续重复
uniq -u file # 只显示不重复行
uniq -d file # 只显示重复行
uniq -dc file # 统计重复次数
grep root /etc/passwd
grep ^root /etc/passwd # 以root开头
grep halt$ /etc/passwd # 以halt结尾
grep -i root file # 忽略大小写
grep -n root file # 显示行号
grep -c root file # 统计行数
grep -r root /etc/ # 递归查找
grep -rl root /etc/ # 只显示文件名
grep -A2 -B2 -C2 root # 显示前后行
cut -c 1-5 file # 截取字符
cut -d: -f1 file # 以:分隔取第1列
cut -d: -f1,3,5 file
cut -d: -f1-5 file
# 大小写转换
echo"hello" | tr'[a-z]''[A-Z]'
# 去重字符
echo"helloooo" | tr -s '[a-z]'
# 删除空行
tr -s "\n" < file
wc -l file # 行数
wc -w file # 单词数
wc -c file # 字符数
basename /var/log/messages # messages
dirname /var/log/messages # /var/log
split -l 2 passwd # 每2行切分
split -b 1 file # 按字节切分
join a.txt b.txt
join -a1 -a2 a.txt b.txt
paste a.txt b.txt
ls | paste -d" " - - - -
cmd="echo \$name"
eval$cmd
date
date +%F
date +%Y-%m-%d-%H-%M-%S
date -s "2025-01-01 12:00:00"
logger "test log"
logger -p local5.err -t tag msg
echo"1+2" | bc
echo"scale=3;7/3" | bc
echo"sqrt(100)" | bc
# 进制转换
echo"ibase=10;obase=2;10" | bc
pwd && echo"ok"
aaa || echo"fail"
stat install.log/etc/passwdgrep过滤、sort排序、cut截取是运维日志处理核心。xargs解决参数过长问题。本文覆盖Linux运维文本处理、管道重定向、系统工具、Shell基础全场景高频命令,是运维人员从入门到进阶必备干货。熟练掌握后,排查问题、编写脚本、批量处理效率会大幅提升。
后续会持续更新Shell脚本实战、Linux优化、服务部署、自动化运维系列干货。如果本文对你有帮助,欢迎点赞、在看、收藏、转发,关注我,一起成为更专业的运维人!
摸鱼、通勤、碎片时间,不如来一局轻松的小游戏!《全民学霸》全新白蛇传版本上线,卡牌闯关玩法简单不费脑,点开就能玩,几分钟一局,既能打发时间又能解压!关注我,下次给你们整理更多摸鱼必备的轻量小游戏,快点击下方开玩👇#全民学霸 #摸鱼小游戏 #卡牌小游戏 #白蛇传 IP #轻量小游戏 #不用下载的游戏
请在微信客户端打开

