Linux operation commands (3)

Linux operation commands (3)

1. Introduction to the experiment

1.1 Experiment content

This experiment will introduce the usage of more, less, head, and tail commands in Linux commands.

1.2 Experimental knowledge points

  • more command
  • less command
  • head command
  • tail command

1.3 Experimental Environment

The experimental environment used in the course is Ubuntu Linux 14.04 64-bit version. The program will be used in the experiment:

  • Xfce Terminal

2. Experimental steps

2.1 more command

The more command is similar in function to cat. The cat command displays the contents of the entire file on the screen from top to bottom. The more command will be displayed page by page, which is convenient for users to read page by page, and the most basic command is to press the space key (space) to display the next page, and press the b key to display the page back (back), and There is also a search string function. The more command reads the file from front to back, so the entire file is loaded at startup.

(1) Command format

more [options] file

(2) Common parameters

parameter describe
+n Display from the nth line
-n Define the screen size as n lines
+/pattern Search for the string (pattern) before each file is displayed, then start displaying after the first two lines of the string
-c Clear the screen from the top, then display
-d Prompt "Press space to continue, 'q' to quiet", disable the ringing function
-p Forms the file by clearing the window instead of scrolling, similar to the -c option
-s Display multiple consecutive blank lines as one line
-u Remove the underscore from the contents of the file

(3) Common operations

symbol describe
= print the line number of the current line
q exitmore
space bar scroll down one screen
b Return to previous screen

(4) Common examples

Please create a file shiyanlou.log with the following contents:

2014-11-5 a
2014-11-5 b
2014-11-5 c
2014-11-5 d
2014-11-5 e
2014-11-5 f
2014-11-5 g
2014-11-5 h
2014-11-5 e
2014-11-5 a
2014-11-5 b
2014-11-5 c
2014-11-5 d
2014-11-5 e
2014-11-5 f
2014-11-5 g
2014-11-5 h
2014-11-5 a
2014-11-5 b
2014-11-5 c
2014-11-5 d
2014-11-5 e

Example 1: From the fifth line to display the contents of the shiyanlou.log file, you can use the following command:

more +5 shiyanlou.log 

Alt text

Example 2: Find the first line in the shiyanlou.log file where the "g" string appears, and start displaying the output from the first two lines there, you can use the following command:

more +/g shiyanlou.log 

Alt text

例三:设定每屏行数为 5,可以使用如下命令:

more -5 shiyanlou.log 

Alt text

例四:使用 ll 和 more 命令显示/etc 目录信息,可以使用如下命令:

ll /etc | more -10

Alt text

每页显示 10 个文件信息,按 Ctrl+F 或者 空格键 将会显示下 10 条文件信息。

2.2 less 命令

less 工具也是对文件或其它输出进行分页显示的工具,应该说是 linux 正统查看文件内容的工具,功能极其强大。

(1)命令格式

less [选项] 文件

(2)常用参数

参数 描述
-e 当文件显示结束后,自动离开
-f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件
-i 忽略搜索时的大小写
-m 显示类似 more 命令的百分比
-N 显示每行的行号
-s 显示连续空行为一行

(3)常用操作

符号 描述
/字符串 向下搜索“字符串”的功能
?字符串 向上搜索“字符串”的功能
n 重复前一个搜索(与 / 或 ? 有关)
N 反向重复前一个搜索(与 / 或 ? 有关)
b 向前翻一页
d 向后翻半页
q 退出 less 命令
空格键 向后翻一页
向上键 向上翻动一行
向下键 向下翻动一行

(4)常用范例

例一:显示 shiyanlou.log 文件中的内容,并显示行号,可以使用如下命令:

less -N shiyanlou.log 

Alt text

例二:显示 shiyanlou.log 文件中的内容,搜索字符串”shiyanlou”,可以使用如下命令:

less  shiyanlou.log
/shiyanlou

Alt text

Alt text

例三:ps 查看进程信息并通过 less 分页显示 ,可以使用如下命令:

ps -f | less

Alt text

less 与 cat 和 more 的区别:

cat 命令功能:用于显示整个文件的内容,单独使用没有翻页功能。因此经常和 more 命令搭配使用,cat 命令还有就是可以将数个文件合并成一个文件的功能。

more 命令功能:让画面在显示满一页时暂停,此时可按空格健继续显示下一个画面,或按 q 键停止显示。

less 命令功能:less 命令的用法与 more 命令类似,也可以用来浏览超过一页的文件。所不同的是 less 命令除了可以按空格键向下显示文件外,还可以利用上下键来卷动文件。当要结束浏览时,只要在 less 命令的提示符“:”下按 q 键即可。

其实这三个命令除了 cat 命令有合并文件的功能,其余功能上相近,只是从浏览习惯和显示方式上有所不同。

2.3 head 命令

head 命令就像它的名字一样浅显易懂,主要是用来显示档案的开头至标准输出中,默认 head 命令打印其相应文件的开头 10 行。

(1)命令格式

head [选项] [文件]

(2)常用参数

参数 描述
-q 隐藏文件名
-v 显示文件名
-c<字节> 显示字节数
-n<行数> 显示的行数

(3)常用范例

例一:显示 shiyanlou.log 文件中的前 5 行内容,可以使用如下命令:

head -n 5 shiyanlou.log 

Alt text

例二:显示 shiyanlou.log 和 zhou.log 文件中的前 5 行内容,可以使用如下命令:

head -n 5 shiyanlou.log  zhou.log

Alt text

例三:显示 shiyanlou.log 文件中除了最后 5 行的内容,可以使用如下命令:

head -n -5 shiyanlou.log 

Alt text

2.4 tail 命令

tail 命令主要用于显示指定文件末尾内容。常用查看日志文件。

(1)命令格式

tail [选项] [文件]

(2)常用参数

参数 描述
-f 循环读取
-q 不显示处理信息
-v 显示详细的处理信息
-c<字节> 显示的字节数
-n<行数> 显示行数

(3)常用范例

例一:显示 shiyanlou.log 文件中的最后 5 行内容,可以使用如下命令:

tail -n 5 shiyanlou.log 

Alt text

例二:显示 shiyanlou.log 文件中的最后 5 行内容,当 shiyanlou.log 文件有新内容增加,自动更新显示。可以使用如下命令:

tail -n 5 -f shiyanlou.log 

Alt text

ping www.shiyanlou.com >> shiyanlou.log 这条命令作用是,ping 远程主机,并将信息追加到 shyanlou.log 文件中。& 的作用是将这条命令放在后台执行,这样 shiyanlou.log 文件就会一直有内容增加。说明一下,linux 下执行 ping 命令会一直执行,必须手动停止才行。而 windows 下执行 ping 命令时,发送一定请求后会自动停止。

Alt text

使用 tail 命令的-f 选项可以即时输出文件变化后追加的内容,tail -f filename 会把 filename 里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容。 另外顺便说一下怎么查看后台正在运行的任务及怎么停止任务。 jobs 命令可以查看正在后台运行的任务。kill 命令可以杀死一个任务,但要使用任务的 id。任务的 id 可以通过 ps 命令查看获得。

三、参考链接

本课程部分内容参考博文每天一个Linux命令,感谢作者peida提供的优质教程。

上一节:Linux操作命令(二) 下一节:Linux操作命令(四)

Guess you like

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