> 文档中心 > Linux的读取文件信息命令详解

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


1、cat

  • 将整个文档加载到内存中, 并进行一次性显示
  • 除非后面使用管道, 传递数据
[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-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 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-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 informationauth --enableshadow --passalgo=sha512# Use CDROM installation mediacdrom# Use graphical installgraphical# Run the Setup Agent on first bootfirstboot --enableignoredisk --only-use=sda# Keyboard layoutskeyboard --vckeymap=us --xlayouts='us'# System languagelang en_US.UTF-8# Network informationnetwork  --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activatenetwork  --hostname=CentOS-7-64.19-08# Root passwordrootpw --iscrypted $6$k/vlIdW730sbeDdt$HuO/aw9Fq.iQT6RFhsPl4ckkDlTGCa2zsNts1DOZaDTYXgxN9w3S9OJf4ZQuqEJjt.jY/J0fYZ.VXvPkN7kRH0# System servicesservices --disabled="chronyd"# System timezonetimezone Asia/Shanghai --isUtc --nontp# System bootloader configurationbootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sdaautopart --type=lvm# Partition clearing informationclearpart --none --initlabel%packages@^minimal@corekexec-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 informationauth --enableshadow --passalgo=sha512# Use CDROM installation mediacdrom# Use graphical installgraphical# Run the Setup Agent on first bootfirstboot --enableignoredisk --only-use=sda

5、tail

从文章末尾读取N行

[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-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%anacondapwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notemptypwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyokpwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty%end

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

定位文件的第三行

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

[root@CentOS-7-64 ~]# lltotal 8-rw-------. 1 root root 1255 May 11 13:10 anaconda-ks.cfgdrwxr-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 -1auth --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