• 08 linux文件检索和编辑


    1. 检索命令

    1.1 检索文件内容

    1.1.1 cat

    作用 : 显示文件全部信息

    常用参数 :

    cat -n 文件      # 显示行号和内容
    
    [root@ymn ~]# cat -n ymn.txt
         1	ymn
         2	nihaiyigedahaoren
         3	dheugfymnchdg
         4	hahah
         5	ymn
         6	nihai
    

    补充 : nl 文件实现一样的显示行号和内容

    1.1.2 head

    作用 : 只显示文件前几行信息,默认前10行

    常用参数 :

    head -n 文件  # 显示文件前n行内容
    
    [root@ymn ~]# head num.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@ymn ~]# head -3 num.txt
    1
    2
    3
    

    1.1.3 tail

    作用 : 只显示文件后几行信息,默认后10行

    常用参数 :

    tail -n 文件  # 显示文件后n行内容
    tail -f 文件  # 阻塞住,一直显示文件新追加的内容,一般用作监听日志
    
    [root@ymn ~]# tail num.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [root@ymn ~]# tail -3 num.txt
    18
    19
    20
    

    1.1.4 grep

    作用 : 根据关键词匹配文件内容,以行的形式显示

    常用参数 :

    参数 作用
    -A n 检索关键信息包含后n行
    -B n 检索关键信息包含前n行
    -C n 以检索信息为中心显示上下n行
    -i 忽略大小写
    -E 识别特殊符号
    -v 忽略掉检索到的信息,返回剩下的信息
    [root@ymn ~]# grep 'mn' ymn.txt
    dheugfymnchdg
    ymn
    
    [root@ymn ~]# grep -A 2 'mn' ymn.txt    # 检索关键信息,包括后两行
    dheugfymnchdg
    hahah
    cnjdehfur
    ymn
    jvfrehgut
    nihai
    
    [root@ymn ~]# grep -B 2 'mn' ymn.txt
    jhuwgfuedvfh
    nihaiyigedahaoren
    dheugfymnchdg
    hahah
    cnjdehfur
    ymn
    
    [root@ymn ~]# grep -C 2 'mn' ymn.txt    # 检索关键信息,包括前后两行
    jhuwgfuedvfh
    nihaiyigedahaoren
    dheugfymnchdg
    hahah
    cnjdehfur
    ymn
    jvfrehgut
    nihai
    
    [root@ymn ~]# grep -i 'Mn' ymn.txt
    dheugfymnchdg
    ymn
    
    [root@ymn ~]# grep -E '^ymn' ymn.txt
    ymn
    
    [root@ymn ~]# grep -Ev '^ymn' ymn.txt
    jhuwgfuedvfh
    nihaiyigedahaoren
    dheugfymnchdg
    hahah
    cnjdehfur
    jvfrehgut
    nihai
    kcjfruiehf
    vjirjei
    vrgrt
    

    1.1.5 more和less

    作用 : 逐行或者逐页显示文件内容

    一般常用于大的文本文件的查看

    1.2 检索文件

    1.2.1 find

    作用 : 通过关键字查找对应的文件

    常用参数 :

    参数 作用
    -name 按照文件名查找
    -iname 忽略文件名的大小写
    -type 按照类型查找
    -size 按照大小查找
    -mtime 按照天数查找
    -mmin 按照分钟查找
    -user 按照文件属主查找
    -o 或者,可以连接参数 ( -size -100k or -size +10M)
    ! 取反
    # 查找当前路径下以.txt结尾的文件,*是通配符
    [root@ymn ~]# find ./ -name "*.txt"    #./可以不写
    ./test.txt
    ./num.txt
    ./d1/res.txt
    ./ymn.txt
    
    # 查找当前路径下所有的文件
    [root@ymn ~]# find ./ -type f
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./anaconda-ks.cfg
    ./.bash_history
    ./test.txt
    ./num.txt
    ./d1/res.txt
    ./ymn.txt
    ./.viminfo
    
    # 查找当前路径下所有的目录
    [root@ymn ~]# find ./ -type d
    .
    ./d1
    
    # 查找/etc/下大于5m的文件
    [root@ymn ~]# find /etc/ -type f -size +5M    #-5M是小于5m,5M是等于5m
    /etc/udev/hwdb.bin
    [root@ymn ~]# ls -lh /etc/udev/hwdb.bin
    -r--r--r--. 1 root root 7.6M Nov 30 10:16 /etc/udev/hwdb.bin
    
    # 查找/etc/下7天以前的文件
    [root@ymn ~]# find /etc/ -type f -mtime +7
    /etc/selinux/targeted/modules/active/README.migrated
    /etc/firewalld/zones/public.xml
    /etc/firewalld/zones/public.xml.old
    
    # 查找/etc/下7天以内的文件
    [root@ymn ~]# find /etc/ -type f -mtime -7
    /etc/selinux/targeted/modules/active/README.migrated
    /etc/firewalld/zones/public.xml
    /etc/firewalld/zones/public.xml.old
    
    # 查找/etc/下2分钟以内的文件      一般用于检测是否有新文件产生
    [root@ymn ~]# find /etc/ -type f -mmin -2
    
    # 查找/etc/下oldboy用户创建的文件
    [root@ymn ~]# find /etc/ -type f -user oldbody
    

    1.2.2 xargs

    作用 : 将标准输入转换成命令行参数

    一般和find连用,用来在精确查找之后在做下一步的操作,比如删除rm,但是要和|管道符号连用

    [root@ymn find]# ll -h
    total 111M
    drwxr-xr-x. 3 root root   41 Dec  3 09:43 a
    -rw-r--r--. 1 root root 100K Dec  3 10:03 data1.txt
    -rw-r--r--. 1 root root 300K Dec  3 10:03 data2.txt
    -rw-r--r--. 1 root root  10M Dec  3 10:03 data3.txt
    -rw-r--r--. 1 root root 100M Dec  3 10:03 data4.txt
    
    # 查找大于5m的文件,然后并删除
    [root@ymn find]# find ./ -type f -size +5M|xargs rm -rf
    [root@ymn find]# ll -h
    total 400K
    drwxr-xr-x. 3 root root   41 Dec  3 09:43 a
    -rw-r--r--. 1 root root 100K Dec  3 10:03 data1.txt
    -rw-r--r--. 1 root root 300K Dec  3 10:03 data2.txt
    

    2. vim编辑器

    linux操作系统下默认是没有vim编辑器的,在有网络的情况下可以通过命令下载 , 安装

    yum install vim -y
    

    2.1 什么是vim

    vim是linux系统下的一款编辑器,被称之为上古神器,但是学会如何使用有一定的门槛,常常劝退一些自认为

    自己很牛逼的点点程序员

    2.2 vim可以干什么

    vim可以摆脱鼠标,只通过键盘就能编辑文本,学会并熟练使用vim可以十分高效的在linux系统下完成文本的编辑

    那如何使用vim呢,我们来从他的三种模式下学习如何使用vim

    2.3 vim三种模式

    2.3.1 普通模式

    vim 文件名(如果文件不存在,会创建一个新文件并打开)

    普通模式下只能移动光标是无法编辑文本的

    普通模式下光标移动快捷键

    快捷键 作用
    G 将光标移动到文本最后一行
    gg 将光标移动到文本第一行
    0 将光标移到行头
    $ 将光标移到行尾
    ctrl + ( 将光标移动到第一个字符位置
    ctrl + ) 将光标移动到最后一个字符位置
    n回车 将光标向下移到n行
    ngg 将光标移动到第n行
    h,j,k,l 左下上右

    普通模式下的复制,粘贴,删除

    快捷键 作用
    yy 复制当前行
    nyy 复制n行
    p 粘贴
    dd 删除当前行
    ndd 删除n行
    u          撤销,类似windows的上一步
    ctrl + r   取消撤销操作
    .          下一步之类的
    

    2.3.2 编辑模式

    快捷键 作用
    i 在当前光标处插入文字
    esc 退出编辑模式,回到命令行模式

    2.3.3 命令行模式

    快捷键 作用
    :q 退出,不保存
    :q! 强制退出,不保存
    :wq 保存并退出
    :wq! 强制保存并退出
    :set nu 显示行号
    :set nonu 取消显示行号
  • 相关阅读:
    记录一次无法很好解决的问题
    java与进制转换
    花了点时间写了下测试框架
    利用eclipse或者pycharm编写monkeyrunner脚本,cmd打开应用“转转”并截图保存到D盘
    Instrumentation
    关于学生机受控应用的问题总结
    忙里偷闲一天
    linux下python3的安装(已安装python2的情况下)
    ROS上利用usb_cam读取摄像头图像
    ch8 -- directMethod
  • 原文地址:https://www.cnblogs.com/xcymn/p/14105122.html
Copyright © 2020-2023  润新知