编程小贴士

给你的编程提供小点子


如何解压 tar 文件到不同的目录中

我想要解压一个tar文件到一个叫/tmp/data的指定目录。我该如何在Linux或者类Unix的系统中使用tar命令解压一个tar文件到不同的目录中?

tips1000_2015-02-04_00-37-07

你不必使用cd命令切换到其他的目录并解压。可以使用下面的语法解压一个文件:

语法

典型Unix tar语法:

  1. tar -xf file.name.tar -C /path/to/directory

GNU/tar 语法:

  1. tar xf file.tar -C /path/to/directory
  2. tar xf file.tar directory /path/to/directory

示例:解压文件到另一个目录中

在本例中。我解压$HOME/etc.backup.tar到/tmp/data目录中。首先,需要手动创建这个目录,输入:

  1. mkdir /tmp/data

要解压$HOME/etc.backup.tar 到/tmp/data中,输入:

  1. tar -xf $HOME/etc.backup.tar -C /tmp/data

要看到进度,使用-v选项:

  1. tar -xvf $HOME/etc.backup.tar -C /tmp/data

示例输出:

tips1000_2015-02-04_00-37-07

Gif 01: tar命令解压文件到不同的目录

你也可以指定解压的文件:

  1. tar -xvf $HOME/etc.backup.tar file1 file2 file3 dir1 -C /tmp/data

要解压foo.tar.gz(.tgz扩展文件)包到/tmp/bar中,输入:

  1. mkdir /tmp/bar
  2. tar -zxvf foo.tar.gz -C /tmp/bar

要解压foo.tar.bz2(.tbz, .tbz2 和 .tb2 扩展文件)包到/tmp/bar中,输入:

  1. mkdir /tmp/bar
  2. tar -jxvf foo.tar.bz2 -C /tmp/bar

4 Responses to “ 如何解压 tar 文件到不同的目录中 ”

  1. Joe Tadt说道:

    Helpful blog post and thanks for sharing. Some things in here I haven’t thought about before, I would like to use this moment to say that I really like this blog. It has been a great resource of knowledge for me. Thank you so much!

  2. yeezy boost说道:

    I simply desired to appreciate you again. I’m not certain what I could possibly have achieved in the absence of those information shared by you relating to my problem. It was actually a very fearsome issue for me personally, nevertheless coming across a skilled tactic you solved the issue took me to leap with delight. I’m just grateful for this service and in addition have high hopes you are aware of a great job that you are getting into educating men and women by way of your webpage. I am sure you have never got to know any of us.

  3. You ought to experience a contest first of the finest blogs on-line. I will suggest this page!

  4. i love pron说道:

    qW2Ciz wow, awesome post.Really thank you! Much obliged.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>