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

Top

NSD ADMIN DAY04

  1. 往日重现:明天尚未可知,今天是上天的恩赐
  2. 制作tar包(tar打包)
  3. 释放tar包(tar解包)
  4. tar高级打包
  5. 案例:创建一个备份包
  6. 重定向(重新定向命令的输出)
  7. 管道(操作符号 | )
  8. 案例:重定向与管道操作
  9. find精确查找
  10. find高级使用
  11. 案例:查找并处理文件
  12. vim末行模式操作
  13. vimdiff同时比对或修改多个文件
  14. 案例:vim效率操作
  15. 课后基础练习:

1 往日重现:明天尚未可知,今天是上天的恩赐

1.1 Linux中系统中默认的解释器是哪个程序?

1.2 如何结束当前正在运行的命令,快捷键是?

1.3 如何快速去往用户root的家目录?

1.4 ls常用选项有哪些?不用写作用

1.5 如何查看Linux系统具体版本?

1.6 实现hn相当于运行hostname,如何实现?

1.7 强制删除一个文件夹以及其内容的命令是?

1.8 cp命令如果源数据是一个目录,需要加什么选项?

1.9 vim编辑器有几种工作模式?分别是什么?

1.10 过滤/etc/passwd文件内容,不包含root的行,命令是?

1.11 将/opt重新命名成/student,具体命令是?

2 制作tar包(tar打包)

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

-c:动作为创建

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

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

 
  1. [root@localhost /]# tar -zcf /root/cbd.tar.gz /etc/passwd /home
  2. [root@localhost /]# ls /root
  3. [root@localhost /]# tar -jcf /root/haha.tar.bz2 /etc/passwd /home
  4. [root@localhost /]# ls /root
  5. [root@localhost /]# tar -Jcf /root/cctv.tar.xz /etc/passwd /home
  6. [root@localhost /]# ls /root

3 释放tar包(tar解包)

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

-x:释放归档

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

-C(大写):指定路径

 
  1. [root@localhost /]# tar -tf /root/haha.tar.bz2 #查看tar包内容
  2. [root@localhost /]# mkdir /nsd11
  3. [root@localhost /]# tar -xf /root/haha.tar.bz2 -C /nsd11
  4. [root@localhost /]# ls /nsd11
  5. [root@localhost /]# ls /nsd11/etc
  6. [root@localhost /]# ls /nsd11/home
  7. [root@localhost /]# tar -tf /root/cbd.tar.gz #查看tar包内容
  8. [root@localhost /]# mkdir /nsd12
  9. [root@localhost /]# tar -xf /root/cbd.tar.gz -C /nsd12
  10. [root@localhost /]# ls /nsd12
  11. [root@localhost /]# ls /nsd12/etc

4 tar高级打包

利用-C指定路径

 
  1. [root@localhost /]# tar -zcf /root/yy.tar.gz -C /etc/ passwd -C /etc/sysconfig/network-scripts/ ifcfg-lo
  2. [root@localhost /]# tar -tf /root/yy.tar.gz
  3. [root@localhost /]# mkdir /nsd12
  4. [root@localhost /]# tar -xf /root/yy.tar.gz -C /nsd12
  5. [root@localhost /]# ls /nsd12
  6. [root@localhost /]# tar -zcf /root/zz.tar.gz -C /etc passwd shells hosts fstab
  7. [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压缩工具,保留路径:

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

步骤二:确认结果

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

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

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

>:覆盖重定向

>>:追加重定向

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

echo命令的使用

 
  1. [root@localhost /]# echo 123456     
  2. [root@localhost /]# echo 123456 > /opt/p.txt
  3. [root@localhost /]# cat /opt/p.txt
  4. [root@localhost /]# echo hello world     
  5. [root@localhost /]# echo hello world >> /opt/p.txt
  6. [root@localhost /]# cat /opt/p.txt
  7. [root@localhost /]# cat /etc/hostname
  8. [root@localhost /]# echo nb.tedu.cn > /etc/hostname
  9. [root@localhost /]# cat /etc/hostname

重定向高级使用

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

7 管道(操作符号 | )

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

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

显示8~12行内容

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

grep高级使用

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

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

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

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

过滤命令的输出

 
  1. [root@localhost /]# ifconfig | grep inet
  2. [root@localhost /]# ifconfig | grep 127
  3. [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命令的前两行内容

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

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

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

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

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

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

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

9 find精确查找

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

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

 
  1. [root@A /]# find /boot -type d
  2. [root@A /]# find /opt -type d
  3. [root@A /]# find /etc -type l
  4. [root@A /]# find /boot -type f
  5. [root@A /]# find /usr -type d
  6. [root@A /]# find /var -type d
  7. [root@A /]# find /sbin -type l
  8. [root@A /]# find /bin -type f

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

 
  1. [root@localhost /]# find /etc -name "passwd"
  2. [root@localhost /]# find /etc -name "*tab"
  3. [root@localhost /]# find /etc -name "*.conf"
  4. [root@localhost /]# find /root -name ".*" #查找隐藏数据
  5. [root@localhost /]# find /boot -type d | cat -n
  6. [root@localhost /]# find /opt -type d | wc -l
  7. [root@localhost /]# find /etc -type l | cat -n
  8. [root@localhost /]# find /boot -type f | cat -n
  9. [root@localhost /]# find /boot -type f | wc -l
  10. [root@localhost /]# find /etc -name "*tab" | wc -l
  11. [root@localhost /]# find /etc -name "*.conf" | wc -l
  12. [root@localhost /]# find /etc -name "*.conf" | cat -n

两个条件联合使用

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

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

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

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

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

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

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

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

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

10 find高级使用

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

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

-exec额外操作的开始

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

\; 额外操作的结束

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

两个条件联合使用

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

案例:查找并处理文件

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

 
 
  1. [root@localhost /]# useradd student #创建普通用户student
  2. [root@localhost /]# mkdir /root/findfiles
  3. [root@localhost /]# find / -user student -type f
  4. [root@localhost /]# find / -user student -type f -exec cp {} /root/findfiles \;
  5. [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用户)

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

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

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

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

 
  1. [root@server0 ~]# find /boot -name “vm*”
  2. [root@server0 ~]# find /boot -name “vm*” -type f -exec cp {} /opt \;
  3. [root@server0 ~]# ls /opt

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

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

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

 
 
  1. [root@server0 ~]# find /etc -name “*tab” -type f

12 vim末行模式操作

读取文件内容

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

字符串替换

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

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

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

开关参数的控制

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

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

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

永久开关功能设置

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

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

 
  1. [root@a ~]# echo hahaxixi > /opt/aa.txt
  2. [root@a ~]# echo hahaabc > /opt/bb.txt
  3. [root@a ~]# vimdiff /opt/aa.txt /opt/bb.txt
  4. 命令模式下Ctrl与w同时按下,然后左右键移动光标
  5. 末行模式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:

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

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

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

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/ 目录下:

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

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

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

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

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

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

4)保存并退出编辑器

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

15 课后基础练习:

案例1:数据的整理

1. 在目录/mnt下创建一个子目录public

2. 在目录/mnt/public 创建文件linux.txt,利用vim写入内容 Study Linux

3. 将/mnt/public/linux.txt文件复制到/root目录下,同时 改名为 study.txt

4. 利用vim 修改文件/etc/hostname将其原有内容全部删除,写入新的内容为www.qq.com

5. 将/etc/passwd、/etc/resolv.conf、/etc/hostname 同时拷贝到/mnt/public/目录下

6. 将文件 /mnt/public/hostname 重改名为 stu.txt

7. 创建目录结构/mnt/public/test/vm

8. 将目录 /boot内容中以 vm 开头的 复制到/mnt/public/test/vm目录下(建议利用find命令)

9. 将/home目录复制到/mnt/public/test/目录下

案例2:虚拟机上操作:复制、删除、移动及vim文本编辑器

1. 创建目录结构/study/nsd01

2. 在目录/study/nsd01 创建文件abc.txt,利用vim写入内容 abc.tedu.cn

3. 将/study/nsd01/abc.txt文件复制到/opt目录下,同时 改名为 test.txt

4. 利用vim 修改文件/etc/hostname将其原有内容全部删除,写入新的内容为www.sina.com

5. 将/etc/passwd 、/etc/resolv.conf、/etc/hostname 同时拷贝到/study/nsd01/目录下

6. 将文件 /study/nsd01/hostname 重改名为 haxi.txt

7. 创建目录结构/root/vm

8. 将目录 /boot内容中以 vm 开头的 复制到/root/vm目录下

9. 将/home目录复制到/root/vm目录下

案例3:虚拟机上操作

1. 显示根目录下所有内容

2. 显示/etc目录下所有以tab结尾的文件

3. 显示/etc/resolv.conf文件的详细属性并加上易读的单位

4. 显示/etc/passwd文件的详细属性并加上易读的单位

5. 显示/etc/passwd文件的头4行内容

案例4:tar制作/释放归档压缩包

首先创建/root/boothome/与/root/usrsbin/目录

1)备份/boot、/home这两个文件夹,保存为boothome.tar.gz文件

2)查看boothome.tar.gz文件内包含哪些内容

3)将boothome.tar.gz释放到文件夹/root/boothome/下

4)备份/usr/sbin目录,保存为usrsbin.tar.bz2文件

5)查看usrsbin.tar.bz2文件内包含哪些内容

6)将usrsbin.tar.bz2释放到/root/usrsbin/文件夹下

案例5:tar制作/释放归档压缩包

首先创建/root/zhsan目录

1)仅备份/etc/目录下shells与hosts这两个文件,保存为sh.tar.xz文件(不打包路径)

2)查看sh.tar.xz文件内包含哪些内容

3)将sh.tar.xz释放到文件夹/root/zhsan下

案例6:tar制作/释放归档压缩包

首先创建/root/mystudent目录

1)备份/etc/目录下passwd文件 、/boot/目录下vmlinuz-4.18.0-372.9.1.el8.x86_64程序 、/root/目录下的anaconda-ks.cfg文件,保存为myfile.tar.bz2文件(不要压缩路径)

2)查看myfile.tar.xz文件内包含哪些内容

3)将myfile.tar.xz释放到文件夹/root/mystudent下

案例7:虚拟机上操作,查找并处理文件

–创建用户student,创建目录/root/findfiles

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

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

 
  1. [root@localhost /]# find /boot -type f -size +10M
  2. [root@localhost /]# find /boot -type f -size +10M -exec cp {} /opt \;

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

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

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

  • 36
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值