• Linux文件内容查看相关指令


    Linux文件内容查看相关指令

    1.cat

    cat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]

    由第一行开始显示文件内容

    [root@centtos7 ~]# cat /etc/profile
    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    可以输入-n参数显示行数:

    [root@centtos7 ~]# cat -n /etc/profile
         1	# /etc/profile
         2	
         3	# System wide environment and startup programs, for login setup
         4	# Functions and aliases go in /etc/bashrc
         5	
         6	# It's NOT a good idea to change this file unless you know what you
         7	# are doing. It's much better to create a custom.sh shell script in
         8	# /etc/profile.d/ to make custom changes to your environment, as this
         9	# will prevent the need for merging in future updates.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用管道符进行翻页显示:

    [root@centtos7 ~]# cat -n /etc/profile | more
    
    • 1

    会出现翻页提示more,按住回车读取下一行,按住空格读取下一页:

    在这里插入图片描述


    2.more

    more命令用于查看纯文本文件(内容较多的),格式为“more [选项] 文件

    一页一页翻动显示文件

    [root@www ~]# more /etc/man_db.config 
    #
    # Generated automatically from man.conf.in by the
    # configure script.
    #
    # man.conf from man-1.6d
    ....(中间省略)....
    --More--(28%)  <== 重点在这一行喔!你的光标也会在这里等待你的命令
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在 more 这个程序的运行过程中,你有几个按键可以按的:

    • 空白键 (space):代表向下翻一页;
    • Enter :代表向下翻『一行』;
    • /字串 :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
    • :f :立刻显示出档名以及目前显示的行数;
    • q :代表立刻离开 more ,不再显示该文件内容。
    • b 或 [ctrl]+b :代表往回翻页,不过这动作只对文件有用,对管线无用。

    3.less

    less 与 more 类似,但是比 more 更好的是,他可以往前翻页!

    less在显示文件内容的时候,不是加载所有文件内容之后才显示,而是根据显示加载需要的内容,因此在查看超大文件时有较高的效率

    less /etc/profile
    
    • 1

    less运行时可以输入的命令有:

    • 空白键 :向下翻动一页;
    • [pagedown]:向下翻动一页;
    • [pageup] :向上翻动一页;
    • /字串 :向下搜寻『字串』的功能;
    • ?字串 :向上搜寻『字串』的功能;
    • n :重复前一个搜寻 (与 / 或 ? 有关!)
    • N :反向的重复前一个搜寻 (与 / 或 ? 有关!)
    • q :离开 less 这个程序;

    4.head

    取出文件前面几行

    选项与参数:

    • -n :后面接数字,代表显示几行的意思

    例如:取出文件的前10行:

    [root@centtos7 ~]# head -10 /etc/profile
    # /etc/profile
    
    # System wide environment and startup programs, for login setup
    # Functions and aliases go in /etc/bashrc
    
    # It's NOT a good idea to change this file unless you know what you
    # are doing. It's much better to create a custom.sh shell script in
    # /etc/profile.d/ to make custom changes to your environment, as this
    # will prevent the need for merging in future updates.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.tail

    取出文件后面几行

    tail命令最强悍的功能是-f参数可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用

    选项与参数:

    • -n :后面接数字,代表显示几行的意思
    • -f :表示持续侦测后面所接的文档名,要等到按下[ctrl]+c才会结束tail的侦测
    [root@centtos7 ~]# tail /etc/profile
            if [ "${-#*i}" != "$-" ]; then 
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done
    
    unset i
    unset -f pathmunge
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6.tr

    tr命令用于替换文本文件中的字符,格式为“tr [原始字符] [目标字符]

    在很多时候,我们想要快速地替换文本中的一些词汇,又或者把整个文本内容都进行替换,如果进行手工替换,难免工作量太大。这时,就可以先使用cat命令读取待处理的文本,然后通过管道符把这些文本内容传递给tr命令进行替换操作即可。

    例如,把某个文本内容中的英文全部替换为大写:🙌

    [root@centtos7 dahe]# cat hello.txt | tr [a-z] [A-Z]
    HELLO WORLD
    HELLO WORLD
    1234567890
    !!!
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.wc

    wc命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本

    在这里插入图片描述

    案例:如下,这三个值分别是行数,单词数,字节数:

    [root@centtos7 dahe]# wc hello.txt
     4  6 39 hello.txt
    
    • 1
    • 2

    8.stat

    stat命令用于查看文件的具体存储信息和时间等信息,格式为“stat 文件名称

    [root@centtos7 dahe]# stat hello.txt
      File: ‘hello.txt’
      Size: 39        	Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d	Inode: 1611614528  Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2022-10-04 06:18:35.383430370 -0400
    Modify: 2022-10-04 06:18:31.049425015 -0400
    Change: 2022-10-04 06:18:31.051425017 -0400
     Birth: -
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    9.cut

    cut命令用于按“列”提取文本字符,格式为“cut [参数] 文本

    我们使用下述命令尝试提取出passwd文件中的用户名信息:

    如果按列搜索,不仅要使用-f参数来设置需要看的列数,还需要使用-d参数来设置间隔符号🥭

    [root@centtos7 dahe]# cut -d: -f1 /etc/passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    10.diff

    diff命令用于比较多个文本文件的差异,格式为“diff [参数] 文件

    在使用diff命令时,不仅可以使用--brief参数来确认两个文件是否不同,还可以使用-c参数来详细比较出多个文件的差异之处,这绝对是判断文件是否被篡改的有力神器

    使用diff --brief命令显示比较后的结果,判断文件是否相同:

    [root@centtos7 dahe]# diff --brief hello.txt ni.txt
    Files hello.txt and ni.txt differ
    
    • 1
    • 2

    使用带有-c参数的diff命令来描述文件内容具体的不同:

    [root@centtos7 dahe]# diff -c hello.txt ni.txt
    *** hello.txt	2022-10-04 06:18:31.049425015 -0400
    --- ni.txt	2022-10-04 06:39:24.889943617 -0400
    ***************
    *** 1,4 ****
    ! hello world
    ! HELLO WORLD
    ! 1234567890
    ! !!!
    --- 1 ----
    ! nihaoa
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    c++语言基础:常量指针和指针常量的区别
    Unity UGUI透明区域点击无效
    带你走进脚本世界,ijkplayer之【init-ios.sh】脚本分析
    蛋白质相互作用
    【云原生】DevOps之使用Gitlab Runner进行CICD
    JAVA的异常
    c++:数据结构链表list的模拟实现
    SpringCloud-16-切换及自定义负载均衡策略
    Python OpenCV通过灰度平均值进行二值化处理以减少像素误差
    iOS 17上如何恢复数据?iOS 17 数据恢复软件
  • 原文地址:https://blog.csdn.net/Gherbirthday0916/article/details/126836779