问题
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