Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
If it isn't possible, you could always create a shell alias. – n0pe Nov 25 '11 at 4:02
up vote 14 down vote accepted
mkdir -p /target/dir && tar -C /target/dir
share|improve this answer

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 dont 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

share|improve this answer
The -p switch will not throw an error if the directory already exists. – David Sep 3 '15 at 21:10
@David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist. – Antonio Pérez Oct 21 '15 at 13:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.