1、使用cat命令查看dir1文件内容(如果没有该文件,需要先创建文件)
[wuhan@localhost ~]$ mkdir dir1
mkdir: 无法创建目录"dir1": 文件已存在
[wuhan@localhost ~]$ cat dir1
cat: dir1: 是一个目录
[wuhan@localhost ~]$ cd dir1
[wuhan@localhost dir1]$ ll
总用量 4
-rw-rw-r--. 1 wuhan wuhan 0 3月 18 23:28 a1.txt
-rw-rw-r--. 1 wuhan wuhan 0 3月 18 23:28 a2.txt
-rw-r--r--. 1 wuhan wuhan 20 3月 25 18:48 dir1
drwxrwxr-x. 2 wuhan wuhan 6 3月 25 18:49 file1
[wuhan@localhost dir1]$ cat file1
welcome
abc
def
ghi
[wuhan@localhost dir1]$
2、使用nl命令计算文件中行号:
(1)利用nl命令列出文件的全部内容,包括行号,但是空白行不加行号。
[wuhan@localhost dir1]$ nl file1
1 welcome
2 abc
3 def
4 ghi
[wuhan@localhost dir1]$
(2)利用nl命令的参数b列出file1的内容,空白行也要加上行号。
[wuhan@localhost dir1]$ nl -b a file1
1 welcome
2 abc
3 def
4 ghi
5
6 jkl
[wuhan@localhost dir1]$
(3)使用命令让行号前面自动补上0,统一输出格式,行号前默认补0,
[wuhan@localhost dir1]$ nl -b a -n rz file1
000001 welcome
000002 abc
000003 def
000004 ghi
000005
000006 jkl
[wuhan@localhost dir1]$
3、使用more命令分页查看文件内容:
(1)以分页方式查看文件名file1的内容
[wuhan@localhost dir1]$ more file1
welcome
abc
def
ghi
jkl
mno
pqr
[wuhan@localhost dir1]$
(2)分页查看install.log文件,按“Q”键退出查看
[wuhan@localhost ~]$ cd dir1
[wuhan@localhost dir1]$ su
密码:
[root@localhost dir1]# more /root/install.log
/root/install.log: 没有那个文件或目录
[root@localhost dir1]# pwd
/home/wuhan/dir1
[root@localhost dir1]# more /home/wuhan/dir1/root/install.log
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
installing setup-2.5.58-7.el5.noarch
[root@localhost dir1]#
4、使用less命令查看文件file1的内容
less file1
5、使用head命令查看文件内容
[root@localhost dir1]# head -5 /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
[root@localhost dir1]#
6、使用tail命令查看文件内容
[root@localhost dir1]# head -5 /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
[root@localhost dir1]# tail -5 /etc/passwd
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
wuhan:x:1000:1000:wuhan:/home/wuhan:/bin/bash
[root@localhost dir1]#
拓展练习:
1、新建一个文件test.log并录入内容
[wuhan@localhost ~]$ touch test.log
2、使用cat命令查看文件test.log的内容
[wuhan@localhost ~]$ cd test
[wuhan@localhost test]$ cat test.log
3、使用nl命令计算文件test.log中的行号
[wuhan@localhost test]$ nl -b a test.log
4、使用more命令分页查看文件test.log的内容
[root@localhost test]# more /home/wuhan/test/test.log
5、使用less命令查看文件test.log的内容
less test.log
6、使用head命令查看文件test.log的前五行内容。
[wuhan@localhost test]$ pwd
/home/wuhan/test
[wuhan@localhost test]$ head -5 /home/wuhan/test/test.log
7、使用tail命令查看文件test.log的后6行内容
[wuhan@localhost test]$ tail -5 /home/wuhan/test/test.log