Reputation: 497
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.
Upvotes: 4
Views: 4003
Reputation: 383
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.
Upvotes: 1
Reputation: 56432
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 {} . \;
Upvotes: 1
Reputation: 659
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
Upvotes: 3