This question already has an answer here:

I have several tar files (~270) which untar into the following separate directory structure:

00001.tar -> /00001/001/file1.ima

00002.tar -> /00002/001/file2.ima

. . .

00270.tar -> /00270/001/file270.ima

I wondered if there is a specifier in the untar command that would allow me to not only untar the files, but also move the files (file1.ima, file2.ima, . . . file217.ima) to a common directory.

Does anyone know how to do this? Sorry if this is a simple thing to do; I am pretty inexperienced in this (obviously).

Thanks hugely in advance for any help on this!

marked as duplicate by David Foerster, Eric Carvalho, Videonauth, Community Jun 6 '16 at 14:13

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.


Something like

for f in {00001..00270}.tar; do
  tar xf "$f" --strip-components=1 

should untar them all into directory 001 (i.e. stripping the leading path components 00001/, 00002/ etc). If you want to untar them to a completely different location, you can use the -C option and (optionally) strip both original path components

for f in {00001..00270}.tar; do
  tar xf "$f" --strip-components=2 -C path/to/other/dir 

(directory path/to/other/dir must already exist).

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