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.

  • Duplicate: unix.stackexchange.com/questions/11018/… Commented Sep 18, 2021 at 3:49
  • 3
    Does this answer your question? How to choose directory name during untarring Commented Sep 18, 2021 at 3:49
  • 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
    Commented Sep 18, 2021 at 4: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
    Commented Sep 18, 2021 at 5:15

1 Answer 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .