Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Possible Duplicate:
Can I extract a specific folder using tar to another folder?

I am new to world of Linux and would like to know if I can extract a specific file or directory by giving it a new name or path. For example if my archive has the folders sample1, sample2 and sample3, I would like to be able to extract sample3 however as sample4. The same question applies to files.

share|improve this question

marked as duplicate by womble, Tim Brigham, Scott Pack, Iain Aug 13 '12 at 18:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer

up vote 1 down vote accepted

There are two ways that you can accomplish this task.

  1. tar has a -s option, defined as follows:

    -s pattern

    Modify file or archive member names according to pattern. The pattern has the format /old/new/[gps]. old is a basic regular expression. If it doesn't apply, the pattern is skipped. new is the replacement string of the matched part. ~ is substituted with the match, 1 to 9 with the content of the corresponding captured group. The optional trail- ing g specifies that matching should continue after the matched part and stopped on the first unmatched pattern. The optional trailing s specifies that the pattern applies to the value of symbolic links. The optional trailing p specifies that after a successful substitution the original path name and the new path name should be printed to standard error.

    You can run tar xf <archive file> -s /sample3/sample4/ to achieve your desired result.

  2. tar can extract files directly to standard output, after which you can use the standard Unix file redirection operations to direct the output into a file named whatever you want.

    Example: tar xf <archive file> -O sample3 > sample4

    Note that the "sample3" filename is not an argument to the -O parameter; it's being used by tar to limit extraction to just that file. If you did not specify sample3, then tar would extract every file in the archive and lump them into one big uncompressed output file.

share|improve this answer
add comment

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