N25第六周 vim,crontab命令的使用

请详细总结vim编辑器的使用并完成以下练习题

vim是一款功能强大的文本编辑器,是程序员的必备神器。



vim工作模式分为三种:编辑模式,输入模式,末行模式,三种工作模式可以进行来自由切换
    编辑模式---》输入模式: 直接键i 
    输入模式---》编辑模式: Esc
    编辑模式---》末行模式: Ctrl+:
    末行模式---》输入模式: 无法直接切换,需要经由编辑模式切换。

    编辑模式下命令:
    a:光标跳至行尾进入输入模式;
    h:向左移动;
    l:向右移动;
    j:向下移动;
    J:把下行移动到光标所在行行尾;
    K:向上移动;
    g: gg---跳转是第一行;
    G: 跳转至末行:
       #G---跳转至指定行;
    w: 跳至下个单词词首;
    b: 跳至上个单词词首;
    e: 当前或下个但是词尾;
    o: 在光标所在行下行插入空白行并切换至输入模式;
    O:在光标所在行上行插入空白行并切换至输入模式;
    P: 粘贴命令,在光标所在行下一行粘贴已复制的内容;
    P: 粘贴命令,在光标所在行下一行粘贴已复制的内容;
    x: 直接删除光标所在的字符;
    X: 删除光标所在字符的上个字符;
    r: replace, 以新字符取代光标所在的字符,按r后输入新字符;
    u: 撤销此前的操作;
    U: 撤销“撤销”操作;
    d: 配合其他键使用
       dd---直接删除所在行;
       dw---删除光标所在的单词;
       d$---删除光标至行尾的字符串;
       d^---删除光标至行首的字符串;
    y: 与d键使用方法类似
       yy---复制光标所在行;
       yw---复制光标所在的单词;
       y$---复制光标至行尾的字符串;
       y^---复制光标至行首的字符串;

    v: visual 类似是鼠标移动选定文本功能,配合h,j,k,l键使用可以上下左右选定文本;

    c: 与d键用法类似,不同的是其执行命令完后切换至输入模式;
    ^: 光标跳至行首;
    $: 光标跳至行尾;
    Z:ZZ---保存文本并退出vim;   

对文本进行翻屏: 
    向前翻一屏:ctrl+f;
    向后翻一屏:ctrl+b;
    Enter      : 向后按行翻屏
    向前翻半屏:ctrl+u; 
    向后翻半屏:ctrl+d;

输入模式下命令:与普通的输入模式无异。

末行模式:
     q!:不保存退出 vim;
     wq: 保存文件并退出;
     w:  w /path/to/somewhere/filename, 当前文件保存至指定路径下;

 对文本指定的行进行定位:
      #1,#2: #1---开始的行号
               #2---结束的行号
               可以对此区间的行进行删除复制等操作:例如,3,5y 表示复制3行至5行的内容
       #,$:指定行至文本行尾;
      %:文本全文;

 对文本内容进行替换处理:
     #1,#2 s/原内容/新内容/g
            原内容:支持正则表达式;
            新内容:不支持正则表达式,但是可以使用&代替原内容; 



 在末行模式下可以对vim特性进行设置:
     set number: 显示行号;
     set number: 不显示行号;
     set hlsearch: 高亮显示匹配结果;
     set nohlsearch: 不高亮显示皮别结果;
     set ai        :  自动缩进;
     set no ai     :  不自动缩进;
     syntax on     : 语法检查开启;
     syntax off    : 语法检查关闭

    注意:此配置仅对当前编辑有效,如需要全局有效则在 /etc/vimrc 中进行配置。

vim支持同时打开多个文件:
        vim /path/to/somewhere/filename1 /path/to/somewhere/filename2. 
        当filename不存在时 系统将自动建立文件.。
    支持多窗口:
        水平:vim -o /path/to/somewhere/filename1 /path/to/somewhere/filename2       
        垂直:vim -O /path/to/somewhere/filename1 /path/to/somewhere/filename2

    各窗口间切换: ctrl+w+箭头;


  通知单文件也支持多窗口:

        ctrl+W,S 水平窗口
        ctrl+w,v 垂直窗口

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

 复制:cp /etc/rc.d/rc.sysinit /tmp

 打开:vim /tmp/rc.sysinit.

 切换至末行模式: %s@^[[:space:]]\+.*@#&@

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

 复制: cp /boot/grub/grub.conf  /tmp

 打开: vim /tmp/grub.conf

 切换至末行模式:  %s#^[[:space:]]##

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符;

  打开: vim /tmp/rc.sysinit

  末行模式下: %s@^#\+[[:space:]]\+@@

4、为/tmp/grub.conf文件中前三行的行首加#号;

  打开: vim  /tmp/grub.conf

  末行模式下: 1,3s/^/#&/

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

    [c6-media]
    name=CentOS-$releasever - Media
    baseurl=file:///media/CentOS/
            file:///media/cdrom/
            file:///media/cdrecorder/
    gpgcheck=1
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    ~                                                                                                                                              
    :%s@\(enabled\|gpgcheck\)=0@\1=1@

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202

   [root@localhost spool]# crontab -l

   0 */4 * * * /bin/cp -r /etc /backup/etc-$(date +\%Y\%m\%d\%H\%M)


   注意:%在crontab有特殊含义必须使用转义字符。

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402

 [root@localhost spool]# crontab -u admin -l

 0 12 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages_$(date +\%Y\%m\%d)

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

  [root@localhost spool]# crontab -l

  0 */2 * * * /bin/cat /proc/meminfo | /bin/grep "^S.*" >/stats/memory.txt

  注意:memory.txt文件必须事先创立,否则任务执行会失败。

9、工作日的工作时间内,每两小时执行一次echo "howdy"

[root@localhost ~]# crontab -l

0 9-18/2 * * 1-5 /bin/echo "howdy"

脚本编程练习

10、创建目录/tmp/testdir-当前日期时间;

[root@localhost tmp]# cat /tmp/scripts1.sh #!/bin/bash mkdir /tmp/testdir-$(date +%F%H%M)

[root@localhost tmp]# ./scripts1.sh
[root@localhost tmp]# ls -l | grep testdir
drwxr-xr-x. 2 root  root   4096 Dec 27 07:01 testdir-2016-12-270701
drwxr-xr-x. 2 root  root   4096 Dec 27 07:03 testdir-2016-12-270703
drwxr-xr-x. 2 root  root   4096 Dec 27 07:09 testdir-2016-12-270709
[root@localhost tmp]#

11、在此目录创建100个空文件:file1-file100;

   #! /bin/bash

   for i in {1..100};do
      touch ./file$i
      done
      echo "100 files completely created"


    [root@localhost tmp]# bash scripts2
    100 files completely created
    [root@localhost tmp]# ls -l ./
    total 260
    -rwsr-xr-x. 1 root  root  48568 Dec 26 00:40 cat
    -rw-------. 1 root  root     87 Dec 27 06:36 crontab.hpk75h
    -rw-------. 1 root  root     56 Dec 27 06:35 crontab.YLrMNM
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file1
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file10
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file100
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file11
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file12
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file13
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file14
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file15
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file16
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file17
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file18
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file19
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file2
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file20
    -rw-r--r--. 1 root  root      0 Dec 27 08:12 file21

  限于篇幅只截取部分显示内容。

12、显示/etc/passwd文件中位于第偶数行的用户的用户名;

   [root@localhost ~]# cat /tmp/scripts3
    #!/bin/bash
    sed -n '2~2p' /etc/passwd |cut -d: -f1
    [root@localhost ~]# bash  /tmp/scripts3
    bin
    bash
    nologin
    user1
    user10
    user12
    user14
    user16
    user18
    user20
    user22
    user24
    user26
    user28
    [root@localhost ~]#

13、创建10用户user10-user19;密码同用户名;

    [root@localhost tmp]# cat /tmp/scripts4
    #!/bin/bash

    for i in {20..29};do
        if ( id user$i &> /dev/null); then
           echo "user exits"
        else useradd user$i && echo "user$i" | passwd user$i --stdin 
        fi
    done
       echo "ten users added completely"


   [root@localhost tmp]# bash scripts4
    Changing password for user user20.
    passwd: all authentication tokens updated successfully.
    Changing password for user user21.
    passwd: all authentication tokens updated successfully.
    Changing password for user user22.
    passwd: all authentication tokens updated successfully.
    Changing password for user user23.
    passwd: all authentication tokens updated successfully.
    Changing password for user user24.
    passwd: all authentication tokens updated successfully.
    Changing password for user user25.
    passwd: all authentication tokens updated successfully.
    Changing password for user user26.
    passwd: all authentication tokens updated successfully.
    Changing password for user user27.
    passwd: all authentication tokens updated successfully.
    Changing password for user user28.
    passwd: all authentication tokens updated successfully.
    Changing password for user user29.
    passwd: all authentication tokens updated successfully.
    ten users added completely
    [root@localhost tmp]# cat /etc/passwd | tail
    user20:x:517:517::/home/user20:/bin/bash
    user21:x:518:518::/home/user21:/bin/bash
    user22:x:519:519::/home/user22:/bin/bash
    user23:x:520:520::/home/user23:/bin/bash
    user24:x:521:521::/home/user24:/bin/bash
    user25:x:522:522::/home/user25:/bin/bash
    user26:x:523:523::/home/user26:/bin/bash
    user27:x:524:524::/home/user27:/bin/bash
    user28:x:525:525::/home/user28:/bin/bash
    user29:x:526:526::/home/user29:/bin/bash

14、在/tmp/创建10个空文件file10-file19;

    #!/bin/bash
        for i in {10..19};do
             touch /tmp/file$i
        done
        echo " ten files completely created "
    ~                                                                                                                                              
    ~                              

   [root@localhost tmp]# bash scripts5
   ten files completely created

15、把file10的属主和属组改为user10,依次类推。

    #!/bin/bash
       for ((i=10;i<=19;i++));do
              chown user$i:user$i file$i
            ls -l /tmp/file$i
            done
       echo " changing the owner of files completed " 
    ~                                                                                                                                              

    [root@localhost tmp]# bash  scripts6
    -rw-r--r--. 1 user10 user10 0 Dec 27 11:53 /tmp/file10
    -rw-r--r--. 1 user11 user11 0 Dec 27 11:53 /tmp/file11
    -rw-r--r--. 1 user12 user12 0 Dec 27 11:53 /tmp/file12
    -rw-r--r--. 1 user13 user13 0 Dec 27 11:53 /tmp/file13
    -rw-r--r--. 1 user14 user14 0 Dec 27 11:53 /tmp/file14
    -rw-r--r--. 1 user15 user15 0 Dec 27 11:53 /tmp/file15
    -rw-r--r--. 1 user16 user16 0 Dec 27 11:53 /tmp/file16
    -rw-r--r--. 1 user17 user17 0 Dec 27 11:53 /tmp/file17
    -rw-r--r--. 1 user18 user18 0 Dec 27 11:53 /tmp/file18
    -rw-r--r--. 1 user19 user19 0 Dec 27 11:53 /tmp/file19
     changing the owner of files completed 
    [root@localhost tmp]# vim scripts6

原创文章,作者:diglinux,如若转载,请注明出处:http://www.178linux.com/65139

(0)
diglinuxdiglinux
上一篇 2016-12-29
下一篇 2016-12-29

相关推荐

  • ansible之Playbook中tags使用

    示例:httpd.yml– hosts: websrvsremote_user: roottasks:– name: Install httpdyum: name=httpd state=present – name: Install configure filecopy: src=files/httpd.conf des…

    Linux干货 2018-01-14
  • 防护墙服务

    iptables的基本认识        Netfilter组件: 内核空间,集成在linux内核中 扩展各种网络服务的结构化底层框架 内核中选取五个位置放了五个Hook(勾子)function(INPUT、OUTPUT、FORWARD、PREROUTING、POST ROUTING),而这五个hoot functio…

    2017-08-21
  • vsftp简单应用

    vsftp配置详解 前言 FTP(File Transfer Protocol)是文件传输协议的简称。它能让用户 连接上一个远程计算机(运行着 FTP 服务器程序)查看远程 计算机上有哪些文件,然后把文件从远程计算机上下载到 本地计算机,或把本地计算机文件上传到远程计算机。但是ftp是一种古老的文件传输协议,是明文传输,特别的不安全所以就有了vsftp。 一…

    Linux干货 2016-12-18
  • 如何正确安装一个源码包

        下周就要考试了,心情挺忐忑不安的,前几天做了25期的考试题,感觉每个题都是老师上课讲过的,但是自己做却想不起来了。这应该就像学习数学一样,需要大量练习,做的多了自然就会了。     这周我们学习了如何使用yum,还学习了磁盘管理。针对如何安装源码包,以httpd包为例我们…

    2017-08-19
  • lamp平台 php解析器基于模块和php-fpm

    首先,我先介绍一下实验环境: http服务器:192.168.236.128(php解析器基于modules) mysql服务器:192.168.236.129 编译和配置http服务器,http版本是2.4以上的。 由于http依赖于apr apr-util这两个包,但是我们系统上的rpm包版本比较低,我们也需要下载这两个源码包来编译,解决依赖关系。 还要…

    Linux干货 2015-08-31
  • 马哥教育网络班25期+第一周课程练习

    一、计算机的组成及功能     现代计算机的结构:冯诺依曼结构体系(储存程序,顺序执行),由20世纪30年代中期,美国科学家冯·诺依曼提出     冯.诺依曼结构处理器具有以下几个特点:     1:必须有一个存储器;2:必须有一个控制器;3:必须有一个运算器…

    Linux干货 2016-11-28