linux - 鸟哥书中关于tar模拟cp命令的疑问
怪我咯
怪我咯 2017-04-17 12:01:52
0
4
387

例子如下,我搞不懂能用简单的 cp -r /etc /tmp干嘛还要用tar?tar -cvf - /etc | tar -xvf -和cp相比到底有什么特别之处?

范例八:将 /etc/ 打包后直接解开在 /tmp 底下,而不产生文件!
[root@linux ~]# cd /tmp
[root@linux tmp]# tar -cvf - /etc | tar -xvf -
# 这个动作有点像是 cp -r /etc /tmp 啦~依旧是有其有用途的!
# 要注意的地方在於输出档变成 - 而输入档也变成 - ,又有一个 | 存在~
# 这分别代表 standard output, standard input 与管线命令啦!

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
黄舟

There is a common experience: Transmitting a large number of fragments is a waste of time. For example: when backing up and restoring website files, you generally don’t upload and download them directly, but habitually pack them into a package first...

This gameplay is the same. If you direct the cp command, the essence is to read a file and then write a file. Through tar pipeline relay, you can make full use of the size of the pipe buffer, which essentially means reading the size of a buffer and then writing the size of the buffer. In this way, small files can be read in batches at a time and written in batches at a time.

tar can do this because the tar package is a format that facilitates sequential inflow and sequential outflow. The tar format is basically: [TAR文件头][文件实际内容][TAR文件头][文件实际内容] …… [TAR文件头][文件实际内容][TAR文件尾], so the later level of tar can flow in a little bit of data for processing, without having to wait for the entire tar package to arrive before unpacking it.

左手右手慢动作

Performance issues. The
cp command will read the files one by one and then write them to the new location. The
tar command will read as much of the file contents as possible before batch writing to the new location.

刘奇

I think the only special thing is that tar can display the copied files on the screen, while cp displays nothing

小葫芦

I think another reason is that using tar processing can preserve file permissions. If it is cp, it is hard to say what permissions the final file will have.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template