Linux的读取文件信息命令详解

1、cat

  • 将整个文档加载到内存中, 并进行一次性显示
  • 除非后面使用管道, 传递数据
[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x  4 root root   29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root   64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# cat test2.txt
世界, 你好(hello world)!
床前明月光, 疑是地上霜.

2、tac

将整个文档加载到内存中, 并进行一次性按行逆序显示

[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x  4 root root   29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root   64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# tac test2.txt

床前明月光, 疑是地上霜.
世界, 你好(hello world)!
[root@CentOS-7-64 ~]#

3、more/less

分页查看文档内容

快捷键

  • 回车: 下一行
  • 空格: 下一页
  • b: 回退
  • q: 退出
[root@CentOS-7-64 ~]# more 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
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network  --hostname=CentOS-7-64.19-08

# Root password
rootpw --iscrypted $6$k/vlIdW730sbeDdt$HuO/aw9Fq.iQT6RFhsPl4ckkDlTGCa2zsNts1DOZaDTYXgxN9w3S9OJf4ZQuqEJjt.jY/J0fYZ.VXvPkN7kRH0
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

%packages
@^minimal
@core
kexec-tools

%end

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

%end

4、head

从文章开始读取N行

默认如果超过10行读取10行, 否则读取现在行数

[root@CentOS-7-64 ~]# 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

5、tail

从文章末尾读取N行

[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x  4 root root   29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root   64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# 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

拓展一: 如果想精确定位到某一行可以将head命令和tail命令结合起来使用

定位文件的第三行

分析: 利用管道只读取第N行, 管道的作用就相当于把前面的结果以参数的方式传递给后面的的命令

[root@CentOS-7-64 ~]# ll
total 8
-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfg
drwxr-xr-x  4 root root   29 May 13 21:24 bbb1
-rwxr-xr-x  1 root root   64 May 13 22:30 test2.txt
[root@CentOS-7-64 ~]# head -3 anaconda-ks.cfg | tail -1
auth --enableshadow --passalgo=sha512

拓展二: 读取新增数据

ping www.baidu.com >> baiduConfig.txt    //将baidu发送的数据包的数据内容保存到baiduConfig.txt文件中;

使用格式: tail -f/F baiduConfig.txt

如果f:

  • 它会监听指定inode的文件数据变化(就是stat命令得到的那个id), 但是当文件被删除后即使重新创建, inode也会发生变化, 于是监听失败.

如果F:

  • 他会监听指定名字的文件, 如果文件删除后, 重新创建, 它会重新监听新文件的数据变化, 监听不受影响.

6、find

查找指定的文件

find要查找的范围 -name名字

举例: find /etc -name profile  //在etc文件的目录下查找name为profile的文件或者文件夹

[root@CentOS-7-64 ~]# find -name aaa
./bbb1/aaa

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值