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

制作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 /\]# tar -jcf /root/haha.tar.bz2 /etc/passwd /home

  3. root@localhost /\]# tar -Jcf /root/cctv.tar.xz /etc/passwd /home

释放tar包(tar解包)

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

-x:释放归档

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

-C(大写):指定路径

  1. root@localhost /\]# tar -tf /root/haha.tar.bz2 #查看tar包内容

  2. root@localhost /\]# tar -xf /root/haha.tar.bz2 -C /nsd11

  3. root@localhost /\]# ls /nsd11/etc

  4. root@localhost /\]# tar -tf /root/cbd.tar.gz #查看tar包内容

  5. root@localhost /\]# tar -xf /root/cbd.tar.gz -C /nsd12

  6. root@localhost /\]# ls /nsd12/etc

利用-C指定路径

  1. root@localhost /\]# tar -zcf /root/yy.tar.gz -C /etc/ passwd -C /usr games

  2. root@localhost /\]# mkdir /nsd12

  3. root@localhost /\]# ls /nsd12

  4. root@localhost /\]# tar -tf /root/zz.tar.gz

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/

  2. root@server0 \~\]# ls -lh /root/backup.tar.bz2 //确认文件

  3. root@server0 \~\]# tar -tf /root/backup.tar.bz2 //确认内容

  4. usr/local/bin/

  5. usr/local/bin/lab

  6. usr/local/etc/

  7. usr/local/games/

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

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

>:覆盖重定向

>>:追加重定向

  1. root@A /\]# head -5 /etc/passwd \> /opt/p.txt

  2. root@A /\]# head -2 /etc/passwd \> /opt/p.txt

  3. root@A /\]# hostname

  4. root@A /\]# cat /opt/p.txt

  5. root@localhost /\]# echo 123456

  6. root@localhost /\]# cat /opt/p.txt

  7. root@localhost /\]# echo hello world \>\> /opt/p.txt

  8. root@localhost /\]# cat /etc/hostname

  9. root@localhost /\]# cat /etc/hostname

  10. root@localhost /\]# cat /opt/p.txt

  11. root@localhost /\]# cat /opt/p.txt

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

  1. root@localhost /\]# head -4 /etc/passwd \| tail -1

  2. root@localhost /\]# cat -n /etc/passwd \| head -8 \| tail -1

显示8~12行内容

  1. root@localhost /\]# head -12 /etc/passwd \| tail -5

  2. root@localhost /\]# cat -n /etc/passwd \| head -12 \| tail -5

  3. root@localhost /\]# cat /opt/pa.txt

  4. 作用:从文本文件内容中,过滤关键字符串

  5. root@localhost /\]# grep root /etc/passwd

  6. root@localhost /\]# grep \^root /etc/passwd #以root开头

  7. ^$:表示空行,专门与-v选项连用,过滤不要空行

  8. root@localhost /\]# cat /etc/default/useradd

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

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

  1. root@localhost /\]# grep -v \^# /etc/login.defs

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

过滤命令的输出

  1. root@localhost /\]# ifconfig \| grep inet

  2. root@localhost /\]# ifconfig \| less #方便查看

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. root@server0 \~\]# head -9 /etc/passwd \| tail -1

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

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

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

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

  1. root@A /\]# find /boot -type d

  2. root@A /\]# find /etc -type l

  3. root@A /\]# find /usr -type d

  4. root@A /\]# find /sbin -type l

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

  1. root@localhost /\]# find /etc -name "passwd"

  2. root@localhost /\]# find /etc -name "\*.conf"

  3. root@localhost /\]# find /boot -type d \| cat -n

  4. root@localhost /\]# find /etc -type l \| cat -n

  5. root@localhost /\]# find /boot -type f \| wc -l

  6. root@localhost /\]# find /etc -name "\*.conf" \| wc -l

两个条件联合使用

  1. root@localhost /\]# mkdir /mnt/cbd01

  2. root@localhost /\]# touch /mnt/cbd03.txt

  3. root@localhost /\]# find /mnt/ -name "cbd\*" -type d

  4. root@localhost /\]# find /mnt/ -name "cbd\*" -o -type f #两个满足其中一个

  5. root@localhost /\]# find /boot -size +300k

  6. root@localhost /\]# find /boot -size +1M

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

  1. root@A /\]# useradd natasha #创建用户

  2. root@A /\]# find / -user natasha

  3. root@A /\]# useradd harry #创建用户

  4. root@A /\]# find / -user harry

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

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

  1. 三个月之前的数据:
  2. root@A /\]# find /var -mtime +90

  3. root@A /\]# find /root -mtime -10

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

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

-exec额外操作的开始

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

\; 额外操作的结束

  1. root@localhost /\]# find /boot -size +10M

  2. root@localhost /\]# ls /mnt

两个条件联合使用

  1. root@localhost /\]# mkdir /root/mytab

  2. root@localhost /\]# find /etc -name "\*tab" -type f

  3. root@localhost /\]# ls /root/mytab

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

  5. root@localhost /\]# useradd student #创建普通用户student

  6. root@localhost /\]# find / -user student -type f

  7. root@localhost /\]# ls -A /root/findfiles/ #-A显示隐藏数据

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 \~\]# find / -user student -type f

  3. root@server0 \~\]# ls /root/findfiles

  4. root@server0 \~\]# find /boot -size +10M

  5. root@server0 \~\]# ls /opt

  6. root@server0 \~\]# find /boot -name "vm\*"

  7. root@server0 \~\]# ls /opt

  8. root@server0 \~\]# find /boot -type l

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

读取文件内容

复制代码
  1. root@localhost /\]# echo 123456 \> /opt/aa.txt

  2. root@localhost /\]# vim /opt/cc.txt

  3. 末行模式下 :r /etc/passwd
  4. 末行模式下 :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

开关参数的控制

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

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

  1. root@localhost /\]# vim /opt/h.txt

永久开关功能设置

  1. root@nb \~\]# vim /root/.vimrc

  2. root@nb \~\]# vim /etc/passwd #测试是否开启行号

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

  1. root@a \~\]# echo hahaxixi \> /opt/aa.txt

  2. root@a \~\]# vimdiff /opt/aa.txt /opt/bb.txt

  3. 末行模式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 ,启用/关闭自动缩进

步骤

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

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

1)建立练习文件

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

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

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

切换操作: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. root@svr7 \~\]# vim /opt/man_db.conf

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

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

4)保存并退出编辑器

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

相关推荐
一张假钞2 小时前
Ubuntu 24.04 安装 Jenkins
linux·ci/cd·jenkins
getExpectObject()2 小时前
【jenkins】构建安卓
运维·jenkins
tuokuac3 小时前
查看你电脑上某个端口正在被哪个进程占用
linux
小池先生3 小时前
服务请求出现偶发超时问题,经查服务本身没问题,问题出现在nginx转发。
运维·服务器·nginx
java_logo3 小时前
vllm-openai Docker 部署手册
运维·人工智能·docker·ai·容器
MANONGMN3 小时前
Linux 通配符与正则表达式(含实战案例+避坑指南)
linux·运维·正则表达式
带土13 小时前
18 .shell编程-正则表达式
linux·正则表达式
asdfg12589633 小时前
如何判断一个地址是否可以用作主机 IP 地址?
服务器·网络·tcp/ip
勤源科技3 小时前
运维知识图谱的构建与应用
运维·人工智能·知识图谱