归档及压缩、重定向与管道操作和综合使用,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 /# 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

释放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

tar高级打包

利用-C指定路径

  1. root@localhost /# tar -zcf /root/yy.tar.gz -C /etc/ passwd -C /usr games
  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/

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

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

>:覆盖重定向

>>:追加重定向

  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

管道(操作符号 | )

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

  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

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

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/ 文件夹中

  2. root@localhost /# useradd student #创建普通用户student

  3. root@localhost /# mkdir /root/findfiles

  4. root@localhost /# find / -user student -type f

  5. root@localhost /# find / -user student -type f -exec cp {} /root/findfiles \;

  6. 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 \~# 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

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

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 ,启用/关闭自动缩进

步骤

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

步骤一: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

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

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

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

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

4)保存并退出编辑器

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

相关推荐
2601_956414147 小时前
迈向智慧实验室:金现代的全链路质量管控与自动化解决方案
运维·自动化
Jurio.8 小时前
tmux 安装与使用教程:SSH 断开后任务继续运行,终端分屏与多窗口管理
linux·经验分享·ssh·tmux
YJlio8 小时前
《Sysinternals实战指南》16.5 Ctrl2Cap 工具详解:把 Caps Lock 变成 Ctrl 的键盘改造与回退方法
linux·运维·服务器·网络·python·学习·计算机外设
l'm coming8 小时前
[linux]内核启动加载驱动文件的流程
linux·arm开发·驱动开发·嵌入式
王二端茶倒水8 小时前
智慧小区宽带无线运营:从网络交付到认证、计费与运维闭环
运维·物联网·架构
旅僧8 小时前
远程终端工具安装
运维
一拳一个娘娘腔8 小时前
CVE-2026-31431 — “Copy Fail“ 深度拆解
linux·安全
OpsEye8 小时前
日志、指标、链路追踪,谁更适合定位故障?
运维·监控·日志分析
麦麦麦当劳大王9 小时前
Linux SSH服务端配置指南
linux·运维·服务器·ssh
xiami_world9 小时前
私有化部署协同白板选型指南:从Docker容器化到信创全栈适配的架构实践
运维·人工智能·docker·ai·持续部署