zoukankan      html  css  js  c++  java
  • NO17 第二关考试: 返回上次目录和ls -lrt倒序看文件--删除7天前的日志--查看日志更新--记录行号

    第二题:不用cd /ildboy命令如何回到上一次的目录:

    假如当前目录是:

    [root@localhost oldboy]# pwd
    /oldboy
    现在因为需要进入到了/tmp目录下进行操作,执行的命令如下:
    [root@localhost oldboy]# cd /tmp
    [root@localhost tmp]# pwd
    /tmp
    操作完毕后希望快速返回上一次进入的目录,即/oldboy目录,该如何作呢?不能用cd /oldboy命令。

    解答:
    [root@localhost tmp]# cd -  (回到上一次的目录)
    /oldboy

    此题原理:
    [root@localhost oldboy]# env|grep -i oldpwd  (系统有个变量自动跟随记录)
    OLDPWD=/tmp 
    [root@localhost oldboy]# cd - 
    /tmp
    [root@localhost tmp]# env|grep -i oldpwd
    OLDPWD=/oldboy

    关于cd命令:
    [root@localhost tmp]# cd .     #当前目录
    [root@localhost tmp]# cd ..    #上级目录
    [root@localhost /]# cd ~       #家目录
    [root@localhost ~]# cd -       #上一次的目录
    /


    第三题:一个目录中有很多文件(ls查看时好多屏),想最快速度查看到最近更新的文件,如何看?

    解答:
    [root@localhost ~]# ls -lrt /etc


    ls命令:
    -r  倒序,反转排序
    -t  按修改时间排序


    第四题:

    第五题:已知apache服务的访问日志按天记录在服务器本地目录/app/logs下,由于磁盘空间紧张,现在要求只能保留7天访问日志, 请问如何解决?
    请给出解决办法或配置或处理命令。
    (提示:可以从apache服务配置上着手,也可以从生成出来的日志上着手)

    解答:
    [root@localhost logs]# find /app/logs/ -type f -mtime +7 (先查看文件)
    方法一:
    [root@localhost logs]# find /app/logs/ -type f -mtime +7|xargs rm -f
    方法二:
    [root@localhost logs]# find /app/logs/ -type f -mtime +7 -exec rm -f {} ;
    方法三:
    [root@localhost logs]# rm -f 'find /app/logs/ -type f -mtime +7'


    ·find查找与时间有关的参数:
    -atime n    #n 为数字,意义为在n天之前的【一天之内】被access过的档案。
    -ctime n    #n 为数字,意义为在n天之前的【一天之内】被change过状态的档案。
    -mtime n    #n 为数字,意义为在n天之前的【一天之内】被modification过的档案。
    -newer file  #file 为一个存在的档案,意思是说,只要档案比file还要新,就会被列出来


    第六题:调试系统服务时,希望能实时查看系统日志/var/log/messages的更新,如何做?

    解答:
    方法一:
    [root@localhost logs]# tail -f /var/log/messages
    [root@localhost logs]# tailf /var/log/messages    (tailf和tail -f效果一样)

    [root@localhost logs]# tail -F /var/log/messages-F和-f比多个重试的功能,就是文件不存在了,会不断尝试


    第七题:打印配置文件nginx.conf内容的行号及内容,该如何做?

    解答:nginx和apache是不同的网页服务软件,是同类,就像男人和女人都是人类一样。

    创建环境:
    [root@localhost /]# echo stu{01..20} |xargs -n 1 >nginx.conf
    [root@localhost /]# cat nginx.conf
    stu01
    stu02
    stu03
    stu04
    stu05
    stu06
    stu07
    stu08
    stu09
    stu10
    stu11
    stu12
    stu13
    stu14
    stu15
    stu16
    stu17
    stu18
    stu19
    stu20

    方法一:记住
    [root@localhost /]# cat -n nginx.conf   (最常用,记住!)


    方法二:
    [root@localhost /]# nl nginx.conf(number lines,专业显示行号不太常用)空行不记录行号。
     

    方法三:
    [root@localhost /]# grep -n . nginx.conf   (对过滤内容显示行号,想对所有文件显示行号,就得过滤所有内容。“.”表示任意单个字符。这个不会对空行记录行号
    [root@localhost /]# grep -n ".*" nginx.conf  (加*会对空行记录行号)


    方法四:记住
    [root@localhost /]# vim nginx.conf   (记住!)
    然后输入set nu  不要行号就是set nonu

    方法五: 记住
    [root@localhost /]# awk '{print NR,$0}' nginx.conf  (NR表示行号,$0表示整行内容)


    方法六:记住
    [root@localhost /]# sed = nginx.conf| sed 'N;s/ / /'


    方法七:
    [root@localhost /]# less -N nginx.conf

  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/sinsenliu/p/8991687.html
Copyright © 2011-2022 走看看