归档及压缩、重定向与管道操作、管道与重定向综合使用、find精确查找、find处理查找结果、vim高级使用、vimdiff多文件使用

1 打开虚拟机

2 制作tar包(tar打包)

格式:tar 选项 /路径/压缩包名字 /源数据.......

-c:动作为创建

-f:指定压缩包名字(必须在所有选项最后)

-z、-j、-J:调用 .gz、.bz2、.xz 格式工具进行处理

    [root@localhost /]# tar -zcf  /root/cbd.tar.gz  /etc/passwd  /home
    [root@localhost /]# ls  /root          
          
    [root@localhost /]# tar -jcf /root/haha.tar.bz2  /etc/passwd  /home
    [root@localhost /]# ls   /root
    [root@localhost /]# tar -Jcf  /root/cctv.tar.xz  /etc/passwd   /home
    [root@localhost /]# ls   /root

3 释放tar包(tar解包)

格式:tar 选项 /路径/压缩包名字 选项 /释放的位置

-x:释放归档

-f:指定归档文件名称,必须在所有选项的最后

-C(大写):指定路径

    [root@localhost /]# tar  -tf  /root/haha.tar.bz2  #查看tar包内容
    [root@localhost /]# mkdir   /nsd11     
    [root@localhost /]# tar -xf  /root/haha.tar.bz2    -C     /nsd11
    [root@localhost /]# ls    /nsd11
    [root@localhost /]# ls    /nsd11/etc
    [root@localhost /]# ls    /nsd11/home
    [root@localhost /]# tar  -tf  /root/cbd.tar.gz   #查看tar包内容
    [root@localhost /]# mkdir   /nsd12     
    [root@localhost /]# tar -xf  /root/cbd.tar.gz    -C     /nsd12
    [root@localhost /]# ls    /nsd12
    [root@localhost /]# ls    /nsd12/etc

4 tar高级打包

利用-C指定路径

    [root@localhost /]# tar -zcf /root/yy.tar.gz  -C  /etc/   passwd  -C  /etc/sysconfig/network-scripts/    ifcfg-lo
    [root@localhost /]# tar -tf  /root/yy.tar.gz
    [root@localhost /]# mkdir   /nsd12
    [root@localhost /]# tar   -xf   /root/yy.tar.gz    -C  /nsd12
    [root@localhost /]# ls   /nsd12
    [root@localhost /]# tar -zcf /root/zz.tar.gz   -C  /etc  passwd   shells    hosts   fstab
    [root@localhost /]# tar -tf  /root/zz.tar.gz

5 案例:创建一个备份包

5.1 问题

本例要求使用tar工具完成以下备份任务:

  1. 创建一个名为/root/backup.tar.bz2的归档文件
  2. 其中包含/usr/local目录中的内容
  3. tar归档必须使用bzip2进行压缩步骤

实现此案例需要按照如下步骤进行。

步骤一:创建备份文件

使用tar命令制作归档备份,结合-j选项调用bzip2压缩工具,保留路径:

    [root@server0 ~]# tar  -jcf  /root/backup.tar.bz2  /usr/local/

步骤二:确认结果

    [root@server0 ~]# ls  -lh  /root/backup.tar.bz2          //确认文件
    -rw-r--r--. 1 root root 1.9K 12月 23 23:22 /root/backup.tar.bz2
    [root@server0 ~]# tar  -tf  /root/backup.tar.bz2         //确认内容
    usr/local/
    usr/local/bin/
    usr/local/bin/lab
    usr/local/etc/
    usr/local/games/

6 重定向(重新定向命令的输出)

将前面命令的输出,作为内容,写入到后面的文件

>:覆盖重定向

>>:追加重定向

    [root@A /]# head -5 /etc/passwd > /opt/p.txt
    [root@A /]# cat  /opt/p.txt
    [root@A /]# head -2 /etc/passwd > /opt/p.txt
    [root@A /]# cat  /opt/p.txt
    [root@A /]# hostname 
    [root@A /]# hostname   >>   /opt/p.txt 
    [root@A /]# cat  /opt/p.txt

echo命令的使用

    [root@localhost /]# echo     123456           
    [root@localhost /]# echo   123456    >    /opt/p.txt
    [root@localhost /]# cat     /opt/p.txt
    [root@localhost /]# echo     hello  world           
    [root@localhost /]# echo   hello  world    >>    /opt/p.txt
    [root@localhost /]# cat     /opt/p.txt
    [root@localhost /]# cat     /etc/hostname 
    [root@localhost /]# echo    nb.tedu.cn    >    /etc/hostname
    [root@localhost /]# cat    /etc/hostname

重定向高级使用

    [root@localhost /]# cat   /opt/p.txt               
    [root@localhost /]# >  /opt/p.txt   #清空文件内容
    [root@localhost /]# cat   /opt/p.txt

7 管道(操作符号 | )

作用:将前面命令的输出,传递给后面命令,作为后面命令的参数

    [root@localhost /]# head  -4  /etc/passwd   |      tail  -1 
    [root@localhost /]# head  -8  /etc/passwd  |   tail  -1
    [root@localhost /]# cat -n  /etc/passwd  |  head  -8  | tail  -1
    [root@localhost /]# ifconfig  |  head  -2

显示8~12行内容

    [root@localhost /]# head  -12  /etc/passwd   |    tail  -5
    [root@localhost /]# cat -n  /etc/passwd   |  head -12
    [root@localhost /]# cat -n  /etc/passwd  |  head  -12  |  tail -5
    [root@localhost /]# cat -n  /etc/passwd  |  head -12  |   tail -5  > /opt/pa.txt
    [root@localhost /]# cat  /opt/pa.txt       

grep高级使用

     作用:从文本文件内容中,过滤关键字符串
    [root@localhost /]# grep  root  /etc/passwd
    [root@localhost /]# grep -v  root  /etc/passwd   #取反匹配
    [root@localhost /]# grep  ^root  /etc/passwd  #以root开头
    [root@localhost /]# grep  bash$  /etc/passwd  #以bash结尾
    ^$:表示空行,专门与-v选项连用,过滤不要空行
    [root@localhost /]# cat  /etc/default/useradd
    [root@localhost /]# grep  -v  ^$   /etc/default/useradd

Linux中大多数配置文件内容,以#开头的行为注释行

显示配置文件有效信息(去除以#开头的注释行和去除空行)

    [root@localhost /]# grep -v  ^#  /etc/login.defs
    [root@localhost /]# grep -v  ^#  /etc/login.defs  |  grep -v  ^$
    [root@localhost /]# grep -v ^# /etc/login.defs   |  grep -v ^$  > /opt/log.txt
    [root@localhost /]# cat  /opt/log.txt

过滤命令的输出

    [root@localhost /]# ifconfig  |    grep  inet
    [root@localhost /]# ifconfig   |    grep  127
    [root@localhost /]# ifconfig    |    less       #方便查看

8 案例:重定向与管道操作

8.1 问题

  1. 显示ifconfig命令的前两行内容
  2. 显示/etc/passwd第九行内容
  3. 将hostname命令的输出内容,覆盖写入到/opt/hn.txt
  4. 利用echo命令,将"tmooc"内容追加写入到/opt/hn.txt

8.2 步骤

实现此案例需要按照如下步骤进行。

1)显示ifconfig命令的前两行内容

    [root@server0 ~]# ifconfig  |  head  -2

2)显示/etc/passwd第九行内容

    [root@server0 ~]# head  -9  /etc/passwd  |  tail -1

3)将hostname命令的输出内容,覆盖写入到/opt/hn.txt

    [root@server0 ~]# hostname  >  /opt/hn.txt

4)利用echo命令,将"tmooc"内容追加写入到/opt/hn.txt

    [root@server0 ~]# echo  tmooc  >>  /opt/hn.txt

9 find精确查找

格式:find [目录] [条件1]

-type 类型(f文本文件、d目录、l快捷方式)

    [root@A /]# find    /boot     -type    d
    [root@A /]# find    /opt     -type    d
    [root@A /]# find    /etc     -type    l   
    [root@A /]# find    /boot    -type    f
    [root@A /]# find    /usr     -type    d
    [root@A /]# find    /var     -type    d
    [root@A /]# find    /sbin     -type    l   
    [root@A /]# find    /bin     -type    f

-name "文档名称" (-iname 忽略大小写)

    [root@localhost /]# find    /etc    -name    "passwd"
    [root@localhost /]# find    /etc    -name    "*tab"
    [root@localhost /]# find    /etc    -name    "*.conf"
    [root@localhost /]# find  /root  -name  ".*"   #查找隐藏数据
    [root@localhost /]# find  /boot  -type  d  |  cat  -n
    [root@localhost /]# find  /opt  -type  d   |  wc  -l
    [root@localhost /]# find  /etc  -type  l    |  cat  -n
    [root@localhost /]# find  /boot  -type  f   |  cat  -n
    [root@localhost /]# find  /boot  -type  f   |  wc  -l
    [root@localhost /]# find   /etc    -name   "*tab"   |  wc  -l
    [root@localhost /]# find   /etc    -name  "*.conf"   |  wc  -l
    [root@localhost /]# find   /etc   -name  "*.conf"  |  cat  -n

两个条件联合使用

[root@localhost /]# mkdir   /mnt/cbd01
[root@localhost /]# mkdir   /mnt/cbd02
[root@localhost /]# touch   /mnt/cbd03.txt
[root@localhost /]# find   /mnt/   -name   "cbd*"
[root@localhost /]# find   /mnt/   -name   "cbd*"  -type   d
[root@localhost /]# find   /mnt/   -name   "cbd*"  -type   f
[root@localhost /]# find   /mnt/   -name   "cbd*" -o  -type   f    #两个满足其中一个

-size +或- 文件大小(k、M、G)

    [root@localhost /]# find   /boot   -size  +300k
    [root@localhost /]# find   /boot   -size  +10M
    [root@localhost /]# find   /boot   -size  +1M
    [root@localhost /]# find   /boot   -size  +10M    -size  -50M

-user 用户名 (按照数据的所有者)

    [root@A /]# useradd  natasha  #创建用户
    [root@A /]# find  /home   -user    natasha
    [root@A /]# find    /      -user    natasha
    /proc:内存的数据,不占用硬盘空间
    [root@A /]# useradd  harry  #创建用户
    [root@A /]# find  /home   -user    harry
    [root@A /]# find    /      -user    harry

-mtime 修改时间 (所有的时间都是过去时间)

-mtime +90 #90天之前的数据

-mtime -90 #最近90天之内的数据

三个月之前的数据:    
[root@A /]# find  /var  -mtime  +90
最近10天之内的数据:
[root@A /]# find  /root  -mtime  -10

10 find高级使用

处理find找到的数据,每查找的一个就传递一次

find [范围] [条件] -exec 处理命令 {} \;

-exec额外操作的开始

{} 永远表示前面find查找的结果

\; 额外操作的结束

    [root@localhost /]# find   /boot   -size     +10M
    [root@localhost /]# find /boot -size +10M -exec  cp  {}  /mnt \;      
    [root@localhost /]# ls    /mnt
    [root@localhost /]# find  /boot  -size +10M  -exec  ls -lh  {}  \;

两个条件联合使用

    [root@localhost /]# mkdir   /root/mytab
    [root@localhost /]# find    /etc   -name   "*tab"    -type   f
    [root@localhost /]# find   /etc    -name  "*tab"    -type   f
     -exec   cp       {}    /root/mytab    \;
    [root@localhost /]# ls    /root/mytab 

案例:查找并处理文件

  1. 利用find查找,数据的所有者为 student,并且必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中

     [root@localhost /]# useradd  student           #创建普通用户student     
     [root@localhost /]# mkdir  /root/findfiles   
                                                
     [root@localhost /]# find   /    -user  student   -type  f
     [root@localhost /]# find  /  -user student  -type  f  -exec cp  {}  /root/findfiles   \;
     [root@localhost /]# ls  -A  /root/findfiles/   #-A显示隐藏数据
    

11 案例:查找并处理文件

11.1 问题

  1. 利用find查找所有用户student拥有的必须是文件,把它们拷贝到 /root/findfiles/文件夹中
  2. 利用find查找/boot目录下大于10M并且必须是文件,拷贝到/opt
  3. 利用find查找/boot/ 目录下以 vm 开头且必须是文件,拷贝到/opt
  4. 利用find查找/boot/目录下为快捷方式
  5. 利用find查找/etc目录下,以tab作为结尾的必须是文件

11.2 方案

根据预设的条件递归查找对应的文件

格式:find [目录] [条件1]

常用条件表示:

  • -type 类型(f文件、d目录、l快捷方式)
  • -name "文档名称"
  • -size +|-文件大小(k、M、G)
  • -user 用户名
  • -mtime 修改时间

高级使用(处理find查找的结果)

  • -exec 额外操作的开始
  • \; 表示额外操作的结束
  • {} 前面find命令每一个查询的结果

11.3 步骤

实现此案例需要按照如下步骤进行。

1)利用find查找所有用student拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中(确保本机具有student用户)

    [root@server0 ~]# useradd  student
    [root@server0 ~]# mkdir /root/findfiles
    [root@server0 ~]# find /   -user   student   -type f
    [root@server0 ~]# find /   -user   student   -type f  -exec  cp  {}  /root/findfiles \;
     [root@server0 ~]#  ls  /root/findfiles

2)利用find查找/boot目录下大于10M并且必须是文件,拷贝到/opt

    [root@server0 ~]# find  /boot  -size +10M
    [root@server0 ~]# find  /boot  -size +10M --type  f  -exec  cp  {}  /opt  \;
    [root@server0 ~]# ls  /opt

3)利用find查找/boot/ 目录下以 vm 开头且必须是文件,拷贝到/opt

    [root@server0 ~]# find  /boot  -name  "vm*"  
    [root@server0 ~]# find  /boot  -name  "vm*"  -type f -exec  cp  {}   /opt  \;
    [root@server0 ~]# ls   /opt

4)利用find查找/boot/ 目录下为快捷方式

    [root@server0 ~]# find  /boot  -type  l

5)利用find查找/etc 目录下,以 tab 作为结尾的 必须是文件

    [root@server0 ~]# find  /etc   -name  "*tab"  -type f

12 vim末行模式操作

读取文件内容

    [root@localhost /]# echo  123456  >  /opt/aa.txt
    [root@localhost /]# echo  hahaxixi  >  /opt/cc.txt
    [root@localhost /]# vim    /opt/cc.txt
    末行模式下    :r  /opt/aa.txt
    末行模式下    :r  /etc/passwd
    末行模式下    :r  /etc/shells

字符串替换

:1,10s/root/new/g 替换第1-10行所有的"root"

:%s/root/new/g 替换文件内所有的"root"

    [root@localhost /]# cp  /etc/passwd    /opt/s.txt
    [root@localhost /]# vim  /opt/s.txt

开关参数的控制

:set nu或nonu 显示/不显示行号

:set ai或noai 启用/关闭自动缩进

    [root@localhost /]# vim  /opt/h.txt
    :set   ai

永久开关功能设置

    [root@nb ~]# vim   /root/.vimrc 
    set  nu
    [root@nb ~]# vim  /etc/passwd  #测试是否开启行号
    [root@nb ~]# vim  /opt/pass.txt

13 vimdiff同时比对或修改多个文件

    [root@a ~]# echo hahaxixi > /opt/aa.txt
    [root@a ~]# echo hahaabc > /opt/bb.txt
    [root@a ~]# vimdiff  /opt/aa.txt   /opt/bb.txt
    命令模式下Ctrl与w同时按下,然后左右键移动光标
    末行模式wqa保存全部文件并退出

14 案例:vim效率操作

14.1 问题

本例要求掌握使用vim文本编辑器时能够提高操作效率的一些常用技巧和方法,完成下列任务:

  1. 将文件 /etc/passwd 复制为 /opt/nsd.txt,然后打开 /opt/nsd.txt 文件,练习命令模式下的切换/复制/删除/查找操作
  2. 将文件 /etc/man_db.conf 复制到 /opt 目录下,然后打开 /opt/man_db.conf 文件,将第50~100行内的"man"替换为"MAN",在 vim 中设置显示行号查看效果

14.2 方案

命令模式常用操作:

  • 1G 或 gg ,跳转到文件的首行
  • G ,跳转到文件的末尾行
  • yy、#yy ,复制光标处的一行、#行
  • p、P ,粘贴到光标处之后、之前
  • x 或 Delete键 ,删除光标处的单个字符
  • dd、#dd ,删除光标处的一行、#行
  • d^、d$ ,从光标处之前删除至行首/行尾
  • /word 向后查找字符串"word",再按n/N跳至后/前一个结果
  • u ,撤销最近的一次操作
  • U ,撤销对当前行的所有修改
  • Ctrl + r 取消前一次撤销操作
  • ZZ 保存修改并退出

末行模式常用操作:

  • :s/old/new ,替换当前行第一个"old"
  • :s/old/new/g ,替换当前行所有的"old"
  • :n,m s/old/new/g ,替换第n-m行所有的"old"
  • :% s/old/new/g ,替换文件内所有的"old"
  • :w /root/newfile ,另存为其它文件
  • :r /etc/filesystems ,读入其他文件内容
  • :set nu|nonu ,显示/不显示行号
  • :set ai|noai ,启用/关闭自动缩进

14.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:vim命令模式下的切换/复制/删除/查找

1)建立练习文件

将文件 /etc/passwd 复制为 /opt/nsd.txt:

    [root@svr7 ~]# cp  /etc/passwd  /opt/nsd.txt

2)使用vim打开练习文件,默认处于命令模式

    [root@svr7 ~]# vim  /opt/nsd.txt
    .. ..

3)在命令模式下完成下列操作

切换操作:G 最后一行,5G 第5行,gg 第一行。

复制操作:按2yy复制2行,7G移动到第7行,p 粘贴。

删除操作:25G 移动到第25行,200dd 从此行开始删除200行(不够就剩下全删)。

查找操作:gg 第一行,/adm 查找关键词adm,n 跳转到下一个结果。

4)保存并退出编辑器

ZZ 保存退出。

步骤二:vim末行模式下的替换/设置操作

1)建立练习文件

将文件 /etc/man_db.conf 复制到 /opt/ 目录下:

    [root@svr7 ~]# cp  /etc/man_db.conf  /opt/

2)使用vim打开练习文件,输入:切换到末行模式

    [root@svr7 ~]# vim  /opt/man_db.conf
    .. ..
    : 

3)在末行模式下完成下列操作

输入 :set nu ,确认后显示行号。

输入 :50,100 s/man/MAN/g ,确认将第50~100行内的"man"替换为"MAN"。

4)保存并退出编辑器

输入 :wq ,确认后保存并退出编辑器。

相关推荐
Ambition_LAO5 分钟前
解决:进入 WSL(Windows Subsystem for Linux)以及将 PyCharm 2024 连接到 WSL
linux·pycharm
Pythonliu722 分钟前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
你疯了抱抱我23 分钟前
【RockyLinux 9.4】安装 NVIDIA 驱动,改变分辨率,避坑版本。(CentOS 系列也能用)
linux·运维·centos
追风赶月、24 分钟前
【Linux】进程地址空间(初步了解)
linux
栎栎学编程24 分钟前
Linux中环境变量
linux
挥剑决浮云 -1 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
小O_好好学2 小时前
CentOS 7文件系统
linux·运维·centos
x晕x3 小时前
Linux dlsym符号查找疑惑分析
linux·运维·服务器
活跃的煤矿打工人3 小时前
【星海saul随笔】Ubuntu基础知识
linux·运维·ubuntu
fasewer4 小时前
第五章 linux实战-挖矿 二
linux·运维·服务器