ubuntu常用操作命令以及它的通道模式简解

1.tail

  tail -f filename :可以动态查看文件的写入,按ctrl+c结束查看.

  要显示 notes 文件的最后十行,输入:

  tail -n -10 notes  

  tail notes要指定从 notes 文件末尾开始读取的行数,输入: 
  tail  -n 20 notes 
  要从第 200 字节开始,每次显示一页 notes 文件,输入: 
  tail  -c +200 notes | pg 

2.less与more逐行查看文件

  more filename : 使用回车查看下一行,有百分比显示

  less filename:使用上下箭头查看上一行,下一行.

  两个命令退出方式都是:按一次Q键即可.

3.head 查看文件几行数据

  例:查看文件的一行数据

head -n 1 path/filename

4.sort 与 uniq 

   sort -r filename > newfilename :将文件排序然后放入新的文件中 (-r 逆序 默认为升序,原理为从第一行开始以逐个字符的ASCII码比较进行排序)

  sort -u filename > newfilename: 将文件去重然后放入新的文件中(与uniq不同,不是连续的数据只要重复她也会去除掉)

  uniq -c filename >newfilename :将文件去重后放入新的文件中(-c 在每行前显示此行出现重复的次数   注意:只是去掉连续出现的记录.不是连续出现的 就算重复也不删除)

5.sed

  匹配一段时间:如2017-12-25 10点到2017-12-26 9点的数据

sed -n '/2017.12.25 10:[0-9][0-9]/,/2017.12.26 10:[0-9][0-9]/p' err.log | sort -r | uniq -c > newfile.log

  注:sed匹配开始和结尾必须都要有一行是匹配的才会匹配,否则不会匹配到任何结果.

6.date

  获取多天日期例子:

  

[root@Gman root]# date -d next-day +%Y%m%d #明天日期  
20091024  
[root@Gman root]# date -d last-day +%Y%m%d #昨天日期  
20091022  
[root@Gman root]# date -d yesterday +%Y%m%d #昨天日期  
20091022  
[root@Gman root]# date -d tomorrow +%Y%m%d # 明天日期  
20091024  
[root@Gman root]# date -d last-month +%Y%m #上个月日期  
200909  
[root@Gman root]# date -d next-month +%Y%m #下个月日期  
200911  
[root@Gman root]# date -d next-year +%Y #明年日期  
2010  

  

7.linux的通道模式

  公式:

command1 | command2 | command3 |....

  原理:以command1的输出结果作为为command2的输入进行通道式的传递.

  注:此处的command2.command3 必须能接受标准输入,否则管道无法输送成功,只是执行最后一个命令.

  如下图所示,只是执行了ls命令,cat并未执行: 

       

   参考链接:https://jingyan.baidu.com/album/5d368d1ef8afd93f60c05708.html?picindex=4

        http://www.itwis.com/html/os/linux/20100202/7360.html

原文地址:https://www.cnblogs.com/ssjifm/p/8110276.html