LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   find and unzip selected sub folders in tar.gz files (https://www.linuxquestions.org/questions/linux-server-73/find-and-unzip-selected-sub-folders-in-tar-gz-files-842349/)

samanp 11-04-2010 11:32 AM

find and unzip selected sub folders in tar.gz files
 
there are few hundred tar.gz files each having several sub dirs in them (ex: ftp_server1_logs, mail_server2_logs). However the main tar.gz files (ex: 20101001.tar.gz) does not contain all sub dirs every time (ex: 20101001.tar.gz may only contain some ftp server log dirs etc..)

what i have to do is find the tar.gz files that contain a specific type of log type (ex: ftp logs) and extract all logs to a single folder (ex: ftp logs)

any help on this (with specific command line options) is highly appreciated.

thanks

thesnow 11-05-2010 09:16 AM

I would do something like this (probably not the most elegant, but it works). It needs to be tweaked if you have duplicate filenames within the archives, and will leave you with some empty directories after the extract/move, but should be easy enough to clean up.

Code:

#!/bin/bash

for i in $(ls *.gz)
do
        for j in $(tar -tzf $i | grep "search string")
                do
                        tar -zxvf $i $j
                        mv -v $j output_directory/
                done
done

exit



All times are GMT -5. The time now is 09:54 PM.