1.tail命令 (显示指定文件尾部内容)
tail -f filename
打印filename文件尾部的内容(默认10行,相当于添加参数-n 20)control +c 退出
tail -n 20 filename
打印出filename文件的最后10行
tail -n +20 filename
显示filename文件前20行
tail -r -n 10 filename
逆序显示filename文件的最后10行
**********************************
和tail相似的命令有:
cat:从第一行开始显示文件的内容
tac:从最后一行开始显示文件的内容
more:分页显示文件内容,
less:分页显示文件内容,且支持向前翻页
head:显示前面几行
tail:仅显示后面几行
n:带行号显示内容
od:二进制显示档案内容
-----------------------------------------------------------------------------------------------
2.grep命令(过滤或搜索特定字符)
命令格式:grep [option] pattern file
ps -ef | grep svn 查找指定进程
[root@localhost ~]# ps -ef|grep svn
root 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/
root 16867 16838 0 19:53 pts/0 00:00:00 grep svn
ps -ef | grep svn -c (ps -ef| grep -c svn )查找指定进程的个数
[root@localhost ~]# ps -ef|grep svn -c
2
[root@localhost ~]# ps -ef|grep -c svn
2
[root@localhost ~]#
cat test2.txt |grep -f test1.txt 从指定文件中读取关键词进行搜索
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -f test2.txt
hnlinux
ubuntu linux
Redhat
linuxmint
[root@localhost test]#
cat test2.txt | grep -nf test1.txt从文件中读取关键词进行搜索,并显示行号
[root@localhost test]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost test]# cat test2.txt
linux
Redhat
[root@localhost test]# cat test.txt | grep -nf test2.txt
1:hnlinux
4:ubuntu linux
6:Redhat
7:linuxmint
[root@localhost test]#
grep 'linux' test.txt 从文件中查找关键词
[root@localhost test]# grep 'linux' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost test]# grep -n 'linux' test.txt
1:hnlinux
4:ubuntu linux
7:linuxmint
[root@localhost test]#
grep 'linux' test1.txt test2.txt 从多个文件中查找关键字
[root@localhost test]# grep -n 'linux' test.txt test2.txt
test.txt:1:hnlinux
test.txt:4:ubuntu linux
test.txt:7:linuxmint
test2.txt:1:linux
[root@localhost test]# grep 'linux' test.txt test2.txt
test.txt:hnlinux
test.txt:ubuntu linux
test.txt:linuxmint
test2.txt:linux
[root@localhost test]#
cat test.txt | grep^u 找出以u开头的行内容
[root@localhost test]# cat test.txt |grep ^u
ubuntu
ubuntu linux
[root@localhost test]#
cat test.txt | grep^[^u] 非u开哦图的行内容
[root@localhost test]# cat test.txt |grep ^[^u]
hnlinux
peida.cnblogs.com
redhat
Redhat
linuxmint
[root@localhost test]#
cat test.txt | grep hat& 输出以hat结尾的行内容
[root@localhost test]# cat test.txt |grep hat$
redhat
Redhat
[root@localhost test]#
支持正则表达式:
[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]"
inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0
[root@localhost test]#
cat test.txt | grep -E "ed | at" 显示包含ed或者at的内容行
[root@localhost test]# cat test.txt |grep -E "peida|com"
peida.cnblogs.com
[root@localhost test]# cat test.txt |grep -E "ed|at"
redhat
Redhat
[root@localhost test]#
显示当前目录下面以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
grep '[a-z]\{7\}' *.txt
[root@localhost test]# grep '[a-z]\{7\}' *.txt
test.txt:hnlinux
test.txt:peida.cnblogs.com
test.txt:linuxmint
[root@localhost test #