【Linux系列教程】多种查看命令的使用方法

  1. cat
  2. more
  3. less
  4. head
  5. tail

一、cat 查看文件全部内容

[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# cat -n /etc/hosts
     1	127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2	::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

查看Linux发行版本

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

二、more 和 less 分页查看

[root@localhost ~]# more /usr/share/dict/words 	#不可向前翻页
[root@localhost ~]# less /usr/share/dict/words #可向前向后随意翻页
  • 回车 按行查看
  • 空格 按页查看
  • q 退出

三、head 查看头部

[root@localhost ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@localhost ~]# head -n 1 /etc/passwd 
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# head /etc/passwd	#不加任何参数,默认查看前十行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

四、tail 查看尾部

[root@localhost ~]# tail -n 3 /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
martin:x:1000:1000:martin:/home/martin:/bin/bash
[root@localhost ~]# tail /etc/passwd	#不加任何参数,默认查看后十行
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
martin:x:1000:1000:martin:/home/martin:/bin/bash

五、拓展

1. file 查看文件类型

[root@localhost ~]# file /etc/hosts
/etc/hosts: ASCII text

[root@localhost ~]# file /boot/vmlinuz-3.10.0-1160.el7.x86_64 
/boot/vmlinuz-3.10.0-1160.el7.x86_64: Linux kernel x86 boot executable bzImage, version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) #1 , RO-rootFS, swap_dev 0x6, Normal VGA

2. wc 统计

[root@localhost ~]# wc /etc/hosts
  2  10 158 /etc/hosts

[root@localhost ~]# wc -l /etc/hosts	#统计文件行数
2 /etc/hosts