Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to extract files to another directory using 'tar' command?

I thought tar archive.tar /users/mylocation would work, but it doesn't. How can I do that?
by

3 Answers

akshay1995
To extract an archive to a directory different from the current, use the -C, or --directory, tar option, as in

tar -xf archive.tar -C /target/directory

Note that the target directory has to exist before running that command (it can be created by mkdir /target/directory).
RoliMishra
Note that if your tarball already contains a directory name you want to change, add the --strip-components=1 option:

tar xf archive.tar -C /target/directory --strip-components=1
pankajshivnani123
Combining the previous answers and comments:

To simply extract the contents and create target directory if it is missing:
mkdir -p /target/directory && tar xf archive.tar -C /target/directory


To extract and also remove the root(first level) directory in the zip
mkdir -p /target/directory && tar xf archive.tar -C /target/directory --strip-components=1

Login / Signup to Answer the Question.