如何通过 mv 传输 tar 的输出?

Jos*_*nto 3 tar pipe shell-script mv

一个先前的问题我的问到如何通过管道焦油下载的文件,现在我想怎么管焦油的输出通过MV就知道了。看到我现在有这个命令:

wget -c https://github.com/JeffHoogland/moksha/archive/0.1.0.tar.gz | tar -xz
Run Code Online (Sandbox Code Playgroud)

这将创建一个名为 的目录moksha-0.1.0,但我想知道如何将此输出目录重命名为moksha,也许通过|此命令末尾的管道 ( )。虽然如果您知道如何在没有管道的情况下执行此操作,但仍然与 wget 和 tar 位于同一行代码上,我也很乐意接受它。

需要明确的是,我知道:

wget -c https://github.com/JeffHoogland/moksha/archive/0.1.0.tar.gz | tar -xz -C moksha
Run Code Online (Sandbox Code Playgroud)

将创建一个输出目录,moksha但在此输出目录中会有该moksha-0.1.0目录,而我想将此moksha-0.1.0目录重命名为moksha,而不是放置moksha-0.1.0在名为moksha.

小智 8

像这样?

[root@b se]# wget -cqO - https://github.com/JeffHoogland/moksha/archive/0.1.0.tar.gz | tar -xz --transform=s/moksha-0.1.0/moksha/
[root@b se]# ls
moksha
[root@b se]# ls moksha
ABOUT-NLS       config.guess          debian                 Makefile.am
aclocal.m4      config.guess.dh-orig  depcomp                Makefile.in
AUTHORS         config.h.in           doc                    missing
autogen.sh      config.rpath          enlightenment.pc.in    netwm.txt
autom4te.cache  config.sub            enlightenment.spec.in  NEWS
BACKPORTS       config.sub.dh-orig    INSTALL                po
BUGS            configure             install-sh             README
ChangeLog       configure.ac          intl                   src
compile         COPYING               ltmain.sh              xdebug.sh
config          data                  m4                     x-ui.sh
Run Code Online (Sandbox Code Playgroud)

tar手册页:

--transform= EXPRESSION , --xform= EXPRESSION
    使用 sed 替换EXPRESSION来转换文件名。

所以sed可能需要这样做。虽然如果你有wget,你可能也有sed


Wil*_*ard 5

注意:我的回答并不是关于tar; 这是对您问题的这一部分的具体回答:

...这将创建一个名为 的目录moksha-0.1.0,但我想知道如何将此输出目录重命名为moksha,也许通过|此命令末尾的管道 ( )。虽然如果您知道如何在没有管道的情况下执行此操作,但仍然与 wget 和 tar 位于同一行代码上,我也很乐意接受它。

管道将一个命令的标准输出与下一个命令的标准输入连接起来。它与文件没有太大关系——当然与移动由管道中的前一个命令创建的文件无关。

你想要的是一个列表

来自man bash

   AND  and  OR  lists are sequences of one of more pipelines separated by
   the && and ?? control operators, respectively.  AND and  OR  lists  are
   executed with left associativity.  An AND list has the form

          command1 && command2

   command2  is  executed if, and only if, command1 returns an exit status
   of zero.
Run Code Online (Sandbox Code Playgroud)

对一个命令进行简单的 if-then 检查,或者使一个命令以另一个命令的成功为条件的最简单方法是使用&&. 例子:

[ -r somefile ] && cat somefile
# Checks if somefile exists and is readable; cats the file if it is.

sed -f sedscript inputfile > outputfile && mv outputfile inputfile
# One way (not the best) to edit a file in place.
# The point is that the second command only executes if the first command "succeeds" (i.e. returns a zero exit status).
Run Code Online (Sandbox Code Playgroud)

因此,对于您的特定命令,只需执行以下操作:

(The whole command you wrote) && mv moksha-0.1.0 moksha
Run Code Online (Sandbox Code Playgroud)