博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
View the start/end of a file linux
阅读量:4030 次
发布时间:2019-05-24

本文共 2792 字,大约阅读时间需要 9 分钟。

tail - lets you view the LAST 10 lines in a file

head - shows you the FIRST 10 lines in a file by default

Introduction

There are two very useful commands in Linux which let you see part of a file. The first is called  and by default it shows you the first 10 lines in a file. The second is the tail command which by default lets you view the last 10 lines in a file.

Imagine the file you are reading has 300,000 lines in it. Imagine also that the file consumes a lot of disk space.

A common use for the head command is to make sure that the file you want to view is indeed the correct file. 

You can usually tell if you are looking at the correct file just by seeing the first few lines.

The tail command is useful for viewing the last few lines of files and is very good when you want to see .

How To Specify The Number Of Lines To Show

Maybe you want to see more than the last 10 lines of the file. You can specify the number of lines you want to see using the following command:

sudo tail -n20 <filename>

The above example would show the last 20 lines of the file.

Perhaps you know the first 30 rows in a file are comments and you just want to see the data within a file. In this case you would use the following command:

sudo tail -n+20 <filename>

The tail command is often use alongside the more command so that you can read the file a page at a time.

For example:

sudo tail -n+20 <filename> | more

The above command sends the last 20 lines from filename and pipes it as the input to the more command:

You can also use the tail command to show a certain number of bytes instead of lines:

sudo tail -c20 <filename>

Again you can use the same switch to start showing from a certain byte number as follows:

sudo tail -c+20 <filename>

How To Monitor A Log FIle

There are many scripts and programs that don't output to the screen but do append to a log file as they are running.

In this instance you might want to monitor the log file as it changes. You can use the following tail command to check how the log changes every so many seconds:

sudo tail -F -s20 <filename>

You can also use tail to continue monitoring a log until a process dies as follows:

sudo tail -F --pid=1234 <filename>

To find the process id for a process you can use the following command:

ps -ef | grep <programname>

For example imagine you are editing a file using nano. You can find the process ID for nano using the following command:

ps -ef | grep nano

The output from the command will give you a process ID. Imagine the process ID is 1234.

You can now run tail against the file being edited by nano using the following command:

sudo tail -F --pid=1234 <filename>

Every time the file is saved within nano the tail command will pick up the new lines at the bottom. The command only stops when the nano editor is closed.

转载地址:http://ihhbi.baihongyu.com/

你可能感兴趣的文章
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
系统架构:Web应用架构的新趋势---前端和后端分离的一点想法
查看>>
JVM最简生存指南
查看>>
漂亮的代码,糟糕的行为——解决Java运行时的内存问题
查看>>
Java的对象驻留
查看>>
自己动手写GC
查看>>
Java 8新特性终极指南
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++指针常量与常量指针详解
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
STL::deque以及由其实现的queue和stack
查看>>
CS4344驱动
查看>>