文件传输
不同系统之间的文件传输
####1.文件归档####
1.
文件归档,就是把多个文件变成一个归档文件
2.
tar c ##创建
f ##指定归档文件名称
t ##显示归档文件中的内容
r ##向归档文件中添加文件
--get ##取出单个文件
--delete ##删除单个文件
x ##取出归档文件中的所有内容
-C ##指定解档目录
-z ##gz格式压缩
-j ##bz2格式压缩
-J ##xz格式压缩
####2.压缩####
gz
gzip etc.tar ##压缩成gz格式
gunzip etc.tar.gz ##解压gz格式压缩包
tar zcf etc.tar.gz /etc ##把文件归档为tar并压缩成gz
tar zxf etc.tar.gz ##解压并解档gz格式压缩包
bz2
bzip2 etc.tar ##压缩成bz2格式
bunzip2 etc.tar.bz2 ##解压bz2格式压缩包
tar jcf etc.tar.bz2 /etc ##把文件归档为tar并压缩成bz2
tar jxf etc.tar.bz2 ##解压并解档bz2格式压缩包
xz
xz etc.tar ##压缩成xz格式
unxz etc.tar.xz ##解压xz格式压缩包
tar Jcf etc.tar.xz /etc ##把文件归档为tar并压缩成zx
tar Jxf etc.tar.xz ##解压并解档xz格式压缩包
zip
zip -r etc.tar.zip etc.tar ##压缩成zip格式
unzip etc.tar.zip ##解压zip格式压缩包
####3.系统中的文件传输####
scp file [email protected]:/dir ##上传
scp [email protected]:/dir/file /dir ##下载
(本机ip为172.25.254.113)scp file1 [email protected]:/root/Desktop
把自己的文件file1以超级用户的身份传给ip为172.25.254.213的用户在其/root/Desktop下存放
scp [email protected]:/root/Desktop/file2 . 把ip为172.25.254.213用户的/root/Desktop/file2文件复制到当前
scp文件的传输相对来说比较慢此时我们有一种比较快的同步传输方式rsync
rsync [参数] file [email protected]:/dir 把自己自己机子的文件或目录同步给ip机子上的某个目录下但此时只会同步文件不会同步文件权限、所有人、所有组、时间等信息)
rsync -r ##同步目录-l ##不忽略链接
-p ##不忽略文件权限
-t ##不忽文件时间戳
-g ##不忽文件所有组
-o ##不忽文件所有人
-D ##不忽略设备文件
(服务端)[[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
[[email protected] mnt]# touch file{1..5}
[[email protected] mnt]# ls
file1 file2 file3 file4 file5
[[email protected] mnt]# ls -l
total 0
-rw-r--r--. 1 root root 0 Apr 6 03:10 file1
-rw-r--r--. 1 root root 0 Apr 6 03:10 file2
-rw-r--r--. 1 root root 0 Apr 6 03:10 file3
-rw-r--r--. 1 root root 0 Apr 6 03:10 file4
-rw-r--r--. 1 root root 0 Apr 6 03:10 file5
[[email protected] mnt]# chmod 777 *
[[email protected] mnt]# chown student.student *
[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file5
[[email protected] mnt]# rsync -r /mnt/ [email protected]:/mnt 将/mnt/下的内容同步到172.25.254.113中(但此时只会同步文件不会同步文件权限、所有人、所有组、时间等信息)
[email protected]'s password:
[[email protected] mnt]# mkdir test
[[email protected] mnt]# touch test/file8
[[email protected] mnt]# ls
file1 file2 file3 file4 file5 test
[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file5
drwxr-xr-x. 2 root root 18 Apr 6 03:13 test
[[email protected] mnt]# rsync -r /mnt/test [email protected]:/mnt
[email protected]'s password: 同步/mnt/test目录在172.25.254.113/mnt中只会出现test目录
[[email protected] mnt]# rsync -rp /mnt/ [email protected]:/mnt 不忽略文件权限
[email protected]'s password:
[[email protected] mnt]# rsync -rpg /mnt/ [email protected]:/mnt 不忽略文件权限以及组
[email protected]'s password:
[[email protected] mnt]# rsync -rpgo /mnt/ [email protected]:/mnt 不忽略文件权限,所有组,所有人
[email protected]'s password:
[[email protected] mnt]# rsync -rpgot /mnt/ [email protected]:/mnt 不忽略文件权限,所有组,所有人时间戳
[email protected]'s password:
(客户端)[[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
[[email protected] mnt]# ls
file1 file2 file3 file4 file5
[[email protected] mnt]# ll 此时只同步了文件
total 0
-rwxr-xr-x 1 root root 0 Apr 6 03:12 file1
-rwxr-xr-x 1 root root 0 Apr 6 03:12 file2
-rwxr-xr-x 1 root root 0 Apr 6 03:12 file3
-rwxr-xr-x 1 root root 0 Apr 6 03:12 file4
-rwxr-xr-x 1 root root 0 Apr 6 03:12 file5
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ls
test
[[email protected] mnt]# ll
total 0
drwxr-xr-x 2 root root 18 Apr 6 03:14 test
[[email protected] mnt]# ls /mnt/test
file8
[[email protected] mnt]# ls -ld test
drwxr-xr-x 2 root root 18 Apr 6 03:14 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll
total 0
drwxr-xr-x 2 root root 18 Apr 6 03:17 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll
total 0
[[email protected] mnt]# ll 此时同步了文件权限
total 0
-rwxrwxrwx 1 root root 0 Apr 6 03:18 file1
-rwxrwxrwx 1 root root 0 Apr 6 03:18 file2
-rwxrwxrwx 1 root root 0 Apr 6 03:18 file3
-rwxrwxrwx 1 root root 0 Apr 6 03:18 file4
-rwxrwxrwx 1 root root 0 Apr 6 03:18 file5
drwxr-xr-x 2 root root 18 Apr 6 03:18 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll 此时同步了所有组
total 0
-rwxrwxrwx 1 root student 0 Apr 6 03:18 file1
-rwxrwxrwx 1 root student 0 Apr 6 03:18 file2
-rwxrwxrwx 1 root student 0 Apr 6 03:18 file3
-rwxrwxrwx 1 root student 0 Apr 6 03:18 file4
-rwxrwxrwx 1 root student 0 Apr 6 03:18 file5
drwxr-xr-x 2 root root 18 Apr 6 03:18 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll 此时同步了所有人
total 0
-rwxrwxrwx 1 student student 0 Apr 6 03:19 file1
-rwxrwxrwx 1 student student 0 Apr 6 03:19 file2
-rwxrwxrwx 1 student student 0 Apr 6 03:19 file3
-rwxrwxrwx 1 student student 0 Apr 6 03:19 file4
-rwxrwxrwx 1 student student 0 Apr 6 03:19 file5
drwxr-xr-x 2 root root 18 Apr 6 03:19 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll 此时同步了时间戳
total 0
-rwxrwxrwx 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx 1 student student 0 Apr 6 03:10 file5
drwxr-xr-x 2 root root 18 Apr 6 03:13 test
[[email protected] mnt]# rm -fr *
(服务端)[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file5
drwxr-xr-x. 2 root root 18 Apr 6 03:13 test
[[email protected] mnt]# ln -s /mnt/file1 /mnt/westos
[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file5
drwxr-xr-x. 2 root root 18 Apr 6 03:13 test
lrwxrwxrwx. 1 root root 10 Apr 6 03:40 westos -> /mnt/file1
[[email protected] mnt]# rsync -r /mnt [email protected]:/mnt
[email protected]'s password:
skipping non-regular file "mnt/westos"
[[email protected] mnt]# rsync -rl /mnt [email protected]:/mnt 链接
[email protected]'s password:
[[email protected] mnt]# ll /dev/pts
total 0
crw--w----. 1 root tty 136, 0 Apr 6 02:50 0
crw--w----. 1 root tty 136, 1 Apr 6 03:44 1
c---------. 1 root root 5, 2 Apr 5 22:06 ptmx
[[email protected] mnt]# rsync -r /dev/pts [email protected]:/mnt
[email protected]'s password:
skipping non-regular file "pts/0"
skipping non-regular file "pts/1"
skipping non-regular file "pts/ptmx"
[[email protected] mnt]# rsync -rD /dev/pts [email protected]:/mnt 设备
[email protected]'s password:
[[email protected] mnt]# ll
total 0
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file1
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file2
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file3
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file4
-rwxrwxrwx. 1 student student 0 Apr 6 03:10 file5
drwxr-xr-x. 2 root root 18 Apr 6 03:13 test
lrwxrwxrwx. 1 root root 10 Apr 6 03:40 westos -> /mnt/file1
[[email protected] mnt]# rm -fr *
(客户端)[[email protected] mnt]# ll
total 0
drwxr-xr-x 3 root root 77 Apr 6 03:42 mnt
[[email protected] mnt]# ll mnt
total 0
-rwxr-xr-x 1 root root 0 Apr 6 03:42 file1
-rwxr-xr-x 1 root root 0 Apr 6 03:42 file2
-rwxr-xr-x 1 root root 0 Apr 6 03:42 file3
-rwxr-xr-x 1 root root 0 Apr 6 03:42 file4
-rwxr-xr-x 1 root root 0 Apr 6 03:42 file5
drwxr-xr-x 2 root root 18 Apr 6 03:42 test
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll
total 0
[[email protected] mnt]# ll /mnt
total 0
drwxr-xr-x 3 root root 90 Apr 6 03:43 mnt
[[email protected] mnt]# ll mnt
total 0
-rwxr-xr-x 1 root root 0 Apr 6 03:43 file1
-rwxr-xr-x 1 root root 0 Apr 6 03:43 file2
-rwxr-xr-x 1 root root 0 Apr 6 03:43 file3
-rwxr-xr-x 1 root root 0 Apr 6 03:43 file4
-rwxr-xr-x 1 root root 0 Apr 6 03:43 file5
drwxr-xr-x 2 root root 18 Apr 6 03:43 test
lrwxrwxrwx 1 root root 10 Apr 6 03:43 westos -> /mnt/file1
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll
total 0
[[email protected] mnt]# ll
total 0
drwxr-xr-x 2 root root 6 Apr 6 03:45 pts
[[email protected] mnt]# ll pts
total 0
[[email protected] mnt]# rm -fr *
[[email protected] mnt]# ll
total 0
[[email protected] mnt]# ll pts
total 0
crw------- 1 root root 136, 0 Apr 6 03:46 0
crw------- 1 root root 136, 1 Apr 6 03:46 1
c--------- 1 root root 5, 2 Apr 6 03:46 ptmx