zoukankan      html  css  js  c++  java
  • 【linux题目】第二关

    1、创建目录/data/oldboy,并且在该目录下创建文件oldboy.txt,然后在文件oldboy.txt里写入内容”inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0”(不包含引号)。

    解答:

    [root@oldboy /]# mkdir /data/oldboy -p
    [root@oldboy /]# cd /data/oldboy
    [root@oldboy oldboy]# touch oldboy.txt
    [root@oldboy oldboy]# echo "inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0" > oldboy.txt 
    [root@oldboy oldboy]# cat oldboy.txt
    inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0

    2、将题1中的oldboy.txt文件内容通过命令过滤只输出如下内容:

    10.0.0.8 10.0.0.255 255.255.255.0

    解答:

    方法1:用awk字段分隔符划分
    [root@oldboy oldboy]# awk -F "[: ]+" '{print $3,$5,$7 }' oldboy.txt 10.0.0.8 10.0.0.255 255.255.255.0 方法2:用sed命令替换全部英文和冒号
    [root
    @oldboy oldboy]# sed 's#[a-zA-Z:]##g' oldboy.txt 10.0.0.8 10.0.0.255 255.255.255.0
    方法3:用反向保留ip地址,去除其它 [root
    @oldboy oldboy]# sed -nr 's#^.*dr:([0-9.]+).*t:([0-9.]+).*:([0-9.]+)$#1 2 3#gp' oldboy.txt 10.0.0.8 10.0.0.255 255.255.255.0

    3、将题1中的oldboy目录移动到/tmp目录下,并将/etc/passwd文件复制到/tmp/oldboy下。

    解答:

    [root@oldboy oldboy]# mv /data/oldboy /tmp/
    [root@oldboy oldboy]# cp /etc/passwd /tmp/oldboy/ 

    4、在题3的基础上使用awk取passwd文件的第10行到20行的第三列重定向到/tmp/oldboy/test.txt文件里。

    解答:
    [root@oldboy oldboy]# awk 'NR==10,NR==20 {print}' passwd|cut -d ":" -f 3 >> /tmp/oldboy/test.txt
    [root@oldboy oldboy]# cat test.txt
    10
    11
    12
    13
    14
    99
    81
    69
    173
    68
    38

    5、在题3的基础上要求用命令rm删除文件时提示如下禁止使用rm的提示,并使该效果永久生效。

    解答:

    1. 临时生效:alias别名修改rm。
      • alias rm='echo Do not use rm command.'
    2. 永久生效:配置文件~/.bashrc, ~/.bashrc_prfile等文件永久修改。
      • [root@oldboy ~]# cat .bashrc
        # .bashrc
        
        # User specific aliases and functions
        
        alias rm='rm -i'
        alias cp='cp -i'
        alias mv='mv -i'
        alias grep='grep --color=auto'
        alias ll='ls -l'
        alias rm='Do not use rm command!'
        alias net='cat /etc/sysconfig/network-scripts/ifcfg-eth3'
        
        # Source global definitions
        if [ -f /etc/bashrc ]; then
                . /etc/bashrc
        fi

        定义别名永久生效:

        • /etc/profile 全局生效
        • /etc/bashrc 全局生效

        • ~/.bashrc 当前用户生效

        • ~/.bashrc_profile 当前用户生效

    6、在题3的基础上,删除/tmp/oldboy/下移除passwd以外的其他文件。

    解答:find /tmp/oldboy -type f ! -name passwd -exec rm {} ;

    7、在题3的基础上,请打印/etc/passwd文件中的第2-5行(不低于三种方法)

     解答:

    方法1: head -5 /etc/passwd|tail -4

    方法2:  sed -n '2,5p' /etc/passwd

    方法3: awk 'NR==2,NR==5 {print }' /etc/passwd

    8、在题3的基础上,使用命令调换passwd文件root位置和/bin/bash位置?即将所有的第一列和最后一列位置调换?

    解答:

    方法1: sed替换,后向引用
    [root@oldboy oldboy]# sed -n '1p' passwd
    root:x:0:0:root:/root:/bin/bash
    [root@oldboy oldboy]# sed -nr '1s#(.*):x(.*):(.*)#3:x2:1#gp' passwd  
    /bin/bash:x:0:0:root:/root:root
    
    方法2: awk分隔符
    awk -F ":" '{print $7":"$2":"$3":"$4":"$5":"$6":"$1}' passwd

    9、把/data目录及其子目录下所有以扩展名.txt结尾的文件中包含oldgirl的字符串全部替换为oldboy。

    解答:

    find /data -type f -name "*.txt"|xargs sed -i 's#oldgirl#oldboy#g'   

    10、查找/oldboy下所有七天以前以log结尾的大于1M的文件移动/tmp下。

    解答:

    • find /oldboy -type f -name "*.log" -size +1M -mtime +7 -exec mv {} /tmp/ ;
    • find /oldboy -type f -name ".log" -size +1M -mtime +7 |xargs -I {} mv {} /tmp/
    • mv $(find . -name "*.log" -size +1M -atime +7) /tmp/
    • mv `find . -name "*.log" -size +1M -atime +7` /tmp/

    11、什么是Linux的运行级别,请描述Linux的运行级别不同数字的含义?(附加题)

    解答:

    • 0 关机
    • 1 单用户命令行模式
    • 2 无网络的多用户
    • 3 正常的文本模式下的多用户
    • 4 unused
    • 5 桌面系统模式
    • 6 重启
    [root@zoe oldboy]# cat /etc/inittab
    # inittab is only used by upstart for the default runlevel.
    #
    # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    #
    # System initialization is started by /etc/init/rcS.conf
    #
    # Individual runlevels are started by /etc/init/rc.conf
    #
    # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
    #
    # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
    # with configuration in /etc/sysconfig/init.
    #
    # For information on how to write upstart event handlers, or how
    # upstart works, see init(5), init(8), and initctl(8).
    #
    # Default runlevel. The runlevels used are:
    #   0 - halt (Do NOT set initdefault to this)
    #   1 - Single user mode
    #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
    #   3 - Full multiuser mode
    #   4 - unused
    #   5 - X11
    #   6 - reboot (Do NOT set initdefault to this)
    # 
    id:3:initdefault:

    12、请描述buffer和cache的区别?(附加题)

    解答:

    缓冲和缓存

    • 写入数据到内存里,这个数据的内存空间称为缓冲区(buffer)
    • 从内存读取数据,这个数据的内存空间称为缓存区(cache)

    13、请说出你知道的下列字符在Linux里可以代表的意义。(附加题)

    ~ - . .. | > >> < << !

    解答:

    • ~ 家目录
    • - 上一次所在的目录
    • . 当前目录; 单个任意字符
    • .. 上一级目录
    • | 管道
    • > 标准正确输出
    • >> 追加标准正确输出
    • < 标准输入
    • << 追加标准输入
      • !! 上一条命令
      • !数字 history命令排行的数字的命令
      • ! -name "aa.txt" 取反
      • !c 表示调出最近一次以c开头的命令
  • 相关阅读:
    ajax获取值的两种方法
    java反射
    idea 方便的设置代码段
    jstl核心标签库
    git遇到的问题 .Git: There is no tracking information for the current branch.
    java使用顺序存储实现队列
    RabbitMQ基本操作
    springboot 如何操作redis
    docker遇到的问题以及docker 操作镜像的基本操作
    教你在 Yii2 中添加全局函数
  • 原文地址:https://www.cnblogs.com/zoe233/p/11950963.html
Copyright © 2011-2022 走看看