57

Is it possible to create a target directory, similar to mkdir -p, where I can define a non-existent target directory within my tar command, and tar will create the directory for me?

I know I can redirect the output to a directory using tar -C /target/dir, but this doesn't work if the target directory is non-existent.

1
  • 4
    If it isn't possible, you could always create a shell alias.
    – nopcorn
    Nov 25, 2011 at 4:02

5 Answers 5

64
mkdir -p /target/dir && tar -C /target/dir
5
  • 4
    No spoonfeeding, straight and to the point. Oct 31, 2016 at 6:56
  • 27
    Minor suggested change: mkdir -p /target/dir && tar -C $_ Jan 13, 2017 at 22:41
  • 4
    @MarkMelville describe to us what $_ supposed to de here
    – Suhayb
    Apr 22, 2019 at 20:13
  • 13
    @Suhayb $_ is a special parameter in Bash that expands to the last argument to the previous command.
    – ConceptRat
    May 28, 2019 at 4:09
  • Why assume BASH?
    – muthuh
    May 8, 2023 at 10:53
60
tar -xvf bash.html_node.tar.gz --one-top-level

From man page of tar command

--one-top-level[=DIR]

    Extract all files into DIR, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by --auto-compress).

----- test

test 1

tar zxvf ../aaa.tgz --one-top-level
aaa/
aaa/222
aaa/111

tree aaa
aaa/
├── 111
└── 222

// if tgz name same as root_dir of archive, no other effect

test 2

mv ../aaa.tgz ../bbb.tgz

tar zxvf ../bbb.tgz --one-top-level
aaa/
aaa/222
aaa/111

tree bbb
bbb
└── aaa
    ├── 111
    └── 222

// if tgz name not same as root_dir of archive, create a top-level dir

5
  • 13
    Requires GNU tar? Mac default bsd tar does not have this option. Apr 18, 2019 at 18:12
  • 4
    Yes, this should be mentioned more clearly.
    – kcrisman
    Nov 7, 2019 at 17:36
  • 5
    GNU Tar 1.26 does not have the --one-top-level option, it appears. Mar 16, 2020 at 0:10
  • 1
    --one-top-level is available as of GNU Tar 1.28. Apr 1, 2023 at 11:03
  • 1
    if it needs to be unarchived to someother destination then use -C option tar --use-compress-program=unzstd -xvf recipe-zlib.tar.zst --one-top-level -C destination_path Jan 25 at 9:13
9

This made more sense to me: mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder

mkdir makes the folder although I don't quite understand the -p switch. && lets you execute a second command. I used typical tar switches but at the end -C is used to change directories and extract to that location needed.

reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive

3
  • 3
    The -p switch will not throw an error if the directory already exists.
    – David
    Sep 3, 2015 at 21:10
  • 3
    @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist. Oct 21, 2015 at 13:40
  • 1
    Not sure if I quite understood it but -C doesn't do anything except creating directory which was already created by mkdir. Sep 29, 2022 at 17:23
1

If you don't know the paths to create...
Get the needed paths from the tar file, then create them...

for P in \`tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6\`; do  
    C=\`echo "${P: -1}"\`  #get the last character  
    if [ $C = '/' ]; then  
        echo "Found directory: $P"  
        mkdir -p $P  
    fi  
done
0

All the above answers work - but if the contents inside the tar is within a directory - they won't work the way you want:

So this simple trick worked for me:

tar -xvzf v<tarball>.tar.gz && mv ./<INSIDE_FOLDER> ./<NEW_FOLDER>

You must log in to answer this question.

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