1、压缩 gzip
[root@stone file]# ls
f1  f2  f3  file1
[root@stone file]# gzip f1
[root@stone file]# ls
f1.gz  f2  f3  file1
#压缩后源文件被删除,只能出来单个文件
[root@stone file]# gunzip f1.gz 
[root@stone file]# ls
f1  f2  f3  file1
#解压缩后压缩文件被删除
[root@stone file]# cat f1 | gzip -c > f1.gz
[root@stone file]# ls
f1  f1.gz  f2  f3  file1
#从标准输入读取文件,使用-c选项并重定向输出,可以保留源文件
[root@stone file]# gzip f1
[root@stone file]# gzip -l f1.gz 
         compressed        uncompressed  ratio uncompressed_name
                108               10240  99.2% f1
#-l选项列出压缩文件属性信息

[root@stone file]# echo aaa > f1
[root@stone file]# cat f1
aaa
[root@stone file]# gzip f1
[root@stone file]# ls
f1.gz  f2  f3  file1
[root@stone file]# zcat f1.gz 
aaa
#zcat直接读取gzip格式文件内容

2、压缩 bzip2
[root@stone file]# bzip2 f1
[root@stone file]# ls
f1.bz2  f2  f3  file1
#压缩后源文件被删除,只能处理单个文件
[root@stone file]# bunzip2 f1.bz2 
[root@stone file]# ls
f1  f2  f3  file1
#解压缩后压缩文件被删除
[root@stone file]# cat f1 | bzip2 -c > f1.bz2
[root@stone file]# ls
f1  f1.bz2  f2  f3  file1
#从标准输入读取文件,使用-c选项并重定向输出,可以保留源文件

[root@stone file]# bzip2 f2 -k
[root@stone file]# ls
f1  f1.bz2  f2  f2.bz2  f3  file1
[root@stone file]# rm f2
rm: remove regular empty file `f2'? y
[root@stone file]# bunzip2 f2.bz2 -k
[root@stone file]# ls
f1  f1.bz2  f2  f2.bz2  f3  file1
#-k选项可以保留源文件及压缩文件

3、归档 tar
[root@stone file]# ls
f1  f2  f3  f4  f5  f6  f7  f8  f9
[root@stone file]# tar -cf f.tar f1 f2
[root@stone file]# ls
f1  f2  f3  f4  f5  f6  f7  f8  f9  f.tar
#创建归档后源文件还存在
#-c选项表示创建归档文件
#-f指定归档文件名,后面接归档文件

[root@stone file]# tar -tf f.tar 
f1
f2
#-t选项查看归档文件内容
[root@stone file]# tar -tvf f.tar 
-rw-r--r-- root/root         0 2012-12-20 00:00:00 f1
-rw-r--r-- root/root         0 2013-05-14 17:44:25 f2
#-v选项列出详细信息

[root@stone file]# tar -rvf f.tar f3
f3
[root@stone file]# tar -tf f.tar 
f1
f2
f3
#-r选项表示向归档文件中添加文件

[root@stone file]# mkdir file1
[root@stone file]# ls
f1  f2  f3  f4  f5  f6  f7  f8  f9  file1  f.tar
[root@stone file]# cd file1/
[root@stone file1]# tar -xvf /root/file/f.tar f3
f3
[root@stone file1]# ls
f3
[root@stone file1]# tar -tf /root/file/f.tar 
f1
f2
f3
#-x选项表示提取归档中的文件,归档文件保持不变

[root@stone file]# tar -xvf f.tar -C file1   
f1
f2
f9
[root@stone file]# ls file1/
f1  f2  f9
#-C指定文件释放路径,-C前面的参数必须是归档文件
[root@stone file]# tar -xvf f.tar -C file1/ f9
f9
[root@stone file]# ls file1/
f9
#提取归档文件中的某个文件到指定路径

[root@stone file]# tar -cf - f7 f8 f9 | tar -xvf - -C file1/
f7
f8
f9
[root@stone file]# ls file1/
f7  f8  f9
#通过stdin和stdout传送文件

[root@stone file]# tar -tf f.tar 
f1
f2
f9
[root@stone file]# tar -cf f1.tar f7 f8 f9
[root@stone file]# tar -tf f1.tar 
f7
f8
f9
[root@stone file]# tar -Af f.tar f1.tar 
[root@stone file]# tar -tf f.tar 
f1
f2
f9
f7
f8
f9
#-A选项将f1.tar的内容合并到f.tar中,且包含文件名为f9的两个同名文件

[root@stone file]# tar -tf f.tar 
f1
f2
f9
f9
[root@stone file]# tar -f f.tar --delete f9
[root@stone file]# tar -tf f.tar 
f1
f2
#--delete从归档文件中删除文件

[root@stone file]# tar -rf f.tar f9
[root@stone file]# tar -tf f.tar 
f1
f2
f9
[root@stone file]# tar -uvf f.tar f9
[root@stone file]# tar -tf f.tar 
f1
f2
f9
#-u选项通过检查加入文件的时间戳来更新归档文件,只有比归档文件中的同名文件更新的时候才进行添加。
[root@stone file]# touch f9
[root@stone file]# tar -uvvf f.tar f9
-rw-r--r-- root/root         0 2013-05-21 10:44:11 f9
[root@stone file]# tar -tf f.tar 
f1
f2
f9
f9

[root@stone file]# tar -df f.tar f9
f9: Mod time differs
#-d选项比较归档文件与文件系统中文件的差别

[root@stone file]# tar -cvzf f2.tar.gz f4 f5
f4
f5
[root@stone file]# ls
f1  f1.tar  f2  f2.tar.gz  f3  f4  f5  f6  f7  f8  f9  file1  f.tar
[root@stone file]# tar -tf f2.tar.gz 
f4
f5
#-z选项表示使用gzip格式压缩归档文件
[root@stone file]# tar -cvjf f2.tar.bz2 f4 f5
f4
f5
[root@stone file]# ls
f1  f1.tar  f2  f2.tar.bz2  f2.tar.gz  f3  f4  f5  f6  f7  f8  f9  file1  f.tar
[root@stone file]# tar -tf f2.tar.gz 
f4
f5
#-j选项表示使用bzip2格式压缩归档文件

[root@stone file]# ls
f1  f2  f3  f4  f5  f6  f7  f8  f9  file1
[root@stone file]# tar -cf f.tar * --exclude file1
[root@stone file]# tar -tf f.tar 
f1
f2
f3
f4
f5
f6
f7
f8
f9
#--exclude表示建立归档文件时排除后面的文件
[root@stone file]# ls
f1  f2  f3  file1
[root@stone file]# ls file1/
[root@stone file]# ls f[0-9] > /root/list
[root@stone file]# cat /root/list
f1
f2
f3
[root@stone file]# tar -cf file.tar * -X /root/list
[root@stone file]# tar -tf file.tar 
file1/
#-X表示建立归档文件时排除后面文件中列出的文件

[root@stone file]# tar -cvf f.tar f1 f2 f3 --totals
f1
f2
f3
Total bytes written: 20480 (20KiB, 9.6MiB/s)
#--totals打印总字节数

4、压缩 zip
[root@stone file]# ls
f1  f2  f3  file1
[root@stone file]# zip f1.zip f1
  adding: f1 (stored 0%)
[root@stone file]# ls
f1  f1.zip  f2  f3  file1
#压缩后源文件不会被删除

[root@stone file]# zip -r file.zip file1 f2 f3
  adding: file1/ (stored 0%)
  adding: f2 (stored 0%)
  adding: f3 (stored 0%)
#-r选项表示对文件和目录进行递归操作,可一次操作多个文件

[root@stone file]# ls
f1  f1.zip  f2  f3  file1  file.zip
[root@stone file]# unzip f1.zip 
Archive:  f1.zip
replace f1? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: f1                      
[root@stone file]# ls
f1  f1.zip  f2  f3  file1  file.zip
#解压缩后压缩文件不会被删除

[root@stone file]# zip f1.zip -u f2
  adding: f2 (stored 0%)
#-u选项为更新压缩文件中的内容
[root@stone file]# unzip -l f1.zip 
Archive:  f1.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        4  05-21-13 11:33   f1
        0  05-14-13 17:44   f2
 --------                   -------
        4                   2 files
#unzip -l列出压缩文件中的文件

[root@stone file]# zip -d f1.zip f2
deleting: f2
[root@stone file]# unzip -l f1.zip 
Archive:  f1.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
        4  05-21-13 11:33   f1
 --------                   -------
        4                   1 file
#-d选项从压缩文件中删除指定文件

5、文件备份 rsync
[root@stone file]# touch file1/f{1,2}
[root@stone file]# touch file2/f{3,4}
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f3
    `-- f4

2 directories, 4 files
[root@stone file]# rsync -av /root/file/file1 /root/file/file2
building file list ... done
file1/
file1/f1
file1/f2

sent 160 bytes  received 70 bytes  460.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f3
    |-- f4
    `-- file1
        |-- f1
        `-- f2

3 directories, 6 files
#-a表示进行归档
#-v表示列出详细信息
#源地址末尾不使用/,表示连目录一起备份

[root@stone file]# rm -r file2/f*[1-2]
rm: descend into directory `file2/file1'? y
rm: remove regular empty file `file2/file1/f1'? y
rm: remove regular empty file `file2/file1/f2'? y
rm: remove directory `file2/file1'? y
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f3
    `-- f4

2 directories, 4 files
[root@stone file]# rsync -av /root/file/file1/ /root/file/file2
building file list ... done
./
f1
f2

sent 154 bytes  received 70 bytes  448.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f1
    |-- f2
    |-- f3
    `-- f4

2 directories, 6 files
#源地址末尾使用/,仅备份该目录下的内容

[root@stone file]# rm -r file2/f[3,4]
rm: remove regular empty file `file2/f3'? y
rm: remove regular empty file `file2/f4'? y
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f1
    `-- f2

2 directories, 4 files
[root@stone file]# rsync -av /root/file/file1 /root/file/file2/
building file list ... done
file1/
file1/f1
file1/f2

sent 160 bytes  received 70 bytes  460.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f1
    |-- f2
    `-- file1
        |-- f1
        `-- f2

3 directories, 6 files
#目前地址末尾使用/,与不使用/效果一样
#建议统一都不使用/

[root@stone file]# rsync -av /root/file/file1 root@172.16.3.62:/root/file1
The authenticity of host '172.16.3.62 (172.16.3.62)' can't be established.
RSA key fingerprint is 64:b5:96:89:03:8e:4a:79:62:d7:0b:84:f4:54:64:d7.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '172.16.3.62' (RSA) to the list of known hosts.
root@172.16.3.62's password: 
building file list ... done
file1/
file1/f1
file1/f2

sent 160 bytes  received 70 bytes  21.90 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# ssh root@172.16.3.62 tree /root/file1
root@172.16.3.62's password: 
/root/file1
`-- file1
    |-- f1
    `-- f2

1 directory, 2 files
#远程备份

[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f1
    |-- f2
    |-- f3
    `-- f4

2 directories, 6 files
[root@stone file]# rm -r file1
rm: descend into directory `file1'? y
rm: remove regular empty file `file1/f1'? y
rm: remove regular empty file `file1/f2'? y
rm: remove directory `file1'? y
[root@stone file]# tree
.
`-- file2
    |-- f1
    |-- f2
    |-- f3
    `-- f4

1 directory, 4 files
[root@stone file]# rsync -av root@172.16.3.62:/root/file1/ /root/file
root@172.16.3.62's password: 
receiving file list ... done
./
file1/
file1/f1
file1/f2

sent 76 bytes  received 189 bytes  106.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f1
    |-- f2
    |-- f3
    `-- f4

2 directories, 6 files
#远程恢复

[root@stone file]# rsync -avz file1 file2 --exclude "f2"
building file list ... done
file1/
file1/f1

sent 107 bytes  received 48 bytes  310.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   |-- f1
|   `-- f2
`-- file2
    |-- f3
    |-- f4
    `-- file1
        `-- f1

3 directories, 5 files
#-z表示使用数据压缩
#--exclude指定需要排除的文件
#--exclude-from file 通过file文件指定需要排除的文件

[root@stone file]# tree
.
|-- file1
|   `-- f2
`-- file2
    |-- f3
    |-- f4
    `-- file1
        `-- f1

3 directories, 4 files
[root@stone file]# rsync -avz file1 file2 --delete
building file list ... done
deleting file1/f1
file1/
file1/f2

sent 111 bytes  received 48 bytes  318.00 bytes/sec
total size is 0  speedup is 0.00
[root@stone file]# tree
.
|-- file1
|   `-- f2
`-- file2
    |-- f3
    |-- f4
    `-- file1
        `-- f2

3 directories, 4 files
#--delete在目的端删除那些在源端已经不存在的文件

6、备份 dd
[root@stone file]# ls
file1  file2
[root@stone file]# dd if=/dev/zero of=f1 bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00411 seconds, 255 MB/s
[root@stone file]# ls
f1  file1  file2
[root@stone file]# dd if=f1 of=f1.iso
2048+0 records in
2048+0 records out
1048576 bytes (1.0 MB) copied, 0.016806 seconds, 62.4 MB/s
[root@stone file]# ls
f1  f1.iso  file1  file2