0

I tried to extract filename1.tar.gz but I got the different name which is the folder archive inside lets say archive_folder; how I can extract the tar.gz with the same name of the filename.

I use this command:

tar -xzvf filename1.tar.gz

also I want to write a command to extract multiple tar.gz with one line.

AdminBee
  • 22,803
Ala197
  • 1
  • Duplicate: https://unix.stackexchange.com/questions/11018/how-to-choose-directory-name-during-untarring – Nasir Riley Sep 18 '21 at 03:49
  • 3
  • No,I need the tar command to create the folder similar to the tar.gz filename. the answer your refer is to extract to directory already created – Ala197 Sep 18 '21 at 04:38
  • So add the extra step of creating the directory if it doesn't exist. mkdir -p filename1; tar xfz filename1.tar.gz -C filename1. or with the tarfile's name in a variable: f='filename1.tgz'; bn="$(basename "$f" ".tgz")"; mkdir -p "$bn"; tar xfz "$f" -C "$bn". If necessary, also use GNU tar's --transform or --strip-components options to remove any unwanted path elements like archive_folder/. The linked answer provides examples of everything you need to do. – cas Sep 18 '21 at 05:15

1 Answers1

1

As has been explained in the comments, you can use the answers to How to choose directory name during untarring, choosing the directory name to match the archive file name.

Another possibility is to use unar; it has a dedicated option for this, -d, which will extract the given archive to a directory matching its name.

Stephen Kitt
  • 434,908