linux运维--vim编辑详细过滤

本文介绍了Linux系统中常用的grep文本搜索命令,包括大小写敏感、匹配模式、查找范围等。同时,详细讲解了find命令的高级用法,如查找无主文件和按修改时间过滤,以及结合FHS理解文件系统结构。此外,还涵盖了vi/vim文本编辑器的基础操作和重定向/管道在复杂任务中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux基础命令03


1.grep

grep:文本查找 -i:不区分大小写 -v:显示没有被匹配的行

-o:显示匹配的行 -E:正则表达式 -q:静默模式

#过滤
#查找hello
[root@clq ~]# grep 'hello' haha 
hello world
hello world you

#不区分大小写查找
[root@clq ~]# grep -i 'hello' haha 
hello world
hello world you
HELLO
HELLO

#显示被匹配的行
[root@clq ~]# grep -o 'hello world' haha 
hello world
hello world

#正则表达式查找
[root@clq ~]# grep -E 'hell|HELL' haha 
hello world
hello world you
HELLO
HELLO

#查找本内容以及后俩行
[root@clq ~]# grep -A 2 'hello' haha 
hello world
hello world you
HELLO
HELLO

#查找本内容以及前一行
[root@clq ~]# grep -B 1 'HELL' haha
hello world you
HELLO
HELLO


2.find

语法:find / -name 文件

-iname:不区分大小写 - nouser:查找没有属主的文件

-nogroup:查找没有属组的文件

-mtime:修改时间

-fls :查找到的所有文件的长格式信息保存至指定文件中

-ok :对查找到的每个文件执行COMMAND,每次操作都需要用
-exec :对查找到的每个文件执行COMMAND,操作不需要确认

#查找没有属主的文件
[root@clq ~]# ll
-rw-r--r--. 1 1314 1314    40 Apr 12 09:09 haha
[root@clq ~]# find / -nouser
find:/proc/107625: No such file or directory
find:/proc/107646: No such file or directory
find:/proc/107647/task/107647/fd/7: No such file or directory
find:/proc/107647/task/107647/fdinfo/7: No such file or directory
find:/proc/107647/fd/8: No such file or directory
find:/proc/107647/fdinfo/8: No such file or directory
/root/haha

#修改时间
[root@clq ~]# find /mnt/ -mtime +2
/mnt/
/mnt/abcd
/mnt/abcd/c
/mnt/abcd/c/hehe
/mnt/abcd/a
/mnt/abcd/b
/mnt/abcd/b/haha
/mnt/abcd/d
/mnt/abc

#组合使用
[root@clq ~]# find / -type d -size -5 -iname oppo  #f:文件 d:目录 
/root/oppo

#
[root@clq mnt]# find -type d -fls oppo
[root@clq mnt]# cat oppo 
 50791665      0 drwxr-xr-x   3  root     root           41 Apr 12 10:08 .
 51180968      0 drwxr-xr-x   6  root     root           42 Mar 24 19:25 ./abcd
  2807771      0 drwxr-xr-x   3  root     root           18 Mar 24 19:21 ./abcd/c
 17586741      0 drwxr-xr-x   2  root     root            6 Mar 24 19:21 ./abcd/c/hehe
 34275514      0 drwxr-xr-x   2  root     root            6 Mar 24 19:21 ./abcd/a
 51391554      0 drwxr-xr-x   3  root     root           18 Mar 24 19:21 ./abcd/b
  2807781      0 drwxr-xr-x   2  root     root            6 Mar 24 19:21 ./abcd/b/haha
 17586744      0 drwxr-xr-x   2  root     root            6 Mar 24 19:21 ./abcd/d
    
    
[root@clq mnt]# ls
file1  file2  file3  file4  file5  oppo
[root@clq mnt]# find ./ -name file3 -exec ls -l {} \;
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3

相对安全一点
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3
[root@clq mnt]# find ./ -name file3 -ok ls -lh {} \;
< ls ... ./file3 > ? y
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3

3.文件层级系统(FHS)

/(根目录)

/bin(普通执行命令)

/sbin(管理执行命令)

/boot(启动)(相关的文件)

/dev(驱动)

/etc(配置)

/home(普通用户)

/root(管理员家目录超级管理员)

run(运行)

tmp(临时目录)

/usr(应用程序)/local #第三方软件安装目录
/var(日志,邮件mail)

media(媒体/挂u盘)

/proc /sys (伪文件)

srv(服务相关)

/lib /静态库/动态库


4.重定向/管道
>:输出                     <:输入
>>:追加输出                 <<追加输入
2>:错误输出                
2>>:错误追加输出
&>覆盖重定向标准输出或错误输出至同一个文件
&>>追加重定向标准输出或错误输出至同一个文件
0(键盘)1(显示器)2(显示器)3(文件)

标准输入--stdin
[root@clq ~]# echo '123456' |passwd  --stdin oppo
Changing password for user oppo.
passwd: all authentication tokens updated successfully.
#管道(piping):可以将多个命令组合起来,一次性完成复杂的任务
#查看root的第一条信息 
#grep管道组合使用
[root@clq mnt]# cat /etc/passwd |grep root|head -1
root:x:0:0:root:/root:/bin/bash
[root@clq mnt]# cat /etc/passwd |grep root |cut -d: -f1
root
operator

                        
#tee管道:三通管道交给其它程序处理保存副本
[root@clq mnt]# touch 999       #创建一个文件夹999
[root@clq mnt]# cat /etc/passwd |tee 999 |head -2   
                           ###查看/etc/passwd的开头第2行并且保存到副本999
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@clq mnt]# cat 999
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
,,,

#xargs(参数传递)(特殊cp和rm)
[root@clq mnt]# ls     #文件
file1  file2  file3  file4  files.txt
[root@clq mnt]# cat files.txt 
/mnt/file1             #文件路径
/mnt/file4
#删除文件路径就能删除指定文件  
#通过文件路径利用管道命令删除不了!!
[root@clq mnt]# cat files.txt |rm -rfv 
[root@clq mnt]# ls
file1  file2  file3  file4  files.txt
俩个方法删除:
[root@clq mnt]# cat files.txt |rm -rfv *  #只能删全部
[root@clq mnt]# cat files.txt |xargs rm -rfv  #删指定文件

5.文本编辑vi/vim
安装vim
[root@clq ~]# dnf -y install vim

模式:

模式:1.insert(插入)<esc+i,l,a,o
	 2.(末行模式命令模式)<esc+shift+:

功能操作:

进入文本第5行
[root@clq ~]# vim +5 file01

在这里插入图片描述

hjkl:左下上右
末行模式关闭文件:q!:不保存退出
			   wq!:强行保存并退出
			   x:强行保存并退出
			   X:给文本加一个密码
模行模式下:yy复制
		  p:粘贴
		  dd:删除 ndd:删n行
          D:删除光标的内容
		  s:删一个指定的字
		  v:可视化
          u:恢复
          set nu:显示列表数字
          set nonu:取消列表数字
          /:查看
gg:页首 GG:页尾  n(gg GG)移动n个字符
h(左)j(下)k(上)l(右)n(hjkl)移动n个字符
         :w 保存一样的别名文本
        :n s///g ^删掉

vimdiff:查看文本差异

[root@clq ~]# vimdiff file01 file02

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神慕蔡蔡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值