3

I have a .tar.gz file which has multiple folders underneath. Each folder has multiple files and I only want to extract the SNAPSHOT.jar from all the folders it has underneath.

I tried using wildcards but its not helping. Ex:

    tar -xf samplejars.tar.gz --wildcards "*SNAPSHOT*.jar"

samplejars.tar.gz has many folders and I only want to extract SNAPSHOT.jar. How do I do that?

Note: All the jars have unique/different names.

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
Dhiraj
  • 427
  • 8
  • 20
  • [P.S. Answer to [this][1] question by user 'ford' answers my question perfectly] [1]: https://stackoverflow.com/questions/14295771/how-do-i-extract-files-without-folder-structure-using-tar?noredirect=1&lq=1 – Dhiraj May 21 '19 at 08:20

3 Answers3

3

I tested it with the following folder structure:

data/
data/a
data/a/ANOTHER_SNAPSHOT.jar
data/b
data/c
data/c/SNAPSHOT.jar
data/d
data/e
data/f
data/f/SNAPSHOT.jar.with.extension
data/g
data/g/SNAPSHOT.jar
data/h

The following wildcard mask works and extract only the files matching exactly SNAPSHOT.jar not SNAPSHOT.jar.with.extension and ANOTHER_SNAPSHOT.jar

tar -xf data.tar.gz --wildcards "*/SNAPSHOT.jar"

Result:

data/c/SNAPSHOT.jar
data/g/SNAPSHOT.jar
Florian Schlag
  • 639
  • 4
  • 16
  • Thanks Florian, this helps to a certain extent. But, is it possible to get just the jars and not the folders out of the tar.gz? Like, the sample output from your answer has the two SNAPSHOT.jars under the folders data/c/ and data/g/. How do I get just the .jars and not the folders? – Dhiraj May 20 '19 at 08:04
  • 2
    In theory this is possible. Some ways are discussed in this question: https://stackoverflow.com/questions/14295771/how-do-i-extract-files-without-folder-structure-using-tar The problem is, you are looking for multiples jars with the **same** name. Therefore they can't be extracted into the same folder. – Florian Schlag May 20 '19 at 08:50
  • Thanks Florian. The link you shared gave me what I needed. – Dhiraj May 20 '19 at 09:37
0

You can create a file with the pattern you are looking for:

echo "*SNAPSHOT*.jar" > target

If you have multiple patterns, you can add multiple lines to your target file

echo "*.md" >> target

Then you can use the --files-from switch:

tar -xf samplejars.tar.gz --files-from=filename

I tested with

data/
data/a/
data/a/ANOTHER_SNAPSHOT.jar
data/b/
data/c/
data/c/SNAPSHOT.jar
data/d/
data/e/
data/f/
data/f/SNAPSHOT.jar.with.extension
data/g/
data/g/SNAPSHOT-2.jar
data/g/SNAPSHOT.jar
data/h/

Result

data/a/ANOTHER_SNAPSHOT.jar
data/c/SNAPSHOT.jar
data/g/SNAPSHOT-2.jar
data/g/SNAPSHOT.jar

If all the files have unique filenames, as the OP said, you can use --strip-components to remove the file structure

tar -xf samplejars.tar.gz --files-from=filename --strip-components 2

With my data, the result was:

ANOTHER_SNAPSHOT.jar
SNAPSHOT.jar
SNAPSHOT-2.jar

Because I did not have unique names, one of the SNAPSHOT.jar files was overwritten in the --strip-components step.

Dan Meigs
  • 363
  • 4
  • 13
  • Thanks @Dan Meigs, this helps to a certain extent. But, is it possible to get just the jars and not the folders out of the tar.gz? Like, the sample output from your answer has the two SNAPSHOT.jars under the folders data/c/ and data/g/ etc. How do I get just the .jars and not the folders? – Dhiraj May 20 '19 at 08:12
  • 1
    I updated my answer based on the link provided by @Florian Schlag – Dan Meigs May 20 '19 at 14:22
0

You can use xargs for that :

tar -tf data.tar.gz | grep SNAPSHOT.jar | xargs tar -xf data.tar.gz 

Then, to move all the files to the root directory

find archive_root_dir -type f -exec mv -i {} . \;
blue112
  • 52,634
  • 3
  • 45
  • 54
  • Thanks @blue112, this helps to a certain extent. But, is it possible to get just the jars and not the folders out of the tar.gz? I mean, the command you shared brings out the folder structure from the tar.gz. Can we get just the jars out of the tar.gz, and no folder structure? – Dhiraj May 20 '19 at 08:11
  • @Dhiraj How would you handle two jars named the same way ? – blue112 May 20 '19 at 08:13
  • Oh, I forgot to mention that :) The jars have unique names, so that's not a problem. – Dhiraj May 20 '19 at 08:14