Check several commands for viewing files in linux [cat, more, less, tail, head]

1. cat is a tool to display the content of the file connection file 

cat is a text file (viewing) and (connecting) tool, usually used with more, unlike more cat can combine files.

To view the content of a file, it is relatively simple to use cat, that is, the file name is directly followed by cat. 

Such as: # cat /etc/passwd  1. cat syntax: cat [options] [files]...  options    -A, --show-all is equivalent to -vET    -b, --number-nonblank for non-empty output line number    -e is equivalent to -vE    -E, --show-ends show $    -n at the end of each line, --number number all lines of output    -s, --squeeze-blank do not output multiple blank lines    -t is equivalent to -vT    -T, --show-tabs display tab characters as ^I    -u (ignored)    -v, --show-nonprinting use ^ and M- quoting, except LFD and TAB        --help Display this help information and leave  2. cat View file content example: [root
  
 

  












 
@localhost etc ]# cat /etc/ yum.conf      Note: View the content of the yum.conf  file in the /etc/ directory; 
[root@localhost etc]# cat -n yum.conf     Note: For yum.conf in the /etc directory All lines of conf (including blank lines) are numbered and output displayed;
     1 [main]
     2 cachedir=/var/cache/yum/$basearch/$releasever
     3 keepcache=0
     4 debuglevel=2
     5 logfile=/var/log/ yum.log

cat  For files with large content , it can be transferred to the more tool through the pipeline |, and then viewed page by page; 
[ @localhost etc ]# cat /etc/ yum.conf | more  3. Creation of cat , Example of connection file function: cat has the function of creating a file. After creating a file, it must end with EOF or STOP;  [root@localhost ~]# cat > test.txt << EOF    (EOF end input flag) > 1
  
 





> 2

> EOF  

cat also has the function of appending content to an existing file; 
[root@localhost ~]# cat >> test.txt << EEE (EEE ends the input flag, any non-conflict can be used)
> 3
> 4
> 5
> EEE  

cat concatenates the contents of multiple files and outputs to a new file; 

Note: " > " means create, ">>" means append. Don't get confused.

[root@localhost ~]# cat t1.txt t2.txt t3.txt > test.txt 
[root@localhost ~]# cat t1.txt t2.txt t3.txt >> test.txt 

2. More file content or output viewing tool 

more is one of our most commonly used tools, the most commonly used is to display the output content, and then paginate according to the size of the window, and then also prompt the percentage of the file; 

#ll -l /etc >test.txt (add test.txt content)

# more /etc/test.txt 

#cat -n /etc/test.txt |more

1. More syntax, parameters and commands: 

more [参数选项] [文件] 
参数如下: 
+num   从第num行开始显示; 
-num   定义屏幕大小,为num行; 
+/pattern   从pattern 前两行开始显示; 
-c   从顶部清屏然后显示; 
-d   提示Press space to continue, 'q' to quit.(按空格键继续,按q键退出),禁用响铃功能; 
-l    忽略Ctrl+l (换页)字符; 
-p    通过清除窗口而不是滚屏来对文件进行换页。和-c参数有点相似; 
-s    把连续的多个空行显示为一行; 
-u    把文件内容中的下划线去掉退出more的动作指令是q 

2、more 的参数应用举例:  
[root@localhost ~]# more -dc /etc/profile    注:显示提示,并从终端或控制台顶部显示; 
[root@localhost ~]# more +4 /etc/profile      注:从profile的第4行开始显示; 
[root@localhost ~]# more -4 /etc/profile      注:每屏显示4行; 
[root@localhost ~]# more +/MAIL /etc/profile     注:从profile中的第一个MAIL单词的前两行开始显示; 

3、more 的动作指令:  
我们查看一个内容较大的文件时,要用到more的动作指令,比如ctrl+f(或空格键) 是向下显示一屏,ctrl+b是返回上一屏; Enter键可以向下滚动显示n行,要通过定,默认为1行; 
Enter       向下n行,需要定义,默认为1行; 
Ctrl+f    向下滚动一屏; 
空格键          向下滚动一屏; 
Ctrl+b  返回上一屏; 
=         输出当前行的行号; 
:f      输出文件名和当前行的行号; 
v      调用vi编辑器; 
! 命令            调用Shell,并执行命令; 
q     退出more当我们查看某一文件时,想调用vi来编辑它,不要忘记了v动作指令,这是比较方便的; 

4、其它命令通过管道和more结合的运用例子:  
比如我们列一个目录下的文件,由于内容太多,我们应该学会用more来分页显示。这得和管道 | 结合起来,比如: 
[root@localhost ~]# ls -l /etc |more 

三、less 查看文件内容 工具 

less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大;您是初学者,我建议您用less。由于less的内容太多,我们把最常用的介绍一下; 

1、less的语法格式:  
less [参数] 文件 
   常用参数 
-c 从顶部(从上到下)刷新屏幕,并显示文件内容。而不是通过底部滚动完成刷新; 
-f 强制打开文件,二进制文件显示时,不提示警告; 
-i 搜索时忽略大小写;除非搜索串中包含大写字母; 
-I 搜索时忽略大小写,除非搜索串中包含小写字母; 
-m 显示读取文件的百分比; 
-M 显法读取文件的百分比、行号及总行数; 
-N 在每行前输出行号; 
-p pattern 搜索pattern;比如在/etc/profile搜索单词MAIL,就用 less -p MAIL /etc/profile 
-s 把连续多个空白行作为一个空白行显示; 
-Q 在终端下不响铃; 
  
比如:我们在显示/etc/profile的内容时,让其显示行号; 
# less -N    /etc/test.txt 
  

四、head 工具,显示文件内容的前几行 

head 是显示一个文件的内容的前多少行; 
用法比较简单; 
head -n 行数值 文件名; 

比如我们显示/etc/profile的前10行内容,应该是: 
[root@localhost ~]# head -n 10 /etc/profile 

五、tail 工具,显示文件内容的最后几行 

tail 是显示一个文件的内容的最后多少行; 

用法比较简单; 
tail   -n 行数值 文件名; 

比如我们显示/etc/profile的最后5行内容,应该是: 
[root@localhost ~]# tail -n 5 /etc/profile 

tail -f /var/log/syslog 显示文件 syslog 的后十行内容并在文件内容增加后,且自动显示新增的文件内容。 

备注:最后一条命令tail非常有用,尤其在监控日志文件时,可以在屏幕上一直显示新增的日志信息。 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734466&siteId=291194637