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

cat

一次显示整个文件

-n:显示行号

-b :和 -n 相似,只不过对于空白行不编号

-s:当遇到有连续两行以上的空白行,就代换为一行的空白行

-E显示换行符

[root@localhost ~]# cat haha.txt -n
     1    /usr/bin/cp
     2    
     3    
     4    
     5    /usr/bin/gzip
[root@localhost ~]# cat haha.txt -b
     1    /usr/bin/cp



     2    /usr/bin/gzip
[root@localhost ~]# cat haha.txt -bs
     1    /usr/bin/cp

     2    /usr/bin/gzip
[root@localhost ~]# cat haha.txt -bsE
     1    /usr/bin/cp$
$
     2    /usr/bin/gzip$
View Code

从键盘创建一个文件

cat  >  filename

输入完内容后Ctrl+D可以保存。千万不要Ctrl+C,这样不会保存文件内容。

cat  >filename  <<EOF

也可以使用Ctrl+D,但是会警告 -bash: warning: here-document at line 36 delimited by end-of-file (wanted `EOF')

但是文件内容还是被保存下来了。

最好的做法还是按照规矩来,EOF结束文件输入

将几个文件合并为一个文件

cat   file1   file2  > file

more

分屏显示文本文件内容,从前往后翻,翻到文件的尾部 显示结束。结束时不能往前翻了

b键往前翻一屏  

回车 往下翻一行

空格 往下翻一屏

less

分屏显示

向下翻一行 向下键 或 回车

向上翻一行 向上键

  向下翻一屏 pagegdown 或 空格键

  向上翻一屏 pageup

  查找内容:

    /text 或 ?text

  :q退出less命令

head

 显示文本文件的前几行,默认显示前10行

改为显示前2行(-n可以省)

[root@51cto ~]# head -n2 install.log
Installing libgcc-4.4.7-3.el6.x86_64
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
[root@51cto ~]# head -2 install.log
Installing libgcc-4.4.7-3.el6.x86_64
warning: libgcc-4.4.7-3.el6.x86_64: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
View Code

tail

显示文本文件的后几行,默认显示后10行

改为显示前2行(-n可以省)

[root@51cto ~]# tail  -2 install.log
Installing rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***[root@51cto ~]# tail  -n2 install.log
Installing rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***[root@51cto ~]# 
View Code

-f  follow 一直跟踪 不再退出 ctrl+c结束

[root@51cto ~]# ping www.baidu.com > wangyan.txt 
[root@51cto ~]# tail -f wangyan.txt 
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=128 time=64.6 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=128 time=58.8 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=128 time=56.9 ms
64 bytes from 61.135.169.125: icmp_seq=4 ttl=128 time=127 ms
64 bytes from 61.135.169.125: icmp_seq=5 ttl=128 time=121 ms
64 bytes from 61.135.169.125: icmp_seq=6 ttl=128 time=117 ms
64 bytes from 61.135.169.125: icmp_seq=7 ttl=128 time=109 ms
View Code

.。。。。

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9108037.html