我处理大量巨大的日志文件。我通常使用Putty或Telnet连接到我的(Telnet)UNIX和Linux服务器,然后使用标准的UNIX/Linux命令读取文件。其他 IT 人员更喜欢使用 X-window GUI,但如果您的连接带宽较低,最好使用命令行终端更快地完成工作。
如果您需要浏览日志而无需编辑它们,请使用“ less ”命令。Linux和UNIX中的“Less”允许您查看文件并允许您执行基本的查找操作。与“更多(more)”不同,它允许向前和向后导航,因此您可以在文档中的任何位置查找任何搜索字符串。
少胜于多
要使用less打开文件,请在(less)UNIX或Linux提示符中键入less命令,后跟文件名:
less filename
在下面的示例中,我打开了一个名为verbose.log.mo的文件(verbose.log.mo)
Less将打开文件并在终端的左下角显示文件名。要在文件中查找字符串,请键入正斜杠,后跟要搜索的字符串,然后按 Enter(Enter)。在下面的示例中,我输入/error来搜索字符串“error”。
你会看到文件的视图会跳转到字符串所在的行。找到的字符串匹配会突出显示,以便于查看。
要搜索字符串的下一个实例,只需按“n”键。按大写“N”将带您进入上一场比赛。
以下是我从Wiki 的“less”条目中(Wiki’s entry on “less”)找到的其他键盘快捷键:
- [箭头]/[Page Up]/[Page Down]/[Home]/[End]:导航(Navigation)。
- [空格键]:下一页。
- b : 上一页。
- n g:跳转到第n行。默认是文件的开头。
- n G : 跳转到第n行。默认是文件的结尾。
- /pattern:搜索模式(pattern)。可以使用正则表达式。
- n:转到下一个匹配项(成功搜索后)。
- N : 转到上一场比赛。
- m字母:用(letter)字母(letter)标记当前位置。
- '信(letter):回位信(letter)。[ ' = 单引号]
- ‘^或g:转到文件的开头。
- ‘$或G:转到文件末尾。
- s:将当前内容(从另一个程序,如grep获取)保存在文件中。
- =:文件信息。
- F : 不断地从文件中读取信息并跟随它的结尾。对日志观察很有用。使用Ctrl + C退出此模式。
- –选项(option):切换命令行选项 –选项(option)。
- h:帮助。
- 问(q):退出。
我最喜欢“less”的地方在于它会自动根据终端窗口的宽度和高度进行调整。如果您调整终端窗口的大小,使用“less”查看的文本会自动换行。这比称为“更多”的旧工具更先进,后者在您收紧终端宽度时倾向于剪切文本。
如果您在Linux(Linux)或UNIX中有大量文本或ASCII文件要查看,请确保掌握“less”,因为它是使用命令行终端查看文件的高级命令。享受!
Why the “less” Command is Better Than “more” in UNIX and Linux
I work with a lot of huge log files. I usually connect to my UNIX and Linux servers using Putty or Telnet, then I read the files using standard UNIX/Linux commаnds. Other IT fоlks prefer to use an X-window GUI, but if yoυ have a low-bandwidth connection, it is better to use a сommand line terminal to get things done faster.
If you need to browse logs without having to edit them, use the “less” command. “Less” in Linux and UNIX lets you view the file and allows you to do basic find operations. Unlike “more”, it allows for both forward and backward navigation so you can look for any search string at any point in the document.
Less Is Better Than More
To open a file using less, type the less command followed by the file name in the UNIX or Linux prompt:
less filename
In the example below I opened a file called verbose.log.mo
Less will open the file and display the file name at the lower left portion of the terminal. To find a string in the file, type forward slash followed by the string that you want to search and then hit Enter. In the example below, I typed /error to search for the string “error”.
You will see that the view of the file will jump to the line where the string is located. The located string matches are highlighted for easier viewing.
To search for the next instance of the string just press the “n” key. Pressing the uppercase “N” will bring you to the previous match.
Here are other keyboard shortcuts I found from Wiki’s entry on “less”:
- [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.
- [Space bar]: Next page.
- b: Previous page.
- ng: Jump to line number n. Default is the start of the file.
- nG: Jump to line number n. Default is the end of the file.
- /pattern: Search for pattern. Regular expressions can be used.
- n: Go to next match (after a successful search).
- N: Go to previous match.
- mletter: Mark the current position with letter.
- ‘letter: Return to position letter. [‘ = single quote]
- ‘^ or g: Go to start of file.
- ‘$ or G: Go to end of file.
- s: Save current content (got from another program like grep) in a file.
- =: File information.
- F: continually read information from file and follow its end. Useful for logs watching. Use Ctrl+C to exit this mode.
- –option: Toggle command-line option –option.
- h: Help.
- q: Quit.
What I like most about “less” is that it automatically adjusts with the width and height of the terminal window. The text when viewed with “less” is automatically wrapped if you resize the window of the terminal. This makes less more advance than the older tool called “more”, which tends to cut the text as you tighten the width of the terminal.
If you have a lot of text or ASCII files to view in Linux or UNIX, make sure to master “less” as it is a superior command for viewing files using the command line terminal. Enjoy!