Linux ubuntu 将多个 .tar.gz 文件解压到新目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5177762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:06:16  来源:igfitidea点击:

ubuntu extract multiple .tar.gz files to new directory

linuxubuntu

提问by chris

I have about 200,000 thumbs in a folder that are all gzipped ending with .tar.gz What I am looking to do is extract all the files in that folder but to a different folder. Does anyone know a command to do this? I found this online but I wouldnt know how to use it to extract to a different folder.

我在一个文件夹中有大约 200,000 个拇指,所有这些都以 .tar.gz 结尾我想要做的是将该文件夹中的所有文件解压缩到不同的文件夹。有谁知道执行此操作的命令?我在网上找到了这个,但我不知道如何使用它来提取到不同的文件夹。

for i in *.tar.gz; do tar -xvzf $i; done

采纳答案by Delan Azabani

Add the -Coption to select the target directory:

添加-C选择目标目录的选项:

for i in *.tar.gz; do tar xvzf $i -C path/to/output/directory; done

回答by Zsolt Botykai

actdir=`pwd`
for files in *tar.gz ; do
  filedir=`basename $files .tar.gz`
  mkdir $filedir 
  cd $filedir
  tar -xzf ../$files
  cd $actdir
done

HTH

HTH

回答by quarkdown27

Right now you are using

现在你正在使用

tar

to extract all the files. I believe that you can set which directory to output to.

提取所有文件。我相信你可以设置输出到哪个目录。

It would be something like this:

它会是这样的:

for i in *.tar.gz; do tar -xvzf $i -C directory; done

where directory is the path of the folder you want to extract the files to.

其中 directory 是您要将文件解压缩到的文件夹的路径。

Refer to http://www.computerhope.com/unix/utar.htm(documentation on tar).

请参阅http://www.computerhope.com/unix/utar.htm(有关 tar 的文档)。

回答by rrdharan

The -C option is probably better. You could also do:

-C 选项可能更好。你也可以这样做:

$ mkdir /path/to/newfolder
$ for i in *.tar.gz; do files="$files $(readlink -f $i)"; done # builds absolute list of filenames
$ cd /path/to/newfolder
$ for i in $files; do tar -zxvf $i; done

$ mkdir /path/to/newfolder
$ for i in *.tar.gz; do files="$files $(readlink -f $i)"; done # 建立文件名的绝对列表
$ cd /path/to/newfolder
$ for i in $files; 做 tar -zxvf $i; 完毕