Linux基础5(vim,压缩和解压缩)

一、vim编辑器

1、移动
  • h:向左移动 (数字加h,向左移动几个数)

  • l:向右移动(数字加l,向右移动几个数)

  • j:向下移动 (数字加j,向下移动几个数)

  • k:向上移动 (数字加k,向上移动几个数)

  • w:跳到下一个单词的开头

  • e:跳到下一个单词的末尾

  • b:跳到上一个单词的开头

  • 数字+w/e/b:跳多少个单词

  • CTRL+f:向下翻页

  • CTRL+b:向上翻页

  • shift+PgUp:向上翻页

  • shift+PgDn:向下翻页

2、跳转
  • shift+home:跳到行首

  • shift+end:跳到行末

  • ^:跳到行首(略过空格)

  • 0:跳到行首(包括空格)

  • $:跳到行末

  • gg:跳到首行

  • G:跳到行末

  • 数字+gg:调到指定行数 ( : set nu : set nonu)

3、删除
  • dd:删除整行

  • 数字+dd:删除多行(当前光标所在行和下边行数)

  • shift+d:删除光标后边内容

  • d+$:删除光标后边内容

  • d+^:删除光标前边内容

  • x/delete:删除光标所在字符

4、复制粘贴
  • yy:复制(复制光标所在行)

  • p:粘贴(粘贴到光标所在下一行)

  • 数字+yy:复制多行(光标以及下一行)

  • p:粘贴(粘贴到光标所在下一行)

  • shift+p:(粘贴到光标所在上一行)

5、查找
  • /+关键字(以光标为基准,从上边开始向下查找)

  • n:下一个关键字

  • shift+n:上一个关键字

  • ?+ 关键字(以光标为基准,从下边开始向上查找)

  • : s /oldname/newname 替换光标所在行的第一个字段

  • : s /oldname/newname/g 替换光标所在行的所有字段

  • :行号1,行号2 s/oldname/newname 替换指定行间的第一个字段

  • :行号1,行号2 s/oldname/newname/g 替换指定行间的所有字段

  • :行号1,行号2 s/oldname/newname/c 提示是否替换指定行间的第一个字段

  • :行号1,行号2 s/oldname/newname/gc 提示是否替换指定行间的所有字段

  • % s/oldname/newname 替换全文的第一个字段

  • % s/oldname/newname/g 替换全文的所有字段

  • % s/oldname/newname/c 提示是否替换全文的第一个字段

  • % s/oldname/newname/gc 提示是否替换全文的所有字段

  • % s/oldname/newname/gic 提示是否替换全文的所有字段(不区分大小写)

6、插入
  • a:在光标后边新增
  • A:光标所在行最后边新增
  • i:在光标前边插入
  • I:在光标所在行最前边插入
  • o:在光标所在行下边新增
  • O:在光标所在行上边新增
7、替换
  • r:替换光标所在字符
  • R:替换整个字符;用ESC键退出替换
8、撤销
  • u:撤销上一次操作
  • 数字+u:撤回多步操作
  • CTRL+r:恢复上一步操作
  • . :重复上一步操作
9、可视化
  • v:按字符选取,用键盘控制光标选取
  • V:按矩形选取,键盘控制光标选取(然后按d删除)
10、与shell交互
  • :e /文件路径/文件名 打开一个新文件并编辑

  • :e ~/passwd1 ==> :e /root/passwd1

  • ~:表示用户家目录

  • . :表示当前目录

  • :r /文件路径/文件名 读入一个文件内容

  • :r /etc/passwd

  • w /root/新文件名 另存为新文件名(源文件还在)

  • w!/root/存在文件名 覆盖已存在文件(源文件存在)

  • :!command 执行输入命令

11、打开
  • vim +200 filename 打开文件光标在第200行

  • vim +/关键字 filename 打开文件后上一次光标所在位置下边的第一个关键字上(上一次保存时候光标所在位置)

二、压缩和解压缩

1.压缩文件和解压文件
  • zip和unzip

  • zip 压缩文件名(test.zip)要被压缩的文件名字

    [root@localhost tmp]# zip test1.zip test1.txt
    
  • zip -m test1.zip test2.txt 把test2添加到test1.zip中(test2就不存在了)

    [root@localhost tmp]# zip test1.zip test1.txt
      adding: test1.txt (stored 0%)
    [root@localhost tmp]# zip -m test1.zip test2.txt
      adding: test2.txt (stored 0%)
    [root@localhost tmp]# ls
    test1.txt  test1.zip
    [root@localhost tmp]#
    
  • zip -d test1.zip test2.txt 把test2.txt从test1.zip删除掉

    • [root@localhost tmp]# zip -d test1.zip test2.txt
      deleting: test2.txt
      
    
    
  • zip test1.zip ./* -x test10.txt 除了test10.txt以外,将所有的.txt的压缩

    [root@localhost tmp]# zip test1.zip  ./* -x test10.txt
      adding: test1.txt (stored 0%)
    [root@localhost tmp]# ls
    test10.txt  test1.txt  test1.zip
    
  • zip -r /dir递归压缩

  •   [root@localhost tmp]# zip -r dir.zip dir
      adding: dir/ (stored 0%)
        adding: dir/file1 (stored 0%)
      adding: dir/file2 (stored 0%)
        adding: dir/file3 (stored 0%)
        adding: dir/file4 (stored 0%)
        adding: dir/file5 (stored 0%)
      [root@localhost tmp]# ls
      dir  dir.zip  test10.txt  test1.txt  test1.zip  vmware-root_6188-994163143
    
    • unzip test1.zip 解压文件

      [root@localhost tmp]# unzip test1.zip
      
    • unzip test1.zip -d/ 解压目录

      [root@localhost tmp]# unzip dir.zip
      
    • unzip -v test1.zip 查看压缩文件

      [root@localhost tmp]# unzip -v dir.zip
      Archive:  dir.zip
       Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
      --------  ------  ------- ---- ---------- ----- --------  ----
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/file1
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/file2
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/file3
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/file4
             0  Stored        0   0% 07-30-2019 23:26 00000000  dir/file5
      --------          -------  ---                            -------
             0                0   0%                            6 files
      
  • gzip,gunzip

    • gzip 被压缩的文件名 压缩后,源文件不保留

      [root@localhost tmp]# gzip test1
      [root@localhost tmp]# ls
      test1.gz
      
    • gzip -c test.txt > test.txt.gz 保留原文件压缩

      [root@localhost tmp]# ls
      test1
      [root@localhost tmp]# gzip -c test1 > test1.gz
      [root@localhost tmp]# ls
      test1  test1.gz
      
    • gzip -r 压缩目录下的所有内容(但不压缩目录)

      [root@localhost tmp]# gzip -r dir/
      [root@localhost tmp]# ls
      dir
      
    • gunzip 被解压的文件名

      [root@localhost tmp]# gunzip test1.gz
      
    • gunzip -c test.txt.gz > /tmp/test.txt 解压到特定目录下,并且保留源文件

      [root@localhost tmp]# gunzip  -c test.txt.gz  >  /tmp/test.txt
      
    • gunzip ===>gzip -d

  • bzip2,bunzip2

    • bzip2 被压缩的文件名,源文件不保留

      [root@localhost tmp]# bzip2 test1
      [root@localhost tmp]# ls
      test1.bz2
      
    • bzip2 -c test.txt > test.txt.bz2 保留原文件压缩

      [root@localhost tmp]# ls
      test1
      [root@localhost tmp]# bzip2 -c test1 > test1.bz2
      [root@localhost tmp]# ls
      test1  test1.bz2
      
    • bunzip2 被解压的文件名

    • [root@localhost tmp]# bunzip2 test1.bz2
      
    • bunzip2 -c test.txt.bz2 > /tmp/test.txt

      [root@localhost tmp]# bunzip2  -c test.txt.bz2  >  /tmp/test.txt
      
    • bunzip ===>bzip -d

  • xz,unxz

    • xz 被压缩的文件名,源文件不保留

    • xz dir/* 压缩dir目录下的所有文件

    • unxz -d dir 解压目录

    • unxz dir 解压目录

    • xzcat 显示文件内容

      [root@localhost tmp]# zcat test1.xz
      this
      is
      test1
      
2、显示压缩文件内容:zcat、zless、bzcat、bzless
  • zcat

    • [root@localhost tmp]# zcat test1.gz
      this
      is
      test1
      
  • zless 进入文件查看

    • this
      is
      test1
      test1.gz (END)
      
  • bzcat

    • [root@localhost tmp]# bzcat test1.bz2
      this
      is
      test1
      
  • bzless 进入文件查看

    • this
      is
      test1
      test1.gz (END)
      

      练习作业
      1、新建系统组mariadb, 新建系统用户mariadb, 属于mariadb组,要求其没有家目录,且shell为/sbin/nologin;尝试root切换至用户,查看其命令提示符;
      [root@localhost ~]# groupadd -r mariadb
      [root@localhost ~]# useradd -M -s /sbin/nologin -g mariadb mariadb

      2、新建GID为5000的组nebulaedu,新建用户gentoo,要求其家目录为/users/gentoo,密码同用户名;、
      先把/users这个目录建好,然后使用useradd -d /users/gentoo gentoo
      [root@localhost ~]# groupadd -g 5000 nebulaedu
      [root@localhost ~]# mkdir /users
      [root@localhost ~]# useradd -d /users/gentoo gentoo

      3、新建用户fedora,其家目录为/users/fedora,密码同用户名;
      [root@localhost ~]# useradd -d /users/fedora fedora

      4、新建用户www, 其家目录为/users/www;删除www用户,但保留其家目录;
      [root@localhost ~]# useradd -d /users/www www
      [root@localhost ~]# userdel www

      5、为用户gentoo和fedora新增附加组nebulaedu;
      [root@localhost ~]# usermod -aG nebulaedu gentoo
      [root@localhost ~]# usermod -aG nebulaedu fedora

      6、复制目录/var/log至/tmp/目录,修改/tmp/log及其内部的所有文件的属组为nebulaedu,并让属组对目录本身拥有写权限
      [root@localhost tmp]# cp -r /var/log /tmp/
      [root@localhost tmp]# chgrp -R nebulaedu /tmp/log

      研发部开发人员David和Peter属于组A,行政部人员Jack和Mike属于组B;
      [root@localhost tmp]# groupadd A
      [root@localhost tmp]# groupadd B
      [root@localhost tmp]# useradd -g A David
      [root@localhost tmp]# useradd -g A Peter
      [root@localhost tmp]# useradd -g B Jack
      [root@localhost tmp]# useradd -g B Mike

      1.建立目录“/project_a”,该目录里面的文件只能由研发部开发人员读取、增加、删除、修改以及执行,其他用
      户不能对该目录进行任何的访问操作;并要求在此目录下创建的文件研发组内成员可以互相访问
      [root@localhost ~]# mkdir /project_a
      [root@localhost ~]# chgrp A /project_a
      [root@localhost ~]# chmod g=rwx /project_a/
      [root@localhost ~]# chmod o=— /project_a/

      2.建立目录“/project_b”,该目录里面的文件只能由行政部人员读取、增加、删除、修改以及执行,其他用户不
      能对该目录进行任何的访问操作;要求在此目录下创建的文件行政部人员只能删除自己的文件,不得删除其他人员文件

      [root@localhost ~]# mkdir /project_b
      [root@localhost ~]# chgrp B /project_b/
      [root@localhost ~]# chmod o=— /project_b
      [root@localhost ~]# chmod o+t /project_b

      drwxr-x–T. 2 root B 6 Jul 29 23:30 project_b

发布了33 篇原创文章 · 获赞 6 · 访问量 668

猜你喜欢

转载自blog.csdn.net/qq_42508901/article/details/97897953