1.文件传输命令
1.一般命令 scp
scp file username@ip:/dir/ | 上传(将本机文件传输到远程主机) |
---|---|
scp username@ip:/file dir/ | 下载(将远程主机的文件传输到本机) |
scp -r /dir/ username@ip:/dir/ | 将本机目录里面的文件全部传输到远程主机 |
[kiosk@foundation79 ~]$ touch b
[kiosk@foundation79 ~]$ pwd b
/home/kiosk
[kiosk@foundation79 ~]$ scp /home/kiosk/b root@172.25.254.80:/root/Desktop/
root@172.25.254.80's password:
b 100% 0 0.0KB/s 00:00
##将本机/home/kiosk/b文件上传到ip为172.25.254.80的root用户的目录/root/Desktop/里
[kiosk@foundation79 ~]$ scp root@172.25.254.80:/root/Desktop/a /home/kiosk/bash/
root@172.25.254.80's password:
a 100% 2422 2.4KB/s 00:00
##将ip为172.25.254.80的root用户的文件/root/Desktop/a下载到/home/kiosk/bash/目录里
[kiosk@foundation79 ~]$ mkdir dys
[kiosk@foundation79 ~]$ touch file{1..3}
[kiosk@foundation79 ~]$ mv file{1..3} /home/kiosk/dys/
[kiosk@foundation79 ~]$ scp -r /home/kiosk/dys/ root@172.25.254.80:/root/Desktop/
root@172.25.254.80's password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
##将本机/home/kiosk/dys/目录中的所有文件都上传到ip为172.25.254.80的root用户的目录/root/Desktop/里
2.远程同步命令 rsync
-r | 同步目录 |
---|---|
-g | 同步文件所有组 |
-o | 同步文件所有人 |
-D | 同步设备文件 |
-t | 同步文件时间戳 |
-p | 同步权限 |
-l | 同步软连接 |
-a | 同步所有文件 |
[root@dys ~]# rsync -r /etc root@172.25.254.81:/mnt/ ##同步目录
root@172.25.254.81's password:
skipping non-regular file "etc/favicon.png"
skipping non-regular file "etc/grub.conf"
skipping non-regular file "etc/grub2.cfg"
skipping non-regular file "etc/init.d"
skipping non-regular file "etc/localtime"
skipping non-regular file "etc/mtab"
[root@mxh ~]# ll /mnt/ ##ip为172.25.254.81的主机
total 12
drwxr-xr-x. 133 root root 8192 Oct 15 15:59 etc
[root@dys ~]# rsync -a /root/Desktop/dys/ root@172.25.254.81:/mnt/ ##同步所有文件
root@172.25.254.81's password:
[root@mxh ~]# ll /mnt/ ##ip为172.25.254的主机
total 12
drwxr-xr-x. 133 root root 8192 Oct 15 15:59 etc
lrwxrwxrwx. 1 2000 2000 9 Oct 15 16:16 file -> /dys/file
-rw-r--r--. 1 2000 2000 0 Oct 15 16:16 file1
-rw-r--r--. 1 2000 2000 0 Oct 15 16:16 file2
-rw-r--r--. 1 2000 2000 0 Oct 15 16:16 file3
[root@dys ~]# rsync -ao /root/Desktop/dys/ root@172.25.254.81:/mnt/ ##同步所有文件的所有人
root@172.25.254.81's password:
[root@mxh ~]# ll /mnt/ ##ip为172.25.254的主机
total 12
drwxr-xr-x. 133 root root 8192 Oct 15 15:59 etc
lrwxrwxrwx. 1 student 2000 9 Oct 15 16:16 file -> /dys/file
-rw-r--r--. 1 student 2000 0 Oct 15 16:16 file1
-rw-r--r--. 1 student 2000 0 Oct 15 16:16 file2
-rw-r--r--. 1 student 2000 0 Oct 15 16:16 file3
2. 打包tar
1.打包的概念:
- 将多个文件打、目录打包成为一个文件
- 它并没有压缩的功能,所以文件大小不变
2.目的:为了能够在主机之间传输的更快
3.常用命令
tar -c | 创建打包文件 |
---|---|
-v | 显示打包过程(显示都打包了哪些文件) |
-P | 保留绝对路径(备份数据中含有根目录) |
-x | 解档 |
-C | 指定解包路径(将解包出的文件放在哪个目录里) |
-t | 查看打包文件内容 |
-f | 指定打包名称,(必须加否则会出现乱码) |
-r | 添加文件到打包的文件中 |
- -get filename | 解包指定文件 |
- -delete filename | 删除打包中的指定文件 |
3.压缩
1.压缩的概念:
- 压缩是一种通过特定的算法来减小计算机文件大小的机制
- 尤其对网络用户,它可以减小文件的字节总数,使文件能够通过较慢的互联网连接现实更快传输
- 此外还可以减少文件的磁盘占用空间
- 压缩后与压缩的文件所占用的磁盘空间的大小,就可以被称为是压缩比
2.目的:
- 压缩过的文件容量变小,使硬盘可以容纳更多的数据
- 将打包和压缩结合起来使用,提供用户更方便更强大的压缩与打包功能
3.几种常见的压缩文件扩展名
* . z | compress程序压缩的文件 |
---|---|
* . zip | zip程序压缩的文件 |
* . gz | gz程序压缩的文件 |
* . bz2 | bz2程序压缩的文件 |
* . xz | xz程序压缩的文件 |
* . tar . zip | tar程序打包的文件并经过zip压缩 |
* . tar . gz | tar程序打包的文件并经过gz压缩 |
* . bar . bz2 | tar程序打包的文件并经过bz2压缩 |
* . bar . xz | tar程序打包的文件并经过xz压缩 |
4.常用命令
zip -r etc.tar.zip etc.tar | 通过zip压缩打包文件etc.tar压缩为etc.tar.zip |
---|---|
unzip etc.tar.zip | 通过zip解压文件etc.tar.zip |
gzip etc.tar | 通过gzip压缩打包文件etc.tar |
gunzip etc.tar.gz | 通过gzip解压文件etc.tar.gz |
bzip2 etc.tar | 通过bzip2压缩打包文件etc.tar |
bunzip2 etc.tar.bz2 | 通过bzip2解压文件etc.tar.bz2 |
xz etc.tar | 通过xz压缩打包文件etc.tar |
unxz etc.tar.xz | 通过xz解压文件etc.tar.xz |
tar zcf etc.tar.gz /etc | 将/etc打包并压缩为etc.tar.gz |
tar zxf etc.tar.gz | 解包并解压为/etc |
tar jcf etc.tar.bz2 /etc | 将/etc打包并压缩为etc.tar.bz2 |
tar jxf etc.tar.bz2 | 解包并解压为/etc |
tar Jcf etc.tar.xz /etc | 将/etc打包并压缩为etc.tar.xz |
tar Jxf etc.tar.xz | 解包并解压为/etc |
注意:
- 通过zip压缩打包文件时,打包文件存在
- 通过gzip、bzip2、xz压缩打包文件时,打包文件不存在
- 当打包、压缩为一条命令时,解包并解压完成后压缩文件存在
- 打包、压缩命令合为一条时,压缩文件格式只能为 .tar.gz、.tar.bz2、.tar.xz,不能为 .tar.zip