Extract files contained in archive.tar.gz to new directory named archive

柔情痞子 提交于 2019-12-06 16:43:04

问题


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?


回答1:


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.

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

e.g.

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



回答2:


Create a folder in which you want to extract like this mkdir archive and pass folder name with -C while extracting, tar -xvf archive.zip -C archive




回答3:


Well, if you run $ tar -zxf some-archive.tar.gz -C ., (note the dot at the end) a new directory called some-archive/ will be created in the directory you are currently in.

Maybe this is what you meant by your question? This is what I generally want so it may work for you as well.



来源:https://stackoverflow.com/questions/15007947/extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!