1.首先安装sz,rz命令
[root@localhost lianxi]# yum install lrzsz -y
[root@localhost lianxi]# ls
passwd.gz
[root@localhost lianxi]# sz passwd.gz 发送文件到windows --》将linux系统里的文件传输到windows系统
[root@localhost ~]# rz 接受从windows系统里传输文件到linux received
2.xftp
tar 压缩和解压缩命令
- 创建压缩包:
选项:
-c 创建一个压缩文件 create
-f 归档文件
-z 调用gzip压缩命令去压缩 --》推荐
-j 调用bz2命令去压缩
-J 调用xz命令去压缩 -->耗时比较长,压缩效果好
tar -czf 压缩文件名.tar.gz 源文件
tar -cjf 压缩文件名.tar.bz2 源文件
tar -cJf 压缩文件名.tar.xz 源文件
[root@localhost ~]# cd /lianxi
[root@localhost lianxi]# ls
ldj-frond.rar ntfs.png passwd.gz
[root@localhost lianxi]# cp /boot . -r
[root@localhost lianxi]# ls
boot ldj-frond.rar ntfs.png passwd.gz
[root@localhost lianxi]# tar -czf boot.tar.gz boot 将当前目录下的boot文件夹打包压缩成boot.tar.gz文件
创建压缩文件的时候,备份到指定路径,使用绝对路径
[root@localhost machi]# tar czf /machi/passwd.tar.gz /etc/passwd
选项:
include 包含
--exclude 排斥出去
[root@localhost boot]# tar --exclude=/boot/{grub,grub2,efi} -czf /machi/no_grub.boot.tar.gz /boot
tar: 从成员名中删除开头的“/”
[root@localhost boot]# cd /machi/
[root@localhost machi]# ls
boot no_grub.boot.tar.gz passwd.tar.gz
2. 查看压缩包:
选项:
-t list
[root@localhost lianxi]# tar tf hosts.tar.gz
etc/hosts
[root@localhost lianxi]# tar -tf hosts.tar.gz
etc/hosts
3. 解压压缩包
[root@localhost lianxi]#tar xf *.tar.gz *.tar.xz
选项:
-C 将压缩文件解压到指定的目录
[root@localhost gaohui]# tar xf boot.tar.gz -C /machi
[root@localhost gaohui]# ls /machi/
boot
3. zip unzip命令
unzip 命令可以查看和解压缩 zip 文件
zip 命令用于压缩文件
安装:yum install unzip zip -y
[root@localhost dist]# unzip dist.zip 解压
使用 -d 选项手动指定解压缩位置
[root@localhost ~]# unzip -d /tmp/ ana.zip
Archive: ana.zip
inflating: /tmp/anaconda-ks.cfg
#把压缩包解压到指定位置
zip -q -r html.zip /home/html 将 /home/html/ 这个目录下所有文件和文件夹打包为当前目录下的 html.zip
-q 不显示指令执行过程。
-r 递归处理,将指定目录下的所有文件和子目录一并处理。
zip -dv cp.zip a.c 从压缩文件 cp.zip 中删除文件 a.c
-d 从压缩文件内删除指定的文件。
-v 显示指令执行过程或显示版本信息。
练习:
1.创建一个目录在/lianxi下叫tar
mkdir /lianxi/tar
2.进入tar目录,复制/etc/hosts文件和/etc/passwd到tar目录下
[root@localhost tar]# cp /etc/hosts .
[root@localhost tar]# cp /etc/passwd .
[root@localhost tar]# cp /etc/{hosts,passwd} .
3.复制/boot目录到tar目录下
cp /boot . -r
4.将当前目录下的boot目录打包放在/lianxi/tar目录下叫boot.tar.gz
tar czvf boot.tar.gz boot
5.查看boot.tar.gz文件里的内容
tar tf boot.tar.gz
6.将hosts文件和passwd文件打包放到host_passwd.tar.bz2文件
tar cjf host_passwd_boot.tar.bz2 hosts passwd /boot
7.查看host_passwd.tar.bz2文件里的内容
tar tf host_passwd.tar.bz2
8.将/boot/目录和/etc/passwd、/var/log目录备份到/lianxi目录下叫boot_pw_log.tar.xz
tar cJf /lianxi/boot_pw_log.tar.xz /boot /etc/passwd /var/log
9.新建/bak目录,然后将/lianxi目录下的boot_pw_log.tar.xz解压到/bak目录下
mkdir /bak
tar xf /lianxi/boot_pw_log.tar.xz -C /bak
10.将/boot目录下的除grub目录以外的所有文件都备份到/bak目录下叫no-grub.tar.gz
tar --exclude=/boot/grub* -czf /bak/no-grub.tar.gz /boot
tar tf no-grub.tar.gz
拓展练习:
编写一个脚本backup_log.sh实现备份/var/log目录下的所有文件到/scbackup目录下,要求文件名是包含当天日期,精确到秒,文件名例如:20220412200401-log.tar.gz。同时要求删除七天前的备份文件,只保留最近7天的文件。
#!/bin/bash
#获得当前的时间
ctime=$(date +%Y%m%d%H%M%S)
#新建备份目录
mkdir -p /scbackup
#备份/var/log目录到/scbackup下
tar czf /scbackup/${ctime}-log.tar.gz /var/log
#删除7天前的备份文件
find /scbackup -mtime +7 -type f -name "*log.tar.gz" -exec rm -rf {} \;
[root@localhost lianxi]# bash backup_log.sh 执行脚本
tar: 从成员名中删除开头的“/”
[root@localhost lianxi]# ls /scbackup/
20221030172026-log.tar.gz
[root@localhost lianxi]# date -s "2022-10-31 17:20:00" 设置时间
2022年 10月 31日 星期一 17:20:00 CST
[root@localhost lianxi]# date
2022年 10月 31日 星期一 17:20:05 CST
[root@localhost lianxi]# bash backup_log.sh 执行脚本
tar: 从成员名中删除开头的“/”
[root@localhost lianxi]# ls /scbackup/
20221030172026-log.tar.gz 20221031172013-log.tar.gz
[root@localhost lianxi]# date -s "2022-11-13 17:20:00" 设置时间
2022年 11月 13日 星期日 17:20:00 CST
[root@localhost lianxi]# bash backup_log.sh
tar: 从成员名中删除开头的“/”
[root@localhost lianxi]# ls /scbackup/
20221113172009-log.tar.gz