• L010 linux命令及基础手把手实战总结


    一转眼都快两周没更新了,最近实在太忙了,这两周的时间断断续续的把L010学完了,短短的15节课,确是把前10节的课程全部的运用一遍,从笔记到整理,再到重新理解,最后发布到微博,也确实提升了一些综合性能,从一个问题转而可以想到思路了,这个是我非常开心的,好了,那么就开始下面的总结。

    L010本堂课并非讲述新内容,而是从10多道题入手来熟悉之前的命令,那么我就以题的形式来写本次的总结。

    第一题:
    100M的空间创建1K的文件,可以创建多少个?如果创建1M的文件可以创建多少个?

    答:假设硬盘为4k对齐,那么一个1k的文件就占用1个block和一个inode的为4.25k,就是100*1024/4.25=24094个。
    那么一个1M的文件就会占用3个block和一个inode为12.25k,就是100*1024/12.25=8359个
    换句话说,系统中的block的数量比inode多。那么就可以判断出,1k的文件数就等于inode的数量

    相关的命令:

    df -i 查询inode数量 
    df -h硬盘余量

    答:

    查询某块硬盘的inode和block的尺寸和数量(我们以第一块硬盘的第二个分区为例)

    dumpe2fs /dev/sda2 | grep "Block count"  //block的数量
    dumpe2fs /dev/sda2 | grep "Block size"   //block的尺寸
    
    dumpe2fs /dev/sda2 | grep "Inode count"  //inode的数量
    dumpe2fs /dev/sda2 | grep "Inode size"   //indoe的尺寸

    第二题:
    如何过滤出已知当前目录下的ceshi中的所有一级目录(提示:不包含ceshi目录下面目录的子目录及隐藏目录,即只能是一级目录)?

    答:

    首先需要创建环境

    [root@moban kh]# for n in `seq 10`;do mkdir -p "$n";done  
    [root@moban kh]# ls
    1  10  2  3  4  5  6  7  8  9
    [root@moban kh]# mkdir -p 11/12/13
    [root@moban kh]# mkdir -p 14/15/16
    [root@moban kh]# pwd
    /root/kh
    [root@moban kh]# tree /root/kh/
    /root/kh/
    ├── 1
    ├── 10
    ├── 11
    │   └── 12
    │       └── 13
    ├── 14
    │   └── 15
    │       └── 16
    ├── 2
    ├── 3
    ├── 4
    ├── 5
    ├── 6
    ├── 7
    ├── 8
    └── 9
    [root@moban kh]# for n in `seq 5`;do touch "$n.txt";done

    开始做题:

    用find:

    [root@moban kh]# find ./ -type d | awk -F "/" '{print $2}'
    
    4
    6
    14
    14
    14
    5
    10
    3
    1
    8
    2
    9
    11
    11
    11
    7

    [root@moban kh]# find ./ -maxdepth 1 -type d    //find -maxdepth 为查找的目录深度参数
    ./
    ./4
    ./6
    ./14
    ./5
    ./10
    ./3
    ./1
    ./8
    ./2
    ./9
    ./11
    ./7

    用ll:

    [root@moban kh]# ls -l | grep ^d | awk '{print $9}'
    1
    10
    11
    14
    2
    3
    4
    5
    6
    7
    8
    9

    用tree:

    用tree:
    [root@moban kh]# tree -Ld 1    //L是深度,d为目录的属性
    .
    ├── 1
    ├── 10
    ├── 11
    ├── 14
    ├── 2
    ├── 3
    ├── 4
    ├── 5
    ├── 6
    ├── 7
    ├── 8
    └── 9

    用sed:

    [root@moban kh]# ll | sed -n /^d/p | awk '{print $9}'
    1
    10
    11
    14
    2
    3
    4
    5
    6
    7
    8
    9

    第三题:
    去除ssd配置文件中的注释部分

    答:

    [root@moban kh]# grep -v ^# /etc/ssh/sshd_config        //方法1
    
    [root@moban kh]# cat /etc/ssh/sshd_config | grep -v ^#    //方法2

    第四题:
    去除ssd配置文件中的注释部分以及空行部分

    [root@moban ~]# grep -Ev "^#|^$" /etc/ssh/sshd_config  //方法1
    [root@moban ~]# egrep -v "^#|^$" /etc/ssh/sshd_config  //方法2

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

    答:

    [root@moban kh]# ls -lrt    //r为反、倒 t为time,时间

    第六题:
    让egrep搜索的内容填加颜色

    答:

    [root@moban kh]# echo "alias egrep='egrep --color=auto'" >>/etc/profile
    [root@moban kh]# source /etc/profile

    第七题:
    在配置apache时执行了./configure --prefix=/application/apache2.2.17 来编译apache,在make install 完成后,希望
    用户的访问路径更简单,需要给/application/apache2.2.17 目录做一个软连接/application/apache,使得内部开发或者
    管理人员通过/application/apache 就可以访问到apache的安装目录/application/apache2.2.17下的内容,请你给出实现
    的命令。(提示:apache为一个httpd web 服务)

    ln 连接(link)常与-s搭配 做软连接使用
    ln -s [源文件或目录] [目标文件或目录]

    参数:
    -b 删除,覆盖以前建立的链接
    -d 允许超级用户制作目录的硬链接
    -f 强制执行
    -i 交互模式,文件存在则提示用户是否覆盖
    -n 把符号链接视为一般目录
    -s 软链接(符号链接)
    -v 显示详细的处理过程

    答:

    第一步创建做题环境

    [root@moban ~]# mkdir -p /root/ti/application/apache2.2.17/

    这样,目录就已搭建好

    下面:

    [root@moban ti]# ln -s /root/ti/application/apache2.2.17/ /root/ti/application/apache    //前面是实际路径,后面的路径为软连接路径

    查看一下软件链接:

    [root@moban ti]# ll /root/ti/application/
    total 4
    lrwxrwxrwx 1 root root   34 Mar  7 05:46 apache -> /root/ti/application/apache2.2.17/
    drwxr-xr-x 2 root root 4096 Mar  7 05:44 apache2.2.17

    因为是第一次做软连接,所以在原连接目录下创建一文件,去软连接里看看会不会看到,哈哈

    [root@moban ~]# cd /root/ti/application/apache
    [root@moban apache]# ls
    [root@moban apache]# pwd
    /root/ti/application/apache
    [root@moban apache]# touch /root/ti/application/apache2.2.17/123.txt
    [root@moban apache]# ls
    123.txt

    成功,然后还有一点,我试了一下,软连接不一定跟源路径走,比如源连接:/root/ti/application/apache2.2.17/,那么我的连接路径可以也设置为/root/apache

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

    Date 时间
    参数“
    -s 设置时间;根据字符串来设置日期与时间。字符串前后必须加上双引号。
    >date -s //设置当前时间,只有root权限才能设置,其他只能查看。
    >date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
    >date -s 01:01:01 //设置具体时间,不会对日期做更改
    >date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
    >date -s “01:01:01 20080523″ //这样可以设置全部时间
    >date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
    >date -s “20080523 01:01:01″ //这样可以设置全部时间

    答:

    这道题其实难做的是环境(对于这个阶段的我来说)

    先做环境把

    [root@moban log]# for n in `seq 6`;do date -s "03/$n/16";touch access_www_`(date +%F)`.log;done        //一直分不清符号的使用,如``和‘’和"",不过通过这个程序知道了用这个``的时候要是中间是需要执行的程序,
    关于%F,为日期的意思:同%Y-%m-%d

    然后做题

    [root@moban log]# find ./  -type f -mtime +3 -exec rm -f {} ;  //注意“”前要有空格

    find ./ -type f -name "*.log" -mtime +3 | xargs rm -f

    也可以放到循环列表中

    [root@moban oldboy]# echo ‘* 3 * * * find ./ -type f -name "*.log" -mtime +3 -exec rm -f {} ; >> /dev/null 2>&1’ >> /var/spool/cron/root    // -exec rm -f {} ; 可以改成-delete

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

    tail  //用于显示指定文件末尾内容,常用查看日志文件。 常用-f参数可以方便的查阅正在改变的日志文件

    参数:
    -f 循环读取
    -q 不显示处理信息
    -v 显示详细的处理信息
    -c<数目> 显示的字节数
    -n<行数> 显示行数
    --pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.
    -q, --quiet, --silent 从不输出给出文件名的首部
    -s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

    答:

    tail -f /var/log/messages

    第十题:
    装完系统后,希望网络文件共享服务sshd,仅在3级别上开机自启动,该如何去做。

    chkconfig --list
    chkconfig --level
    chkconfig [应用名] on/off //各模式下的全部开启或者关闭

    答:

    [root@moban log]# chkconfig --list sshd            //查看在各level下的sshd开启状态
    sshd            0:off   1:off   2:on    3:on    4:off   5:off   6:off
    [root@moban log]# chkconfig sshd off            //关闭所有level下的sshd
    [root@moban log]# chkconfig --list sshd
    sshd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
    [root@moban log]# chkconfig --level 3 sshd on        //sshd开启level 3模式
    [root@moban log]# chkconfig --list sshd
    sshd            0:off   1:off   2:off   3:on    4:off   5:off   6:off

    第十一题:
    linux系统运行级别一般为0-6,请分别写出每个级别的含义。

    答:

    0 - halt //关机模式
    1 - Single user mode //单一用户模式
    2 - Mutiuser,without NFS //无NFS用户的多用户模式
    3 - Full multiuser mode //命令行模式
    4 - unused //空
    5 - X11 //图形化
    6 - reboot //重启机器 init 6

    第十二题:
    /etc/目录为linux系统的默认的配置文件及服务启动命令的目录
    a.请用tar打包/etc整个目录(打包及压缩)
    b.请用tar打包/etc整个目录(打包及压缩,但需要排除/etc/services文件)
    c.请把a点命令的压缩包,解压到/tmp指定目录下(最好只用tar命令实现)

    tar  压缩命令

    参数:

    tar 参数 [压缩包的路径+文件名] [要压缩文件的名称和地点]
    参数:
    -c 创建
    -z gz格式的压缩文件
    -j bz格式的压缩文件
    -v 显示过程中的文件名
    -f 使用文档名,在f过后要立即加文档或路径
    -x 解压
    -t 查看文档内容
    -X 从文档中读取排除压缩包外的目录名
    -C 指定解压到某地址的参数,后面接路径
    --eclude FILE 排除某个文件或者目录

    答:

    a.[root@moban /]# tar -czvf etc.tar.gz etc/
    b.[root@moban /]# tar --exclude etc/services -czvf etc1.tar.gz etc/
    c.[root@moban etc]# tar -xzvf etc.tar.gz -C /tmp

    -X和-C参数运用:

    [root@moban tmp]# echo var > tar.txt
    [root@moban tmp]# echo tmp >> tar.txt 
    [root@moban tmp]# cat tar.txt
    [root@moban tmp]# tar -X tar.txt  -czvf etc2.tar.gz etc/
    [root@moban tmp]# tar -xzvf etc2.tar.gz -C /tmp/
    [root@moban tmp]# find ./ -type d -name tmp -o -name var

    第十三题:
    已知下面的命令及结果:
    [root@moban ~]# echo "I am oldboy,myqq is 49000448" >> oldboy.txt
    [root@moban ~]# cat oldboy.txt
    I am oldboy,myqq is 49000448
    a.现在需要从文件中过滤出“oldboy”和“49000448”字符串,请给出命令
    b.如果需要从文件中过滤出“oldboy,49000448”字符串,请再给出命令

    cut  //剪切数据

    参数

    -b 字节
    -c 字符
    -d 设定分隔符
    -f 分区域显示,和-d一起使用,-d设定分隔符后加-f显示区域

    参数使用(参数通用,不仅仅只-b):
    -b -3 //-3为前3个字符,包括第三个
    -b 3- //为第三个以后的字符,包括第三个
    -b 3 //第三个字符
    -b 3-5//第三到第五个字符,包括3和5字符
    -b 3-5,6-7 //多个用,连接,从小到大排列,就算6-7,3-5也是从3-5开始

    答:

    a.[root@moban tmp]# cut -b 6-11,20-28 cut.txt 
    oldboy 49000448
    b.[root@moban tmp]# cut -b 6-12,21-28 cut.txt 
    oldboy,49000448

    另:
    已知下面的命令及结果:
    [root@moban ~]# echo "I am oldboy myqq is 49000448" >> oldboy.txt
    [root@moban ~]# cat oldboy.txt
    I am oldboy,myqq is 49000448
    a.现在需要从文件中过滤出“oldboy”和“49000448”字符串,请给出命令
    b.如果需要从文件中过滤出“oldboy,49000448”字符串,请再给出命令

    tr "," " "  //把,替换成空格

    答:

    cut方法

    a.[root@moban tmp]# cut -d " " -f 3,6 cut.txt 
    oldboy 49000448
    b.[root@moban tmp]# cut -d " " -f 3,6 cut.txt|tr " " ","    //把输出的结果中空格字符换成逗号
    oldboy,49000448

    awk方法

    a.[root@moban tmp]# awk -F " " '{printf $3 $6}' cut.txt
    oldboy49000448
    b.[root@moban tmp]# awk -F " " '{printf $3","$6}' cut.txt      
    oldboy,49000448

    sed方法(算是吧,为了巩固sed s###g这个替换法)

    a.[root@moban tmp]# sed s#","#" "#g cut.txt | awk -F " " '{printf $3 $6}'   //用sed sg把逗号换成空格
    oldboy49000448

    第十四题:
    如何查看/etc/services文件的有多少行?

    cat //查看 命令

    参数:
    -n 或 --number 由 1 开始对所有输出的行数编号
    -b 或 --number-nonblank 和 -n 相似,只不过对于空白行不编号
    -s 或 --squeeze-blank 当遇到有连续两行以上的空白行,就代换为一行的空白行
    -v 或 --show-nonprinting

    wc 统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

    参数:
    -c 统计字节数。
    -l 统计行数。----------常用
    -m 统计字符数。这个标志不能与 -c 标志一起使用。
    -w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。
    -L 打印最长行的长度。

    答:

    1)vi /etc/services
    :set nu
    2)less -N /etc/ssh/sshd_config
    3)cat -n /etc/services
    4)nl /etc/services
    5)[root@moban tmp]# grep -n " " ./etc/services

    第十五题:
    过滤出/etc/services 文件包含3306或1521两数字的行的内容

    grep 过滤

    参数
    -n 显示行号

    答:

    [root@moban tmp]# grep -En "3306|1521" /etc/services 
    416:mysql           3306/tcp                        # MySQL
    417:mysql           3306/udp                        # MySQL
    2653:ncube-lm        1521/tcp                # nCube License Manager
    2654:ncube-lm        1521/udp                # nCube License Manager

    [root@moban tmp]# egrep -n "3306|1521" /etc/services 
    416:mysql           3306/tcp                        # MySQL
    417:mysql           3306/udp                        # MySQL
    2653:ncube-lm        1521/tcp                # nCube License Manager
    2654:ncube-lm        1521/udp                # nCube License Manager

    其他一些设计到的内容

    正则表达式:
    ^的意思是以...开头,例如^oldboy表示以oldboy开头的
    与`^`意思相反的是[^*],非以*为开头的
    $的意思是以....结尾,例如oldboy$表示以oldboy字符串为结尾。
    ^$意思是空行
    让一个字符脱掉马甲,还原他本来的意义。([root@moban ceshi]# sed -n '///p' 1.txt //过滤斜线)

    find参数 -maxdepth 1 //数字1表示1层 查找的目录深度参数


    小结:
    find
    !的作用取反 -a为and表示并且(查找多个默认为and) -o为or两遍有一个成立即可。

    [root@moban ceshi]# find ./ -type f -o -name 1.txt
    [root@moban ceshi]# find ./ -type f -a -name 1.txt
  • 相关阅读:
    Div+CSS 布局
    Windows Mobile 参考:
    Linux export的作用
    CSS(2)基本语法
    HTML(6)超链接
    HTML(5)常用的格式标签
    HTML(8)表格
    CSS(1) CSS简介
    HTML(7)图像、背景和颜色
    HTML(10)框架
  • 原文地址:https://www.cnblogs.com/lcrbg/p/5382730.html
Copyright © 2020-2023  润新知