0

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.

PeanutsMonkey
  • 1,892
  • 9
  • 27
  • 28

1 Answers1

1

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.

Handyman5
  • 5,257
  • 26
  • 30