Linux归档及压缩、重定向与管道操作、find精确查找、vim高级使用

一、 环境准备
开启CentOS虚拟机

二、 归档及压缩
作用:1.将零散的数据进行归档 2.减少占用磁盘空间的大小
 归档的含义
– 将许多零散的文件整理为一个文件
– 文件总的大小基本不变
 压缩的含义
– 按某种算法减小文件所占用空间的大小
– 恢复时按对应的逆向算法解压
 Linux常见的压缩格式
.gz  gzip #压缩速度快
.bz2  bzip2
.xz  xz #压缩比例最高

 tar 集成备份工具
– -c:创建归档
– -x:释放归档
– -f:指定归档文件名称,必须放在所有选项的最后
– -z、-j、-J:调用 .gz、.bz2、.xz 格式的工具进行处理
– -t:显示归档中的文件清单
– -C:指定释放路径
 tar 打包格式
tar 选项 /路径/压缩包名字 /路径/被归档压缩的数据……
-c:创建归档 -f:指定压缩包名字
-z、-j、-J:调用 .gz、.bz2、.xz 格式的工具进行处理

]# tar -zcf /opt/nsd01.tar.gz /home /etc/passwd
tar: 从成员名中删除开头的“/”
]# ls /opt/

]# tar -jcf /opt/file.tar.bz2 /home/ /etc/passwd
]# ls /opt/

]# tar -Jcf /opt/abc.tar.xz /home/ /etc/passwd
]# ls /opt/

– -t:显示归档中的文件清单
[root@localhost ~]# tar -tf /opt/nsd01.tar.gz
[root@localhost ~]# tar -tf /opt/file.tar.gz
[root@localhost ~]# tar -tf /opt/abc.tar.gz

 tar 解包格式
tar 选项 /路径/压缩包的名字 -C(大写) /路径/释放位置
]# mkdir /stu01 /stu02 /stu03
]# tar -xf /opt/nsd01.tar.gz -C /stu01/
]# ls /stu01/
]# ls /stu01/etc/
]# ls /stu01/home/

]# tar -xf /opt/file.tar.bz2 -C /stu02/
]# ls /stu02/

]# tar -xf /opt/abc.tar.xz -C /stu03/
]# ls /stu03/

案例1:创建一个备份包
使用 tar 工具完成以下备份任务:
– 创建一个名为 /root/backup.tar.bz2 的归档文件
– 其中包含 /usr/local 目录中的内容
– tar 归档必须使用 bzip2 进行压缩
]# tar -jcf /root/backup.tar.bz2 /usr/local/
tar: 从成员名中删除开头的“/”
]# ls /root/
]# tar -tf /root/backup.tar.bz2 #查看tar包内容

三、 重定向与管道操作
 重定向:将命令输出重新定向,写入文本文件中

:覆盖重定向

:追加重定向
[root@localhost ~]# cat --help > /opt/test.txt
[root@localhost ~]# cat /opt/test.txt

[root@localhost ~]# hostname
[root@localhost ~]# hostname > /opt/test.txt
[root@localhost ~]# cat /opt/test.txt

[root@localhost ~]# ifconfig >> /opt/test.txt
[root@localhost ~]# less /opt/test.txt

[root@localhost ~]# echo 123456 > /opt/5.txt
[root@localhost ~]# cat /opt/5.txt

[root@localhost ~]# echo hello >> /opt/5.txt
[root@localhost ~]# cat /opt/5.txt

[root@localhost ~]# echo hahaixixi >> /opt/5.txt
[root@localhost ~]# cat /opt/5.txt

 管道 | :将前面命令输出,传递给后面命令,作为后面命令的参数

显示/etc/passwd文件内容的8到12内容
]# head -12 /etc/passwd | tail -5
]# head -12 /etc/passwd | tail -5 | cat -n
]# cat -n /etc/passwd | head -12 | tail -5

显示/etc/passwd文件内容的第10行内容
]# head -10 /etc/passwd | tail -1
]# cat -n /etc/passwd | head -10 | tail -1
]# ifconfig | less
]# ifconfig | head -2

四、 find基本使用
根据预设的条件递归查找对应的文件
find [查找目录] [条件]
常用条件表示:
-type 类型(f、d、l)
-name “文档名称”
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 修改时间

 -type 类型(f文件、d目录、l快捷方式)
[root@localhost ~]# find /root -type f #查找是文件
[root@localhost ~]# find /root -type d #查找是目录
[root@localhost ~]# find /boot -type d
[root@localhost ~]# find /etc -type l

 -name “文档名称”
]# find /etc/ -name “passwd”
]# find /etc/ -name “tab"
]# find /etc/ -name "
.conf”

]# mkdir /root/nsd2007
]# touch /root/nsd05.txt
]# touch /root/nsd06.txt

]# find /root -name “nsd*”
]# find /root -name “nsd*” -type d #以nsd开头必须是目录
]# find /root -name “nsd*” -type f #以nsd开头必须是文件

 -size +或-文件大小(k、M、G)
]# find /boot/ -size +300k
]# find /boot/ -size +10M
]# find /boot/ -size -1024M

 -user 用户名(按照数据的所有者用户)
]# useradd student
]# find /home -user student
]# find / -user student #在整个系统中进行查找
/proc:不占用磁盘空间,反映内存的数据

]# find / -user student -type d

 -mtime 按照修改时间 #都是过去时间
-mtime +10 #10天之前的数据
-mtime -10 #最近10天之内的数据

]# find /root/ -mtime -1
]# find /root/ -mtime +1000

五、 find高级使用
 处理查找的内容
– find [范围] [条件] -exec 处理命令 {} ;
– -exec:额外操作的开始,传递参数一次传递一个
– {}:代表find查询的每一个结果
– ; : 额外操作的结束

]# rm -rf /opt/*
]# ls /opt/
]# find /boot/ -size +10M

]# find /boot/ -size +10M -exec cp {} /opt ;
]# ls /opt/

案例3:查找并处理文件
1.利用find查找所有用户 student 拥有的必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中
]# useradd student
]# mkdir /root/findfiles
]# find / -user student -type f

]# find / -user student -type f -exec cp {} /root/findfiles/ ;

]# ls -A /root/findfiles/

六、 vim编辑技巧
三个模式:命令模式、插入模式、末行模式

 命令模式技巧:
[root@localhost ~]# cp /etc/passwd /opt/p.txt
[root@localhost ~]# vim /opt/p.txt
光标跳转
操作类型 按键指令 用 途
移动光标 、、、 上、下、左、右
光标行内跳转 Home 键 或 ^ 或 数字 0 跳转到行首
End 键 或“$”键 跳转到行尾
全文翻页 PgUp 键、PgDn 键 向上翻页、向下翻页
光标行间跳转 1G 或 gg 跳转到文件的首行
G 跳转到文件的末尾行

复制/粘贴/删除
操作类型 按键指令 用 途
复制 yy、#yy 复制光标处的一行、#行
粘贴 p、P 粘贴到光标处之后、之前
删除 x 或 Delete键 删除光标处的单个字符
dd、#dd 删除光标处的一行、#行
d^ 从光标处之前删除至行首
d$或D 从光标处删除到行尾
撤销:按u进行撤销
查找/撤销/保存
操作类型 按键指令 用 途
文本查找 /word 向后查找字符串“word”
n、N 跳至后/前一个结果
撤销编辑 u 撤销最近的一次操作
U 撤销对当前行的所有修改
Ctrl + r 取消前一次撤销操作
保存退出 ZZ 保存修改并退出

 末行模式操作
保存/退出/文件操作
操作类型 设置指令 用 途
存盘及退出 :w 保存当前文件
:q! 放弃已有更改后强制退出
:wq 或 :x 保存已有修改后退出
文件操作 :w /root/newfile 另存为其它文件
:r /etc/filesystems 读入其他文件内容
[root@localhost ~]# echo 123 > /opt/7.txt
[root@localhost ~]# echo abc > /opt/8.txt
[root@localhost ~]# vim /opt/8.txt
末行模式下 :r /opt/7.txt
末行模式下 :r /etc/passwd

字符串替换
操作类型 设置指令 用 途
行内替换 😒/root/new 替换光标所在当前行第一个“root”
😒/root/new/g 替换光标所在当前行所有的“root”
区域内替换 :1,8 s/root/new/g 替换第1-8行所有的“root”
:% s/root/new/g 替换文件内所有的“root”
[root@localhost ~]# cp /etc/passwd /opt/pass.txt
[root@localhost ~]# vim /opt/pass.txt

开关参数的控制
操作类型 设置指令 用 途
编辑器设置 :set nu或set nonu 显示/不显示行号
:set ai或set noai 启用/关闭自动缩进

七、 grep命令补充内容
]# grep root /etc/passwd
]# grep ^root /etc/passwd #必须以root开头的行
]# grep bash$ /etc/passwd #必须以bash结尾的行

^KaTeX parse error: Expected 'EOF', got '#' at position 8: :表示空行 ]#̲ cat /etc/defa… /etc/default/useradd
]# grep -v ^$ /etc/default/useradd #去除空行

在Linux系统中,大多数配置文件内容,以#开头的行为注释行
显示/etc/login.defs配置有效配置(去除注释行,去除空行)
]# grep -v ^# /etc/login.defs
]# grep -v ^# /etc/login.defs | grep -v ^$
]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/12.txt
]# cat /opt/12.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值