云计算应用管理-Linux命令行基础 find grep
]# hostname
]# which hostname #查询命令对应的程序
]# cat -n /etc/shells #显示行号
ls -l /etc/passwd #长格式显示
Ctrl + l:清空整个屏幕
Ctrl + u:从光标处清空至行首
Ctrl + w:往回删除一个单词(以空格界定)
alias hn='hostname' #定义别名
alias #查看系统中有哪些别名
unalias hn #删除hn别名
- mkdir — Make Directory
– 格式:mkdir [/路径/]目录名…
[-p]:连同父目录一并创建
[root@localhost /]# mkdir -p /opt/aa/bb/cc/dd
grep高级使用
作用:从文本文件内容中,过滤关键字符串
]# grep root /etc/passwd
]# grep -v root /etc/passwd #取反匹配
]# grep ^root /etc/passwd #以root开头
]# grep bash$ /etc/passwd #以bash结尾
^$:表示空行
]# cat /etc/default/useradd
]# grep -v ^$ /etc/default/useradd 显示不带空行的内容
Linux中大多数配置文件内容,以#开头的行为注释行
显示配置文件有效信息(去除以#开头的注释行和去除空行)
]# grep ^# /etc/default/useradd #以#号开头的行
]# grep -v ^# /etc/default/useradd #去除#号开头的行
]# grep -v ^# /etc/default/useradd | grep -v ^$
显示文件有效信息
]# grep -v ^# /etc/login.defs
]# grep -v ^# /etc/login.defs | grep -v ^$
]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/log.txt
]# cat /opt/log.txt
]# grep -v ^# /etc/login.defs | grep -v ^$ | wc -l
]# grep -v ^# /etc/man_db.conf > /opt/man.txt
]# cat /opt/man.txt
]# wc -l /opt/man.txt #统计行数
find精确查找
格式:find [目录] [条件1]
– 常用条件表示:
-type 类型(f、d、l)
-name "文档名称"
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 修改时间
-type 类型(f文本文件、d目录、l快捷方式)
[root@A /]# find /boot -type d
[root@A /]# find /opt -type d
[root@A /]# find /etc -type l
[root@A /]# find /boot -type f
[root@A /]# find /boot -type d | wc -l
[root@A /]# find /opt -type d | wc -l
[root@A /]# find /etc -type l | wc -l
u -name "文档名称"
]# find /etc/ -name "passwd"
]# find /etc/ -name "*tab"
]# find /etc/ -name "*.conf"
]# find /etc/ -name "*tab" | wc -l
]# find /etc/ -name "*.conf" | wc -l
]# find /etc/ -name "*.conf" | cat -n
]# mkdir /mnt/nsd01
]# mkdir /mnt/nsd02
]# touch /mnt/nsd03.txt
]# find /mnt/ -name "nsd*"
]# find /mnt/ -name "nsd*" -type d
]# find /mnt/ -name "nsd*" -type f
-size +或- 文件大小(k、M、G)
[root@A /]# find /boot/ -size +300k
[root@A /]# find /boot/ -size +10M
[root@A /]# find /boot/ -size +1M
[root@A /]# find /boot/ -size -2M
-user 用户名 (按照数据的所有者)
[root@A /]# useradd natasha #创建用户
[root@A /]# find /home/ -user natasha
[root@A /]# find / -user natasha
/proc:内存的数据,不占用硬盘空间
-mtime 修改时间 (所有的时间都是过去时间)
-mtime +90 #90天之前的数据
-mtime -90 #最近90天之内的数据
[root@A /]# find /root -mtime +90
[root@A /]# find /root -mtime +1000
[root@A /]# find /root -mtime -2
find高级使用
处理find找到的数据,每查找的一个就传递一次
– find [范围] [条件] -exec 处理命令 {} \;
-exec额外操作的开始
{} 永远表示前面find查找的结果
\; 额外操作的结束
find /boot/ -size +10M -exec ls -lh {} \;
find /boot/ -size +10M -exec cp {} /mnt \;
find / -user student -type f -exec cp {} /root/findfiles \;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~