Linux-cat、more、less、head、tail命令

cat 命令:

cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的。

参数:
    cat具体命令格式为 : cat [-AbeEnstTuv] [--help] [--version] fileName

    说明:把档案串连接后传到基本输出(屏幕或加 > fileName 到另一个档案)

    -n 或 –number 由 1 开始对所有输出的行数编号
    -b 或 –number-nonblank 和 -n 相似,只不过对于空白行不编号
    -s 或 –squeeze-blank 当遇到有连续两行以上的空白行,就代换为一行的空白行
    -v 或 –show-nonprinting

应用范例:
    [root@centos7-02 ~]# cat > test.txt << EOF
    > 1231231
    > 123123
    > 123123
    > 123123
    > EOF
    [root@centos7-02 ~]# cat test.txt 
    1231231
    123123
    123123
    123123
    [root@centos7-02 ~]# cat >test.txt << EOF
    > 122222222222222222222222
    > EOF
    [root@centos7-02 ~]# cat test.txt 
    122222222222222222222222
    [root@centos7-02 ~]# cat >> test.txt <<EOF
    > 222222222222222
    > 333333333333333
    > 444444444444444
    > EOF
    [root@centos7-02 ~]# cat test.txt 
    122222222222222222222222
    222222222222222
    333333333333333
    444444444444444
    [root@centos7-02 ~]# 
    -------------------------------------------------------------------------
    [root@centos7-02 ~]# cat > test1.txt << EOF
    > wo wo wowo
    > dflkajlkfjlak
    > slfkal
    > EOF
    [root@centos7-02 ~]#cat test.txt test1.txt  > huangxin.txt
    [root@centos7-02 ~]# ls
    anaconda-ks.cfg  huangxin.txt  test1.txt  test.txt
    [root@centos7-02 ~]# cat huangxin.txt 
    122222222222222222222222
    222222222222222
    333333333333333
    444444444444444
    wo wo wowo
    dflkajlkfjlak
    slfkal
    [root@centos7-02 ~]#

more 命令:

命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作。more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻滚一行),空格(向下滚动一屏),Q(退出命令)

应用范例:
    显示文件file的内容,每10行显示一次,而且在显示之前先清屏。
    [root@centos7-02 ~]# cat -n anaconda-ks.cfg 
     1	#version=DEVEL
     2	# System authorization information
     3	auth --enableshadow --passalgo=sha512
     4	# Use CDROM installation media
     5	cdrom
     6	# Use graphical install
     7	graphical
     8	# Run the Setup Agent on first boot
     9	firstboot --enable
    10	ignoredisk --only-use=sda

    #version=DEVEL
    # System authorization information
    auth --enableshadow --passalgo=sha512
    # Use CDROM installation media
    cdrom
    # Use graphical install
    graphical
    # Run the Setup Agent on first boot
    firstboot --enable
    ignoredisk --only-use=sda

less命令:

less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。less 的用法比起 more 更加的有弹性。 在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup] [pagedown] 等按 键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜。

参数:
b <缓冲区大小> 设置缓冲区的大小

-e  当文件显示结束后,自动离开

-f  强迫打开特殊文件,例如外围设备代号、目录和二进制文件

-g  只标志最后搜索的关键词

-i  忽略搜索时的大小写

-m  显示类似more命令的百分比

-N  显示每行的行号

-o <文件名> 将less 输出的内容在指定文件中保存起来

-Q  不使用警告音

-s  显示连续空行为一行

-S  行过长时间将超出部分舍弃

-x <数字> 将“tab”键显示为规定的数字空格

特殊按键:

/字符串:向下搜索“字符串”的功能

?字符串:向上搜索“字符串”的功能

n:重复前一个搜索(与 / 或 ? 有关)

N:反向重复前一个搜索(与 / 或 ? 有关)

b  向后翻一页

d  向后翻半页

h  显示帮助界面

Q  退出less 命令

u  向前滚动半页

y  向前滚动一行

空格键 滚动一行

回车键 滚动一页

[pagedown]: 向下翻动一页

[pageup]:   向上翻动一页

应用范例:

   1. 通过less分页显示同时显示行号
    cat anaconda-ks.cfg | less -N

    1 #version=DEVEL
      2 # System authorization information
      3 auth --enableshadow --passalgo=sha512
      4 # Use CDROM installation media
     5 cdrom
      6 # Use graphical install
      7 graphical
      8 # Run the Setup Agent on first boot
      9 firstboot --enable
     10 ignoredisk --only-use=sda
     11 # Keyboard layouts
     12 keyboard --vckeymap=cn --xlayouts='cn
     :
    
    2.浏览多个文件
    less huangxin.txt test1.txt

    122222222222222222222222
    222222222222222
    333333333333333
    444444444444444
    wo wo wowo
    dflkajlkfjlak
    slfkal

    ~
    ~
    ~
    ~
    huangxin.txt (file 1 of 2) (END) - Next:test1.txt

     注意:
     输入 :n后,切换到 test1.txt   
     输入 :p 后,切换到huangxin.txt

    3.当正在浏览一个文件时,也可以使用 :e命令 打开另一个文件。

head 命令:

用于显示文件的开头的内容。在默认情况下,head命令显示文件的头10行内容。

参数:
-n<数字>:指定显示头部内容的行数;
-c<字符数>:指定显示头部内容的字符数;
-v:总是显示文件名的头信息;
-q:不显示文件名的头信息。

应用范例:
[root@centos7-02 ~]# head anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
[root@centos7-02 ~]# head -n 3  anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
[root@centos7-02 ~]# head -5 anaconda-ks.cfg 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
[root@centos7-02 ~]# 

tail 命令:

inux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案内容。

参数:
-f 该参数用于监视File文件增长。
-c Number 从 Number 字节位置读取指定文件
-n Number 从 Number 行位置读取指定文件。
-m Number 从 Number 多字节字符位置读取指定文件,比方你的文件假设包括中文字,假设指定-c参数,可能导致截断,但使用-m则会避免该问题。
-b Number 从 Number 表示的512字节块位置读取指定文件。
-k Number 从 Number 表示的1KB块位置读取指定文件。

应用范例:
[root@centos7-02 ~]# tail anaconda-ks.cfg 

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@centos7-02 ~]# 
[root@centos7-02 ~]# tail -3 anaconda-ks.cfg 
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@centos7-02 ~]# tail -n 2 anaconda-ks.cfg 
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

转载于:https://my.oschina.net/u/3866154/blog/1824791

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值