1

I have a tarfile which has the following structure:

- dir1
 -- file
- dir2
- dir3

If I try running tar -xf tarfile.tar.gz dir1/file, dir1 containing the file will be created. I would like instead to just get the file without the directory in between. I could move the file and delete the directory, but I was wondering if the tar command offers support for doing that directly.

I am using Alpine Linux.

3
  • 2
    Does your system's version of tar support the --strip-components option? – steeldriver Sep 26 '16 at 17:20
  • It does on Ubuntu. However, I just realized that I won't be using ubuntu for this command, but Alpine instead. I need to recheck if there's support for it. – Alberto Rivera Sep 26 '16 at 17:22
  • @steeldriver It is supported (I just need to upgrade) – Alberto Rivera Sep 26 '16 at 17:26
2

POSIXly (except for the gunzip part that is not a POSIX command):

gunzip < file.tar.gz | pax -rs'|.*/||' dir1/file1

With bsdtar:

tar -s'|.*/||' -xf file.tar.gz dir1/file1

With GNU tar:

tar --transform='s|.*/||' -xf file.tar.gz dir1/file1

With star:

star -s '|*/||' -x -f file.tar.gz dir1/file1

Note that for some of those, if the file is a a symlink, the substitution will also affect the target of the symlink.

1
  • tar --transform works on Alpine (after upgrading). I ended up using the suggestion in the comments, but I will accept your answer since it also works. – Alberto Rivera Sep 26 '16 at 20:58
0

You could extract to stdout (-O) and pipe it to the wanted filename.

1
  • 1
    Though that would only work for regular files and you'd loose the file metadata (ownership, time, permission...). – Stéphane Chazelas Sep 26 '16 at 20:42
0

Here is my solution in Ruby. I make no attempt to handle sym-links properly, but that could be added of course.

outfiles = `tar zxvf #{ARGV[0]}`
root_dirs = {}
outfiles.each_line do |l|
  s = l.split('/')
  root_dirs[s[0]] = 1
  `mv #{l.chomp} ./#{s[-1]}`
end
root_dirs.each_key {|rd| `rm -rf #{rd}`}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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