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

Update since GNU tar 1.28:
use --one-top-level, see https://www.gnu.org/software/tar/manual/tar.html#index-one_002dtop_002dlevel_002c-summary

Older versions 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=${a%.tar.gz}
    mkdir --parents $a_dir
    tar -xvzf $a -C $a_dir
done

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)