Ok, so I have a tone of .GZ files in a folder, and I'm looking to recursively look through each one of them and extract all the PNG files into another destination folder. How would I do that?
EDIT:
I've been using this command from the terminal to find a string in a GZ file and copy the entire file to another destination directory. Then do stuff with it. There's a few drawbacks. One, when I put in "PNG", it finds files such as CSS files that reference "PNG" instead of file types. Second, it doesn't output anything to the directory except for copying the entire file.. I'd like to extract the file instead.
find . -type f -print0 | xargs -0 grep -lh "png" | xargs -I % cp % /some_destination
EDIT:
Here's an example folder structure:
FILE001.GZ, FILE002.GZ, FILE003.GZ, etc
Not all of them contain PNGs, and some of them contain many files in a folder structure. What I want, is the following in another destination folder:
34950560.png, 3959560.png, etc.
Thank you ahead of time!
Assuming that your ".GZ" files are actually gzipped ".tar" archives with multiple files, then you can accomplish your goal in one line:
Explanation:
find . -type f -iname '*.GZ'
: find all .GZ files in the current path (incl. subdirectories).-iname
means case-insensitive, matching both .gz and .GZ filesxargs -n1 -I'{}' <command> '{}'
: call 'command' with at most one argument (-n1
) from stdin, placing the argument in the placeholder{}
.tar -C "/path/to/extract" -xf '{}' '*.png'
: Extract from the file got from xargs (-xf {}
), only files ending in '*.png'.-C /path/to/extract
: extract files there.2>/dev/null
: Mute the error messages raised from GZ files not-containing .png files.This command will extract all
.png
files in the specified folder (preserving any directory structures in the original tar.gz files). Identically named.png
files across multiple archives will be stored only once, i.e. the last extracted.png
file will overwrite the previous identically-named file. If you want to overcome this issue, then you'll need a more complex script like:The
extract_png
function will save extracted.png
files to a different subfolder for each archive, under/path/to/save
(e.g./path/to/save/FILE001/
,/path/to/save/FILE002/
etc).An explanation about
if tar -tf "$gzfile" '*.png' 2>/dev/null; then ...
: This will return true if there are .png files in the file "$gzfile". The-t
argument in tar means "list contents". When the specified files (*.png
) are not included in the archive,tar -t
prints an error message (hidden by2>/dev/null
) and returns a non-zero code which evaluates this condition to false.