将包含在 archive.tar.gz 中的文件解压缩到名为 archive 的新目录 [英] Extract files contained in archive.tar.gz to new directory named archive
问题描述
我有一个包含大约 800 个 .tgz 档案的目录,每个包含大约 10 个文件.实际上,我想将每个存档转换为同名目录.是否有一个简单的单行命令来执行此操作,还是我应该编写一个脚本?
I have a directory containing about 800 .tgz archives, each containing about 10 files. Effectively, I want to convert each archive into a directory of the same name. Is there a simple one line command to do this, or should I write a script?
推荐答案
我认为您需要为此编写脚本.您可以使用 tar -C 选项指定放置提取物的目录.
I think you will need to script this. You can specify the directory that the extract is placed in by using the tar -C option.
以下脚本假定目录不存在且必须创建.如果目录确实存在,脚本仍然可以工作 - mkdir 只会失败.
The script below assumes that the directories do not exist and must be created. If the directories do exist the script will still work - the mkdir will simply fail.
tar -xvzf archive.tar.gx -C archive_dir
例如
for a in *.tar.gz
do
a_dir=`expr $a : '\(.*\).tar.gz'`
mkdir $a_dir 2>/dev/null
tar -xvzf $a -C $a_dir
done
这篇关于将包含在 archive.tar.gz 中的文件解压缩到名为 archive 的新目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!