Home >Operation and Maintenance >Linux Operation and Maintenance >What is the command to view files in linux

What is the command to view files in linux

青灯夜游
青灯夜游Original
2021-02-07 10:36:4252090browse

Linux command to view files: 1. "cat", display the file content from the first line; 2. "tac", display from the last line; 3. "nl"; 4. "more" , display the file content page by page; 5. "less"; 6. "head", only look at the first few lines; 7. "tail", only look at the last few lines.

What is the command to view files in linux

#The operating environment of this tutorial: Red Hat Enterprise Linux 6.1 system, Dell G3 computer.

Linux commands to view the contents of log files are

  • cat displays the file contents starting from the first line
  • tac displays the contents starting from the last line, It can be seen that tac is cat written backwards
  • nl When displayed, the line number is output along the way!
  • more Displays the file content page by page
  • less is similar to more, but better than more, it can turn pages forward!
  • head Only look at the first few lines
  • tail only look at the tail lines

You can use man [command] to view each command Usage documentation, such as: man cp.

cat

Command function:

Display the file content starting from the first line

Command syntax:

cat [-AbEnTv]
一次显示整个文件。$ cat filename
2.从键盘创建一个文件。$ cat > filename 
只能创建新文件,不能编辑已有文件.
3.将几个文件合并为一个文件: $cat file1 file2 > file

Options and parameters

Options Parameters
-A is equivalent to the integration option of -vET, which can list some special characters instead of just blanks;
-b lists lines Number, only line numbers are displayed for non-blank lines, blank lines are not marked with line numbers!
-E Display the ending line break byte$;
-n column Print line numbers, and blank lines will also have line numbers, which is different from the -b option;
-T displays the [tab] key as ^I ;
-v List some invisible special characters

Related recommendations:《Linux Video Tutorial

Check the contents of /etc/issue:

cat filename  #一次显示整个文件.
cat > filename  #从键盘创建一个文件。
#只能创建新文件,不能编辑已有文件.
cat file1 file2 > file   #将几个文件合并为一个文件

tac

Command function:

tac is just the opposite of the cat command. The file content is displayed from the last line. It can be seen that tac is cat written backwards!

Command syntax:

tac [选项]

Example

[root@www ~]# tac /etc/issue

Kernel \r on an \m
CentOS release 6.4 (Final)

nl

Command function:

Display line number; nl commandRead the file parameter (standard input by default), calculate the line number in the input, and write the calculated line number to the standard output . In the output, the nl command evaluates the lines to the left based on the flags you specify on the command line. Input text must be written in a logical page. Each logical page has header, body and footer sections (can have empty sections). Unless the -p option is used, the nl command resets the line number at the beginning of each logical page. The line calculation flag can be set individually for header, body, and footer sections (for example, header and footer lines can be counted but text lines cannot). The default result is a little different from cat -n. nl can do more display designs for line numbers, including the number of digits and whether to automatically complete 0s, etc.

Command syntax:

nl [-bnw] 文件

Options and parameters:

  • -b: There are two main ways to specify the line number:
    • -b a: Indicates that the line number is also listed regardless of whether it is a blank line (similar to cat -n);
    • -b t: If there is a blank line, do not list the empty line Line number (default value);
  • -n: Lists the ways to represent line numbers. There are three main types:
    • -n ln: The line number is on the screen. Displayed at the far left;
    • -n rn: The line number is displayed at the far right of its own field without adding 0;
    • -n rz: The line number is displayed at the far right of its own field. Display on the right and add 0;
  • -w: The number of digits occupied in the line number field.

Example: Use nl to list the contents of /etc/issue

[root@www ~]# nl /etc/issue
     1  CentOS release 6.4 (Final)
     2  Kernel \r on an \m

more

Command function:

Flip page by page,more command is a text filter based on the vi editor. It displays the content of the text file page by page in full screen mode and supports keywords in vi Positioning operations. There are several shortcut keys built into the more list, the commonly used ones are H (get help information), Enter (scroll down one line), Space (scroll down one screen), Q (exit command).

Command syntax:

more(语法)(参数)

Options

Options Description
- Specify the number of lines displayed on each screen;
-d Display "[press space to continue,'q' to quit.]" and "[Press 'h' for instructions]";
-c No scrolling operation is performed. Refresh this screen every time;
-s Compress multiple blank lines into one line for display;
-u Underlines are prohibited;
Display starts from the line with the specified number.

实例

[root@www ~]# more /etc/man.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 :代表往回翻页,不过这动作只对文件有用,对管线无用。

less

命令功能:

less命令允许用户向前或向后浏览文件,而more命令只能向前浏览。用less命令显示文件时,用PageUp键向上翻页,用PageDown键向下翻页。要退出less程序,应按Q键。

命令语法:

less(选项)(参数)

选项

实验

一页一页翻动,以下实例输出/etc/man.config文件的内容:

[root@www ~]# less /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(中间省略)....
:   <== 这里可以等待你输入命令!

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

  • 空白键 :向下翻动一页;
  • [pagedown]:向下翻动一页;
  • [pageup] :向上翻动一页;
  • /字串 :向下搜寻『字串』的功能;
  • ?字串 :向上搜寻『字串』的功能;
  • n :重复前一个搜寻 (与 / 或 ? 有关!)
  • N :反向的重复前一个搜寻 (与 / 或 ? 有关!)
  • q :离开 less 这个程序;

命令功能:

head命令用于显示文件的开头的内容。在默认情况下,head命令显示文件的头10行内容。

命令语法:

head [-n number] 文件

选项与参数:

选项说明
-e文件内容显示完毕后,自动退出;
-f强制显示文件;
-g不加亮显示搜索到的所有关键词,仅显示当前显示的关键字,以提高显示速度;
-l搜索时忽略大小写的差异;
-N每一行行首显示行号;
-s将连续多个空行压缩成一行显示;
-S在单行显示较长的内容,而不换行显示;
-x<数字>将TAB字符显示为指定个数的空格字符。

例子

  • -n :后面接数字,代表显示几行的意思
[root@www ~]# head /etc/man.config

默认的情况中,显示前面 10 行!若要显示前 20 行,就得要这样:

[root@www ~]# head -n 20 /etc/man.config

tail

命令功能:

取出文件后面几行,tail命令用于输入文件中的尾部内容。tail命令默认在屏幕上显示指定文件的末尾10行。如果给定的文件不止一个,则在显示的每个文件前面加一个文件名标题。如果没有指定文件或者文件名为“-”,则读取标准输入。

命令语法:

语法:

tail [-n number] 文件

选项与参数:

选项说明
-n<数字>指定显示头部内容的行数;
-c<字符数> 指定显示头部内容的字符数;
-v 总是显示文件名的头信息;
-q 不显示文件名的头信息。
选项 说明
--retry 即是在tail命令启动时,文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件。使用此选项时需要与选项“——follow=name”连用;
-c或——bytes= 输出文件尾部的N(N为整数)个字节内容;
-f或--follow 显示文件最新追加的内容。“name”表示以文件名的方式监视文件的变化。“-f”与“-fdescriptor”等效;
-F 与选项“-follow=name”和“--retry"连用时功能相同;
-n或——line= 输出文件的尾部N(N位数字)行内容。
--pid=<进程号> 与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令;
-q或——quiet或——silent 当有多个文件参数时,不输出各个文件名;
-s<秒数>或——sleep-interal=<秒数> 与“-f”选项连用,指定监视文件变化时间隔的秒数;
-v或——verbose 当有多个文件参数时,总是输出各个文件名;
--help 显示指令的帮助信息;
--version 显示指令的版本信息。
  • -n :后面接数字,代表显示几行的意思
  • -f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测
[root@www ~]# tail /etc/man.config
# 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:
[root@www ~]# tail -n 20 /etc/man.config

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of What is the command to view files in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to check ip in linuxNext article:How to check ip in linux