Linux文件内容查看相关指令

简介: 1.catcat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]”由第一行开始显示文件内容



1.cat


cat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]

由第一行开始显示文件内容


[root@centtos7 ~]# cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
...


可以输入-n参数显示行数:


[root@centtos7 ~]# cat -n /etc/profile
     1  # /etc/profile
     2  
     3  # System wide environment and startup programs, for login setup
     4  # Functions and aliases go in /etc/bashrc
     5  
     6  # It's NOT a good idea to change this file unless you know what you
     7  # are doing. It's much better to create a custom.sh shell script in
     8  # /etc/profile.d/ to make custom changes to your environment, as this
     9  # will prevent the need for merging in future updates.


使用管道符进行翻页显示:


[root@centtos7 ~]# cat -n /etc/profile | more


会出现翻页提示more,按住回车读取下一行,按住空格读取下一页:



2.more


more命令用于查看纯文本文件(内容较多的),格式为“more [选项] 文件”

一页一页翻动显示文件


[root@www ~]# more /etc/man_db.config 
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(中间省略)....
--More--(28%)  <== 重点在这一行喔!你的光标也会在这里等待你的命令


在 more 这个程序的运行过程中,你有几个按键可以按的:


空白键 (space):代表向下翻一页;

Enter :代表向下翻『一行』;

/字串 :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;

:f :立刻显示出档名以及目前显示的行数;

q :代表立刻离开 more ,不再显示该文件内容。

b 或 [ctrl]+b :代表往回翻页,不过这动作只对文件有用,对管线无用。


3.less


less 与 more 类似,但是比 more 更好的是,他可以往前翻页!


less /etc/profile


less运行时可以输入的命令有:


空白键 :向下翻动一页;

[pagedown]:向下翻动一页;

[pageup] :向上翻动一页;

/字串 :向下搜寻『字串』的功能;

?字串 :向上搜寻『字串』的功能;

n :重复前一个搜寻 (与 / 或 ? 有关!)

N :反向的重复前一个搜寻 (与 / 或 ? 有关!)

q :离开 less 这个程序;


4.head


取出文件前面几行

选项与参数:

-n :后面接数字,代表显示几行的意思

例如:取出文件的前10行:


[root@centtos7 ~]# head -10 /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.


5.tail


取出文件后面几行

tail命令最强悍的功能是-f参数可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用

选项与参数:


-n :后面接数字,代表显示几行的意思
-f :表示持续侦测后面所接的文档名,要等到按下[ctrl]+c才会结束tail的侦测
[root@centtos7 ~]# tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done
unset i
unset -f pathmunge


6.tr


tr命令用于替换文本文件中的字符,格式为“tr [原始字符] [目标字符]”

在很多时候,我们想要快速地替换文本中的一些词汇,又或者把整个文本内容都进行替换,如果进行手工替换,难免工作量太大。这时,就可以先使用cat命令读取待处理的文本,然后通过管道符把这些文本内容传递给tr命令进行替换操作即可。

例如,把某个文本内容中的英文全部替换为大写:🙌


[root@centtos7 dahe]# cat hello.txt | tr [a-z] [A-Z]
HELLO WORLD
HELLO WORLD
1234567890
!!!


7.wc


wc命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本



案例:如下,这三个值分别是行数,单词数,字节数:


[root@centtos7 dahe]# wc hello.txt
 4  6 39 hello.txt


8.stat


stat命令用于查看文件的具体存储信息和时间等信息,格式为“stat 文件名称”


[root@centtos7 dahe]# stat hello.txt
  File: ‘hello.txt’
  Size: 39          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d  Inode: 1611614528  Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:default_t:s0
Access: 2022-10-04 06:18:35.383430370 -0400
Modify: 2022-10-04 06:18:31.049425015 -0400
Change: 2022-10-04 06:18:31.051425017 -0400
 Birth: -


9.cut


cut命令用于按“列”提取文本字符,格式为“cut [参数] 文本”

我们使用下述命令尝试提取出passwd文件中的用户名信息:

如果按列搜索,不仅要使用-f参数来设置需要看的列数,还需要使用-d参数来设置间隔符号


[root@centtos7 dahe]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
...


10.diff


diff命令用于比较多个文本文件的差异,格式为“diff [参数] 文件”


在使用diff命令时,不仅可以使用--brief参数来确认两个文件是否不同,还可以使用-c参数来详细比较出多个文件的差异之处,这绝对是判断文件是否被篡改的有力神器


使用diff --brief命令显示比较后的结果,判断文件是否相同:


[root@centtos7 dahe]# diff --brief hello.txt ni.txt
Files hello.txt and ni.txt differ


使用带有-c参数的diff命令来描述文件内容具体的不同:


[root@centtos7 dahe]# diff -c hello.txt ni.txt
*** hello.txt 2022-10-04 06:18:31.049425015 -0400
--- ni.txt  2022-10-04 06:39:24.889943617 -0400
***************
*** 1,4 ****
! hello world
! HELLO WORLD
! 1234567890
! !!!
--- 1 ----
! nihaoa


目录
相关文章
|
20天前
|
Linux
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
65 2
|
21天前
|
Linux
Linux 服务器下载百度网盘文件
本教程指导如何使用 `bypy` 库从百度网盘下载文件。首先通过 `pip install bypy` 安装库,接着运行 `bypy info` 获取登录链接并完成授权,最后将文件置于指定目录并通过 `bypy downdir /Ziya-13b-v1` 命令下载至本地。
28 1
Linux 服务器下载百度网盘文件
|
14天前
|
Ubuntu Linux Shell
Linux系统命令 安装和文件相关命令
本文档详细介绍了Linux系统中的常用命令,包括软件安装卸载命令如`dpkg`和`apt-get`,压缩与解压命令如`gzip`、`bzip2`和`xz`,以及`tar`命令用于打包和解包。此外还介绍了文件分割命令`split`,文件操作命令如`cat`、`head`、`tail`、`more`、`less`等,管道命令和`wc`、`grep`、`find`、`cut`、`sort`、`uniq`、`diff`等实用工具。最后,文档还讲解了文件属性相关的命令如`chmod`、`chown`、`chgrp`以及创建硬链接和软链接的`ln`命令。
|
23天前
|
Linux
linux 删除乱码文件名的文件
【8月更文挑战第26天】当遇到文件名显示为乱码,导致无法正常通过键盘输入文件名进行删除操作时,可以利用鼠标的复制功能配合`rm`命令实现删除。对于文件夹的删除,可使用`rm -rf 目录名`。然而,有时这种方式仍无法删除某些特殊乱码文件,这时可以通过获取文件的i节点号(使用`ls -i`或`ll -i`命令查看)并执行`find -inum [节点号] -delete`来进行删除。这种方法特别适用于处理那些因文件名问题而难以删除的情况。
67 2
|
24天前
|
JSON Linux 网络安全
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
|
20天前
|
JavaScript Linux
Linux中和文件相关的操作
Linux中和文件相关的操作
24 0
|
21天前
|
存储 人工智能 Ubuntu
Linux指令学习(一)
Linux指令学习(一)
50 0
|
22天前
|
Linux
Linux0.11 文件打开open函数(五)
Linux0.11 文件打开open函数(五)
30 0
|
24天前
|
前端开发 JavaScript Linux
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
|
24天前
|
Java Linux 网络安全
【Azure 应用服务】App Service for Linux环境中,如何解决字体文件缺失的情况
【Azure 应用服务】App Service for Linux环境中,如何解决字体文件缺失的情况