制作tar包(tar打包)
格式:tar 选项 /路径/压缩包名字 /源数据.......
-c:动作为创建
-f:指定压缩包名字(必须在所有选项最后)
-z、-j、-J:调用 .gz、.bz2、.xz 格式工具进行处理
-
root@localhost /\]# tar -zcf /root/cbd.tar.gz /etc/passwd /home
-
root@localhost /\]# tar -jcf /root/haha.tar.bz2 /etc/passwd /home
-
root@localhost /\]# tar -Jcf /root/cctv.tar.xz /etc/passwd /home
释放tar包(tar解包)
格式:tar 选项 /路径/压缩包名字 选项 /释放的位置
-x:释放归档
-f:指定归档文件名称,必须在所有选项的最后
-C(大写):指定路径
-
root@localhost /\]# tar -tf /root/haha.tar.bz2 #查看tar包内容
-
root@localhost /\]# tar -xf /root/haha.tar.bz2 -C /nsd11
-
root@localhost /\]# ls /nsd11/etc
-
root@localhost /\]# tar -tf /root/cbd.tar.gz #查看tar包内容
-
root@localhost /\]# tar -xf /root/cbd.tar.gz -C /nsd12
-
root@localhost /\]# ls /nsd12/etc
利用-C指定路径
-
root@localhost /\]# tar -zcf /root/yy.tar.gz -C /etc/ passwd -C /usr games
-
root@localhost /\]# mkdir /nsd12
-
root@localhost /\]# ls /nsd12
-
root@localhost /\]# tar -tf /root/zz.tar.gz
5.1 问题
本例要求使用tar工具完成以下备份任务:
- 创建一个名为/root/backup.tar.bz2的归档文件
- 其中包含/usr/local目录中的内容
- tar归档必须使用bzip2进行压缩步骤
实现此案例需要按照如下步骤进行。
步骤一:创建备份文件
使用tar命令制作归档备份,结合-j选项调用bzip2压缩工具,保留路径:
-
root@server0 \~\]# tar -jcf /root/backup.tar.bz2 /usr/local/
-
root@server0 \~\]# ls -lh /root/backup.tar.bz2 //确认文件
-
root@server0 \~\]# tar -tf /root/backup.tar.bz2 //确认内容
-
usr/local/bin/
-
usr/local/bin/lab
-
usr/local/etc/
-
usr/local/games/
重定向(重新定向命令的输出)
将前面命令的输出,作为内容,写入到后面的文件
>:覆盖重定向
>>:追加重定向
-
root@A /\]# head -5 /etc/passwd \> /opt/p.txt
-
root@A /\]# head -2 /etc/passwd \> /opt/p.txt
-
root@A /\]# hostname
-
root@A /\]# cat /opt/p.txt
-
root@localhost /\]# echo 123456
-
root@localhost /\]# cat /opt/p.txt
-
root@localhost /\]# echo hello world \>\> /opt/p.txt
-
root@localhost /\]# cat /etc/hostname
-
root@localhost /\]# cat /etc/hostname
-
root@localhost /\]# cat /opt/p.txt
-
root@localhost /\]# cat /opt/p.txt
作用:将前面命令的输出,传递给后面命令,作为后面命令的参数
-
root@localhost /\]# head -4 /etc/passwd \| tail -1
-
root@localhost /\]# cat -n /etc/passwd \| head -8 \| tail -1
显示8~12行内容
-
root@localhost /\]# head -12 /etc/passwd \| tail -5
-
root@localhost /\]# cat -n /etc/passwd \| head -12 \| tail -5
-
root@localhost /\]# cat /opt/pa.txt
-
作用:从文本文件内容中,过滤关键字符串
-
root@localhost /\]# grep root /etc/passwd
-
root@localhost /\]# grep \^root /etc/passwd #以root开头
-
^$:表示空行,专门与-v选项连用,过滤不要空行
-
root@localhost /\]# cat /etc/default/useradd
Linux中大多数配置文件内容,以#开头的行为注释行
显示配置文件有效信息(去除以#开头的注释行和去除空行)
-
root@localhost /\]# grep -v \^# /etc/login.defs
-
root@localhost /\]# grep -v \^# /etc/login.defs \| grep -v \^$ \> /opt/log.txt
过滤命令的输出
-
root@localhost /\]# ifconfig \| grep inet
-
root@localhost /\]# ifconfig \| less #方便查看
8.1 问题
- 显示ifconfig命令的前两行内容
- 显示/etc/passwd第九行内容
- 将hostname命令的输出内容,覆盖写入到/opt/hn.txt
- 利用echo命令,将"tmooc"内容追加写入到/opt/hn.txt
8.2 步骤
实现此案例需要按照如下步骤进行。
1)显示ifconfig命令的前两行内容
-
root@server0 \~\]# ifconfig \| head -2
-
root@server0 \~\]# head -9 /etc/passwd \| tail -1
-
root@server0 \~\]# hostname \> /opt/hn.txt
-
root@server0 \~\]# echo tmooc \>\> /opt/hn.txt
格式:find [目录] [条件1]
-type 类型(f文本文件、d目录、l快捷方式)
-
root@A /\]# find /boot -type d
-
root@A /\]# find /etc -type l
-
root@A /\]# find /usr -type d
-
root@A /\]# find /sbin -type l
-name "文档名称" (-iname 忽略大小写)
-
root@localhost /\]# find /etc -name "passwd"
-
root@localhost /\]# find /etc -name "\*.conf"
-
root@localhost /\]# find /boot -type d \| cat -n
-
root@localhost /\]# find /etc -type l \| cat -n
-
root@localhost /\]# find /boot -type f \| wc -l
-
root@localhost /\]# find /etc -name "\*.conf" \| wc -l
两个条件联合使用
-
root@localhost /\]# mkdir /mnt/cbd01
-
root@localhost /\]# touch /mnt/cbd03.txt
-
root@localhost /\]# find /mnt/ -name "cbd\*" -type d
-
root@localhost /\]# find /mnt/ -name "cbd\*" -o -type f #两个满足其中一个
-
root@localhost /\]# find /boot -size +300k
-
root@localhost /\]# find /boot -size +1M
-user 用户名 (按照数据的所有者)
-
root@A /\]# useradd natasha #创建用户
-
root@A /\]# find / -user natasha
-
root@A /\]# useradd harry #创建用户
-
root@A /\]# find / -user harry
-mtime +90 #90天之前的数据
-mtime -90 #最近90天之内的数据
- 三个月之前的数据:
-
root@A /\]# find /var -mtime +90
-
root@A /\]# find /root -mtime -10
处理find找到的数据,每查找的一个就传递一次
find [范围] [条件] -exec 处理命令 {} \;
-exec额外操作的开始
{} 永远表示前面find查找的结果
\; 额外操作的结束
-
root@localhost /\]# find /boot -size +10M
-
root@localhost /\]# ls /mnt
两个条件联合使用
-
root@localhost /\]# mkdir /root/mytab
-
root@localhost /\]# find /etc -name "\*tab" -type f
-
root@localhost /\]# ls /root/mytab
-
利用find查找,数据的所有者为 student,并且必须是文件,把它们拷贝到 /root/findfiles/ 文件夹中
-
root@localhost /\]# useradd student #创建普通用户student
-
root@localhost /\]# find / -user student -type f
-
root@localhost /\]# ls -A /root/findfiles/ #-A显示隐藏数据
11.1 问题
- 利用find查找所有用户student拥有的必须是文件,把它们拷贝到 /root/findfiles/文件夹中
- 利用find查找/boot目录下大于10M并且必须是文件,拷贝到/opt
- 利用find查找/boot/ 目录下以 vm 开头且必须是文件,拷贝到/opt
- 利用find查找/boot/目录下为快捷方式
- 利用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 \~\]# find / -user student -type f
-
root@server0 \~\]# ls /root/findfiles
-
root@server0 \~\]# find /boot -size +10M
-
root@server0 \~\]# ls /opt
-
root@server0 \~\]# find /boot -name "vm\*"
-
root@server0 \~\]# ls /opt
-
root@server0 \~\]# find /boot -type l
-
root@server0 \~\]# find /etc -name "\*tab" -type f
读取文件内容
-
root@localhost /\]# echo 123456 \> /opt/aa.txt
-
root@localhost /\]# vim /opt/cc.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
开关参数的控制
:set nu或nonu 显示/不显示行号
:set ai或noai 启用/关闭自动缩进
-
root@localhost /\]# vim /opt/h.txt
永久开关功能设置
-
root@nb \~\]# vim /root/.vimrc
-
root@nb \~\]# vim /etc/passwd #测试是否开启行号
vimdiff同时比对或修改多个文件
-
root@a \~\]# echo hahaxixi \> /opt/aa.txt
-
root@a \~\]# vimdiff /opt/aa.txt /opt/bb.txt
- 末行模式wqa保存全部文件并退出
14 案例:vim效率操作
14.1 问题
本例要求掌握使用vim文本编辑器时能够提高操作效率的一些常用技巧和方法,完成下列任务:
- 将文件 /etc/passwd 复制为 /opt/nsd.txt,然后打开 /opt/nsd.txt 文件,练习命令模式下的切换/复制/删除/查找操作
- 将文件 /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:
-
root@svr7 \~\]# cp /etc/passwd /opt/nsd.txt
-
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/ 目录下:
-
root@svr7 \~\]# cp /etc/man_db.conf /opt/
-
root@svr7 \~\]# vim /opt/man_db.conf
输入 :set nu ,确认后显示行号。
输入 :50,100 s/man/MAN/g ,确认将第50~100行内的"man"替换为"MAN"。
4)保存并退出编辑器
输入 :wq ,确认后保存并退出编辑器。