Viewing text files on Linux
来源:互联网 发布:aris visio 软件 编辑:程序博客网 时间:2024/06/06 09:14
A lot of times, we have the need to view text files on a Linux box, whether it be logs or scripts or what have you. This post is more like a cheat-sheet for me on using thecat
, head
, tail
, more
and less
commands, but with examples and some screenshots.
Short version
head <filename>
- View the top few lines of a file-<lines>
- Displays the first <lines> number of lines of a file
tail <filename>
- View the bottom few lines of a file-<lines>
- Displays the last <lines> number of lines of a file-f
: continually watch for any additions at the end of the file-f --pid=PID
- continually display any additions until process with PID terminates-f -s <sec>
- continually display any additions at intervals of<sec>
seconds
cat <filename>
- View the whole file-n
: line-numbered output-b
: line-numbered output with no line numbers for blank lines-s
: multiple blank lines compressed into a single blank line
more <filename>
- View the whole file, one screenful at a time.spacebar
: View next screenb
: View previous screend
: View next half-screenEnter
: View next line=
: Current line number in filev
: Startvi
editor on current line/string
: Search for string in filen
: Go to next occurrence of string'
: Go to first occurrence of string
less <filename>
- Same asmore
, but with many more features. Displays the portion of the file without waiting for the entire file to be read by it. Accepts most of themore
commands.Pg Dn
: View next screenPg Up
: View previous screenUp arrow
: View previus lineDown arrow
: View next line
Long Version
head
You can use the
head
command to see the first few lines of a text file. This can come in handy especially when you have a long file and want to see its title or comments present at the top and don't want to scroll all the way there.nitin@jane-saucy:~$ head long_test.txt ## A sample "Lorem Ipsum" dummy text file for testing purposes#Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus.Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.
You can use the
-<lines>
switch to see a particular number of lines starting from the first line of the file.nitin@jane-saucy:~$ head -5 long_test.txt ## A sample "Lorem Ipsum" dummy text file for testing purposes#Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.
tail
tail
lets you see the last few lines of a text file. This comes in handy when you're checking log files, etc.nitin@jane-saucy:~$ tail long_test.txtNulla facilisi.Praesent neque justo, accumsan nec elit eu, gravida sagittis leo.Vivamus id aliquet lacus.Cras viverra, metus quis dignissim gravida, mi odio porta tortor, ut venenatis ipsum ipsum sit amet nibh.Integer in magna in nibh facilisis porta in ut libero. Nam vestibulum eros at consequat euismod.
The
-f
switch let's you monitor a file for lines being appended to the file. The-f --pid=<PID>
continually monitors a file until process with the PID terminates. The-f -s <sec>
continually displays additions if any at intervals of<sec>
seconds.Sorry about not being able to show you examples for the
-f
switch. It works better when you watch it live in action.cat
Although you generally use cat to concatenate strings to other strings or append them to files and what not, just typing
cat <filename>
displays the contents of a file on the screen.nitin@jane-saucy:~$ cat test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit.Suspendisse lacinia.
You can also view the file with line numbers using
-n
switch.nitin@jane-saucy:~$ cat -n long_test.txt 1 # 2 # A sample "Lorem Ipsum" dummy text file for testing purposes 3 # 4 5 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit. 6 7 Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus. 8 9 Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.
You can also view the files with line numbers only for the lines containing text with the
-b
switch.nitin@jane-saucy:~$ cat -b long_test.txt 1 # 2 # A sample "Lorem Ipsum" dummy text file for testing purposes 3 #
4 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit. 5 Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus. 6 Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat. 7 Quisque et tortor nisl. Nunc sit amet pretium mi. Donec fringilla tellus vitae justo aliquet fermentum. 8 Praesent tristique feugiat diam, id tempus dolor rhoncus a. 9 Ut ornare ligula nunc, eu faucibus enim bibendum sit amet. Quisque dignissim semper ultrices.
You can also compress multiple blank lines into a single blank line using the
-s
switch. Here's a comparison,nitin@jane-saucy:~$ cat -n blank_test.txt 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit. 2 3 4 5 6 7 Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus. 8 9 10 11 12 13 Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.nitin@jane-saucy:~$ cat -ns blank_test.txt 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit. 2 3 Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus. 4 5 Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.
more
You can use
more
to read through a file right from the beginning. Like,tail -f
, it's better to see this in action yourself than me using screenshots, so I leave that up to you. I will however provide a few screenshots where possible.Navigating
spacebar
lets you view the next screenful of textb
View previous screenful of textd
lets you view the next half-screen of textEnter
lets you view the next line of text=
displays the current line number in file
Searching
To search for a string when
more
is running, type a/
followed by the string. For example, if I wanted to search for the wordet
, I'd type/et
and hit enter. To go to the next occurrence of the string, hit then
key and to find the first occurrence of the string, hit the'
key.Opening
vi
text editor at a lineTo open the
vi
text editor at a particular line, hit thev
key.less
less
takes the most of commands thatmore
does but also packs a more powerful punch. Withless
you don't have to wait for the file to be completely read byless
and can start viewing the content that's already been buffered.Some easier navigation in the
less
tool are as follows.Pg Dn
lets you view next screenPg Up
lets you view previous screenUp arrow
lets you view previous lineDown arrow
lets you view next line
https://2buntu.com/articles/1491/viewing-text-files-on-linux-cat-head-tail-more-and-less/
- Viewing text files on Linux
- Text-Terminals on Linux
- Lab01 Editing text files on the command line
- Linux Log files and usage on Ubuntu
- How to recover deleted files on Linux
- 10 Awesome Examples for Viewing Huge Log Files in Unix
- Installing MDX on ubuntu for viewing results of roi_pac
- Importing text files with Bulk Insert on MS SQL Server 2000
- Linux Log Files Location And How Do I View Logs Files on Linux?
- Find The Largest Top 10 Files and Directories on Linux
- read and X permission on files in Linux
- How to diff and merge files or directories on Linux
- Delete Files Older Than x Days on Linux
- Linux Commands for Beginners--Viewing Resources--du,df,free commands
- linux------------------Files
- Reading and writing text files
- Reading and writing text files
- Reading and writing text files
- C++学习之路指针记录笔记(一)
- svm 出现 Y must be a vector or a character array的错误
- hadoop之 hadoop用途方向
- LaTeX常见问题汇总
- Pycharm获取注册码
- Viewing text files on Linux
- php穷举思维探究百钱白鸡问题
- 程序员应聘必备词汇
- Kotlin学习笔记一(基本语法)
- python实现-字串的连接最长路径查找
- LARC DL笔记(五):pysol toolbox
- 我的oracle数据库旅程
- UE4 美术学习笔记和经验
- 数组的中length的理解