Super User is a question and answer site for computer enthusiasts and power users. 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
tar -tf filename.tar

I'd like to extract file and name to, folder2. Can this be done as a one-liner?

share|improve this question
up vote 19 down vote accepted

Use -C and --strip-components (See man tar).


mkdir FOLDER
# for remote tar file
curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

# for local tar file
tar -xz FILENAME -C FOLDER --strip-components=1


The C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.

The strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nester folders are removed.

share|improve this answer
I read man tar. Didn't spot --strip-components. nice one – Felipe Alvarez May 30 '10 at 6:25
tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here? – mmv-ru Jun 26 '11 at 7:42
I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion. – Iain Jul 22 '14 at 14:45

You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.

For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:

tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-'
share|improve this answer

You must log in to answer this question.

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