请教bash中重定向符“-” 的使用问题

ks9960 2004-01-16 04:23:57
$ (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xvfp -)

该命令表示把 /source/directory 目录下的所有文件通过压缩和解压,快速的全部移动到 /dest/directory 目录下去

在一文档中看到了这么一段,我试了一下,不行,请教到底怎么用啊?
...全文
58 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ks9960 2004-01-19
  • 打赏
  • 举报
回复
就是我提问的那句话啊!我把它直接写死在脚本里面,只有这一句,涉及的目录我都先建立好了的。错误提示也是上面我贴的那段话。(./move.sh: ./0040775000101100010130000000000010002113131010147: No such file or directory)。
昨天有事没上来结贴,抱歉。现在结了,如果chaisave有什么新发现的话给我消息吧。
再次感谢!
ks9960 2004-01-18
  • 打赏
  • 举报
回复
my op is linux
my tar is :
tar (GNU tar) 1.13.19
Copyright 2001 Free Software Foundation, Inc.
This program comes with NO WARRANTY, to the extent permitted by law.
You may redistribute it under the terms of the GNU General Public License;
see the file named COPYING for details.
Written by John Gilmore and Jay Fenlason.
真的是非常感谢chaisave的细心帮助,我也试了一下你写的,也不行,痛苦,但直接运行是可以的啊。哎!下午结贴。
chaisave 2004-01-18
  • 打赏
  • 举报
回复
别客气,你能再贴一下你的脚本吗?
以及你的具体操作步骤,还有出错提示。
ks9960 2004-01-17
  • 打赏
  • 举报
回复
我刚才直接在bash下试了一下,可以用了。昨天我是把这句写在shell脚本里面的,就不能用,报错(./move.sh: ./0040775000101100010130000000000010002113131010147: No such file or directory)。chaisave你试一下了,谢谢啦!^-^
chaisave 2004-01-17
  • 打赏
  • 举报
回复
更正一下,tar并没有压缩的功效。它只是简单的打包,记得“tar means: Tape ARchive”
chaisave 2004-01-17
  • 打赏
  • 举报
回复
OK,我想你最好man tar先,下面使我的机机(Debian sid-- a GNU/Linux)的部分输出:
-f, --file [HOSTNAME:]F
use archive file or device F (default "-", meaning stdin/stdout)
“tar c .”在我的机器上其实是可以压缩当前目录的(default "-"),
若加上“f -”,即“tar cf - .”也行,“f -”说明了tar所操作的文件/设备是stdin/stdout,对于压缩就是stdout,对于解压就是stdin。
不知道你的机器上的tar是什么情况,这也是我让你看manual的原因。

我可能表述上不够清楚,还是多看man吧。
ks9960 2004-01-17
  • 打赏
  • 举报
回复
先谢谢两位了。听大家说了想明确一下意思:
"首先,“-”并不是重定向符,在这里它是stdout/stdin意思",“|”本来就是把前面命令的stdout送到后面命令的标准输入啊?还需要“-”吗?这个我没理解。
另外一个就是“tar cf - .” ,tar cf - .是怎么把目录下所有文件压缩的了?只是
tar cf .是不行的,“-”在这起了什么作用?
谢谢!
chaisave 2004-01-17
  • 打赏
  • 举报
回复
and my tar is:
$tar --version
tar (GNU tar) 1.13.25
Copyright © 2001 Free Software Foundation, Inc.
This program comes with NO WARRANTY, to the extent permitted by law.
You may redistribute it under the terms of the GNU General Public License;
see the file named COPYING for details.
Written by John Gilmore and Jay Fenlason.

chaisave 2004-01-17
  • 打赏
  • 举报
回复
it works under my system (Debian sid).
$cd /tmp;mkdir src dest
$cd src;cp ~/garbage* .;cd .. # fill it with some files to copy
$(cd src && tar cf - . ) | (cd dest && tar xvfp -)
$ls dest
garbage_blah garbage_blah1

Once again, please check your man page of tar, and make sure that your version of tar do support such options. BTW, what your OS is?
chaisave 2004-01-16
  • 打赏
  • 举报
回复
首先,“-”并不是重定向符,在这里它是stdout/stdin意思,
(cd /source/directory && tar cf - . )中,它和f option一起就是把/source/directory压缩出来的文件输出到stdout,
其次,“|”才是楼上提到的“管道”,它把stdout重定向到后面的filter程序的stdin。在这里是被压缩后的程序由括弧里的sub shell处理。
最后, (cd /dest/directory && tar xvfp -)中,“-”和f option一起表示让tar从标准输入读取要解压的文件
blh 2004-01-16
  • 打赏
  • 举报
回复
作为管道使用

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧