Linux入门(四)对文件操作命令(重要)

目录

文件内容操作和历史命令(cat、more、less、head、tail、tailf、vim、history、clear)

cat:查看文件并且将文件内容标准输出到屏幕上

cat -n:显示行号输出

分页显示:more,less

head -n:显示文件的的前n行

tail :默认显示文件后十行

tail -n:显示文件的前n行

tail -n +3:显示文件第三行及之后的行

tail -f=tailf:动态的监控文件的末尾的变化,如果有新的内容,马上显示出来

vim:文本编辑器工具

history:查看历史命令

clear:清除屏幕


文件内容操作和历史命令(cat、more、less、head、tail、tailf、vim、history、clear)

cat:查看文件并且将文件内容标准输出到屏幕上

cat - concatenate files and print on the standard output

-n, --number number all output lines

concatenate 连结,拼接

standard output 标准输出: linux里的标准输出是输出到屏幕

standard input 标准输入: linux里的标准输入是从键盘输入

cat -n:显示行号输出
[root@sc-chenlu lianxi]# cat -n  hosts
     1	127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2	::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@sc-chenlu lianxi]# cat hosts -n
     1	127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2	::1         localhost localhost.localdomain localhost6 localhost6.localdomain6


[root@sc-chenlu lianxi]# nl hosts
     1	127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2	::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

nl空行不给编号 ,cat -n 空行也编号

分页显示:more,less

more命令
用途:全屏方式分页显示文件内容
交互操作方法:
Enter键向下逐行滚动
空格键向下翻一屏、按b键向上翻一屏 back
q键退出 quit

会自动退出

less命令
用途:与more命令相同,但扩展功能更多
交互操作方法:
按Enter键向下逐行滚动
按空格键向下翻一屏、按b键向上翻一屏 back
按q键退出 quit
【page down】【page up】上翻下翻页
不会自动退出

cat、more和less的区别?
​ cat 默认会将整个文件里的内容加载到内存,读取里面的内容,会在短时间内消耗大量的内存 --》瞬间显示大量的内容
more和less慢慢读取内存,不会在短时间内消耗大量的内存空间 —》分页显示

​ 不要使用cat 去读取大文件,会消耗整个服务器大量的资源(内存资源)

head 接文件名:默认显示文件的前十行

[root@sc-chenlu lianxi]# head  weijunlin.txt
welcome to sanchuang,hello world 0
welcome to sanchuang,hello world 1
welcome to sanchuang,hello world 2
welcome to sanchuang,hello world 3
welcome to sanchuang,hello world 4
welcome to sanchuang,hello world 5
welcome to sanchuang,hello world 6
welcome to sanchuang,hello world 7
welcome to sanchuang,hello world 8
welcome to sanchuang,hello world 9
head -n:显示文件的的前n行

head -5 = head -n -5

[root@sc-chenlu lianxi]# head -5  weijunlin.txt
welcome to sanchuang,hello world 0
welcome to sanchuang,hello world 1
welcome to sanchuang,hello world 2
welcome to sanchuang,hello world 3
welcome to sanchuang,hello world 4

tail :默认显示文件后十行

tail -n:显示文件的前n行

tail -n -3 = tail -n 3 = tail -3 #取文件的最后三行

[root@sc-chenlu lianxi]# tail  -3  weijunlin.txt
welcome to sanchuang,hello world 997
welcome to sanchuang,hello world 998
welcome to sanchuang,hello world 999
[root@sc-chenlu lianxi]# tail  -1  weijunlin.txt
welcome to sanchuang,hello world 999
tail -n +3:显示文件第三行及之后的行
[root@wh lianxi]# tail -n -3 num.txt 
8
9

[root@wh lianxi]# tail -n 3 num.txt 
8
9

[root@wh lianxi]# tail -3 num.txt 
8
9

[root@wh lianxi]# tail -n +3 num.txt 
3
4
5
6
7
8
9

[root@wh lianxi]# df -Th    #查看文件系统磁盘的使用情况
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  898M     0  898M    0% /dev
tmpfs                   tmpfs     910M     0  910M    0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M    2% /run
tmpfs                   tmpfs     910M     0  910M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  6.9G   11G   41% /
/dev/sda1               xfs      1014M  151M  864M   15% /boot
tmpfs                   tmpfs     182M     0  182M    0% /run/user/0

[root@wh lianxi]# df -Th |tail -n +2
devtmpfs                devtmpfs  898M     0  898M    0% /dev
tmpfs                   tmpfs     910M     0  910M    0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M    2% /run
tmpfs                   tmpfs     910M     0  910M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        17G  6.9G   11G   41% /
/dev/sda1               xfs      1014M  151M  864M   15% /boot
tmpfs                   tmpfs     182M     0  182M    0% /run/user/0

查看文件的第八行

[root@sc-chenlu lianxi]# cat -n weijunlin.txt |head  -8 |tail -1
    8	welcome to sanchuang,hello world 7

tail -f=tailf:动态的监控文件的末尾的变化,如果有新的内容,马上显示出来

[root@mysql-proxy-1 lianxi]# tail -f shimengmeng.txt



lilanqing

lilanqing

lilanqing

lilanqing

^C --》按ctrl+c强行终止

另外一个终端,重定向内容

[root@mysql-proxy-1 lianxi]# echo  "lilanqing" >>shimengmeng.txt
[root@mysql-proxy-1 lianxi]# echo  "lilanqing" >>shimengmeng.txt
[root@mysql-proxy-1 lianxi]# echo  "lilanqing" >>shimengmeng.txt
[root@mysql-proxy-1 lianxi]# echo  "lilanqing" >>shimengmeng.txt

vim:文本编辑器工具

history:查看历史命令

!number 执行第多少条历史命令
!100 执行第100条历史命令

!string 执行最近以string开头的命令
!bash 执行最近的一条命令以bash开头的

上下方向键–》调到以前的历史命令

clear:清除屏幕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值