tar 与 cpio 的简单对比

简介: 本文对比了 tar 和 cpio 在打包、解包、文件构成及特性上的区别。

生成实验数据

mkdir {
   0..9}{
   a..z}
for i in *;do
        cd $i
        echo $RANDOM > $RANDOM
        cd ..
done

打包时间对比

  • cpio
    $ time find . | cpio -o > bnk.cpio
    real    0m0.094s
    user    0m0.012s
    sys     0m0.012s
    $ mv bnk.cpio ../
    
  • tar
    $ time tar cf bnk.tar {
         0..9}{
         a..z}
    real    0m0.208s
    user    0m0.013s
    sys     0m0.032s
    $ mv bnk.tar ../
    

cpio 打包速度较 tar 快一倍以上。

文件大小对比与十六进制分析

$ du bnk*
40      bnk.cpio
400     bnk.tar
  • cpio
    $ hexdump -C bnk.cpio | less
    00000000  c7 71 02 08 17 8f ed 41  e8 03 e8 03 06 01 00 00  |.q.....A........|
    00000010  90 66 6b fd 02 00 00 00  00 00 2e 00 c7 71 02 08  |.fk..........q..|
    00000020  9b 09 ed 41 e8 03 e8 03  02 00 00 00 90 66 9d fc  |...A.........f..|
    00000030  03 00 00 00 00 00 30 61  00 00 c7 71 02 08 53 57  |......0a...q..SW|
    00000040  a4 81 e8 03 e8 03 01 00  00 00 90 66 9d fc 08 00  |...........f....|
    00000050  00 00 05 00 30 61 2f 33  32 39 32 00 31 34 31 38  |....0a/3292.1418|
    00000060  0a 00 c7 71 02 08 03 80  ed 41 e8 03 e8 03 02 00  |...q.....A......|
    00000070  00 00 90 66 9d fc 03 00  00 00 00 00 30 62 00 00  |...f........0b..|
    00000080  c7 71 02 08 77 89 a4 81  e8 03 e8 03 01 00 00 00  |.q..w...........|
    00000090  90 66 9d fc 09 00 00 00  05 00 30 62 2f 32 32 31  |.f........0b/221|
    000000a0  35 38 00 00 36 37 32 37  0a 00 c7 71 02 08 27 14  |58..6727...q..'.|
    000000b0  ed 41 e8 03 e8 03 02 00  00 00 90 66 9d fc 03 00  |.A.........f....|
    000000c0  00 00 00 00 30 63 00 00  c7 71 02 08 d4 17 a4 81  |....0c...q......|
    000000d0  e8 03 e8 03 01 00 00 00  90 66 9d fc 09 00 00 00  |.........f......|
    000000e0  06 00 30 63 2f 31 39 32  32 33 00 00 33 30 31 34  |..0c/19223..3014|
    000000f0  32 0a c7 71 02 08 b4 f6  ed 41 e8 03 e8 03 02 00  |2..q.....A......|
    00000100  00 00 90 66 9d fc 03 00  00 00 00 00 30 64 00 00  |...f........0d..|
    00000110  c7 71 02 08 9b 7b a4 81  e8 03 e8 03 01 00 00 00  |.q...{
         ..........|
    00000120  90 66 9d fc 09 00 00 00  05 00 30 64 2f 32 34 34  |.f........0d/244|
    00000130  33 31 00 00 34 38 32 32  0a 00 c7 71 02 08 6a 34  |31..4822...q..j4|
    00000140  ed 41 e8 03 e8 03 02 00  00 00 90 66 9d fc 03 00  |.A.........f....|
    00000150  00 00 00 00 30 65 00 00  c7 71 02 08 55 57 a4 81  |....0e...q..UW..|
    00000160  e8 03 e8 03 01 00 00 00  90 66 9d fc 08 00 00 00  |.........f......|
    00000170  06 00 30 65 2f 31 38 36  32 00 31 38 32 35 37 0a  |..0e/1862.18257.|
    00000180  c7 71 02 08 04 80 ed 41  e8 03 e8 03 02 00 00 00  |.q.....A........|
    00000190  90 66 9d fc 03 00 00 00  00 00 30 66 00 00 c7 71  |.f........0f...q|
    000001a0  02 08 78 89 a4 81 e8 03  e8 03 01 00 00 00 90 66  |..x............f|
    000001b0  9d fc 09 00 00 00 06 00  30 66 2f 32 39 33 33 34  |........0f/29334|
    000001c0  00 00 31 35 34 39 31 0a  c7 71 02 08 04 16 ed 41  |..15491..q.....A|
    
  • bnk
    $ hexdump -C bnk.tar | less
    00000000  30 61 2f 00 00 00 00 00  00 00 00 00 00 00 00 00  |0a/.............|
    00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000060  00 00 00 00 30 30 30 30  37 35 35 00 30 30 30 31  |....0000755.0001|
    00000070  37 35 30 00 30 30 30 31  37 35 30 00 30 30 30 30  |750.0001750.0000|
    00000080  30 30 30 30 30 30 30 00  31 34 36 34 34 31 37 36  |0000000.14644176|
    00000090  32 33 35 00 30 31 30 36  37 34 00 20 35 00 00 00  |235.010674. 5...|
    000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000100  00 75 73 74 61 72 20 20  00 61 64 61 6d 6c 7a 00  |.ustar  .adamlz.|
    00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000120  00 00 00 00 00 00 00 00  00 61 64 61 6d 6c 7a 00  |.........adamlz.|
    00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000200  30 61 2f 33 32 39 32 00  00 00 00 00 00 00 00 00  |0a/3292.........|
    00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000260  00 00 00 00 30 30 30 30  36 34 34 00 30 30 30 31  |....0000644.0001|
    00000270  37 35 30 00 30 30 30 31  37 35 30 00 30 30 30 30  |750.0001750.0000|
    00000280  30 30 30 30 30 30 35 00  31 34 36 34 34 31 37 36  |0000005.14644176|
    00000290  32 33 35 00 30 31 31 32  31 31 00 20 30 00 00 00  |235.011211. 0...|
    000002a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000300  00 75 73 74 61 72 20 20  00 61 64 61 6d 6c 7a 00  |.ustar  .adamlz.|
    00000310  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000320  00 00 00 00 00 00 00 00  00 61 64 61 6d 6c 7a 00  |.........adamlz.|
    00000330  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000400  31 34 31 38 0a 00 00 00  00 00 00 00 00 00 00 00  |1418............|
    00000410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    

可见 cpio 比 tar 小得多,cpio 通过文本保存目录及文件信息,无索引,而 tar 通过地址结构分别保存目录、文件及文件归属。这里注意,tar 文件大小限制取决于文件系统,而 cpio 最大仅支持 4GiB

解包时间对比

  • cpio
    $ time cpio -id < bnk.cpio
    real    0m0.193s
    user    0m0.003s
    sys     0m0.050s
    
  • bnk
    $ time tar xf bnk.tar
    real    0m0.031s
    user    0m0.002s
    sys     0m0.014s
    

在解包速度上,tar 比 cpio 更快。

结论

打包耗时 解包耗时 文件大小 备注
cpio 1 6.22 1 无索引,最大仅支持 4GiB
tar 2.21 1 10 地址索引,大小极限取决于文件系统

非必要场景下,更推荐使用 tar 打包备份。

目录
相关文章
|
6月前
|
Ubuntu Linux
在Linux中如何解压 .xz 和 tar.xz 文件?
【4月更文挑战第17天】
4154 6
在Linux中如何解压 .xz 和 tar.xz 文件?
|
Linux
Linux:文件压缩解压gz、tar.gz、tar.xz、tar.bz2、tgz、zip
Linux:文件压缩解压gz、tar.gz、tar.xz、tar.bz2、tgz、zip
162 0
|
存储
解压cpio文件
解压cpio文件 cpio -idmv < filename.cpio 同样可以解压img文件:cpio -idmv < filename.
1196 0
|
Linux
Linux下tar bz gz等压缩包的压缩和解压
Linux下用户经常需要备份计算机系统中的数据,为了节省存储空间,常常将备份文件进行压缩,本文是对压缩和解压命令的大致总结   .tar.gz  解压:tar zxvf FileName.tar.gz  压缩:tar zcvf FileName.
1308 0
gz解压
gzip -d xxx.gz解压
1576 0