how to extract single Folder / File from .tar.gz

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
zaji_nms
Posts: 616
Joined: Tue Oct 16, 2012 12:28 am

how to extract single Folder / File from .tar.gz

Post by zaji_nms »

Dear Expert

I am cracking my head but no success.

there are three question as below

how to view tar.gz to view files (all files including rrd xml) I tried as below but no success (not showing any rrd/xml files)

tar -ztvf nagiosxi.123456789.tar.gz

how to extract one full folder of /usr/local/nagios/share/perfdata/mydevice-7600/

how to extract single file /usr/local/nagios/share/perfdata/mydevice-7600/GigabitEthernet0_2.1234_Bandwidth.rrd

regards
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: how to extract single Folder / File from .tar.gz

Post by scottwilkerson »

Assuming nagiosxi.123456789.tar.gz is the backup file, run

Code: Select all

backupfile=/store/backups/nagiosxi/nagiosxi.123456789.tar.gz
to extract a dir

Code: Select all

tar -xzOf $backupfile $(basename $backupfile .tar.gz)/nagios.tar.gz | tar -xzf - usr/local/nagios/share/perfdata/mydevice-7600
to extract the file

Code: Select all

tar -xzOf $backupfile $(basename $backupfile .tar.gz)/nagios.tar.gz | tar -xzf - usr/local/nagios/share/perfdata/mydevice-7600/GigabitEthernet0_2.1234_Bandwidth.rrd
These files/directories will be placed in their path under where you are running the command, so if you run this from your home directory you would be able to view the file as:

Code: Select all

ls -l ~/usr/local/nagios/share/perfdata/mydevice-7600/GigabitEthernet0_2.1234_Bandwidth.rrd
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
zaji_nms
Posts: 616
Joined: Tue Oct 16, 2012 12:28 am

Re: how to extract single Folder / File from .tar.gz

Post by zaji_nms »

Dear Scott

Thanks for your detail reply Thanks its working fine, God bless u.

but very first question you omitted to reply.....how to list all zip files/folder in the main NagiosXi .tar.gz file? (just to list all files/folders inside .tar.gz)

Regards
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: how to extract single Folder / File from .tar.gz

Post by scottwilkerson »

zaji_nms wrote:Dear Scott

Thanks for your detail reply Thanks its working fine, God bless u.

but very first question you omitted to reply.....how to list all zip files/folder in the main NagiosXi .tar.gz file? (just to list all files/folders inside .tar.gz)

Regards
Sorry I missed that

Code: Select all

 tar -ztvf /store/backups/nagiosxi/nagiosxi.123456789.tar.gz
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
zaji_nms
Posts: 616
Joined: Tue Oct 16, 2012 12:28 am

Re: how to extract single Folder / File from .tar.gz

Post by zaji_nms »

Dear Scott

no, no its not working, in my very first post in this thread, i already mentioned, its just showing this

pwd
/tmp
tar -ztvf nagiosxi.123456789.tar.gz | more
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/
-rw-r--r-- nagios/users 2781800623 2222-22-22 05:09 nagiosxi.123456789/nagios.tar.gz
-rw-r--r-- nagios/users 3004779 2222-22-22 05:09 nagiosxi.123456789/nagvis.tar.gz
-rw-r--r-- nagios/users 901842 2222-22-22 04:59 nagiosxi.123456789/nagiosql.tar.gz
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/httpd/
-rw-r--r-- nagios/users 427 2222-22-22 10:05 nagiosxi.123456789/httpd/nagiosql.conf
-rw-r--r-- nagios/users 1010 2222-22-22 16:46 nagiosxi.123456789/httpd/nagios.conf
-rw-r--r-- nagios/users 601 2222-22-22 10:05 nagiosxi.123456789/httpd/nagiosxi.conf
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/logrotate/
-rw-r--r-- nagios/users 63 2222-22-22 13:19 nagiosxi.123456789/logrotate/nagiosxi
-rw-r--r-- nagios/users 20732817 2222-22-22 05:09 nagiosxi.123456789/nrdp.tar.gz
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/pgsql/
-rw-r--r-- nagios/users 7825867 2222-22-22 05:09 nagiosxi.123456789/pgsql/nagiosxi.sql
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/mysql/
-rw-r--r-- nagios/users 5619213 2222-22-22 05:09 nagiosxi.123456789/mysql/nagiosql.sql
-rw-r--r-- nagios/users 175136707 2222-22-22 05:09 nagiosxi.123456789/mysql/nagios.sql
-rw-r--r-- nagios/users 195 2222-22-22 04:59 nagiosxi.123456789/nagiosql-etc.tar.gz
-rw-r--r-- nagios/users 2917560 2222-22-22 05:09 nagiosxi.123456789/mrtg.cfg
drwxr-xr-x nagios/users 0 2222-22-22 05:09 nagiosxi.123456789/conf.d/
-rw-r--r-- nagios/users 67344 2222-22-22 05:09 nagiosxi.123456789/conf.d/onetwo.cfg
-rw-r--r-- nagios/users 992 2222-22-22 05:09 nagiosxi.123456789/conf.d/onetwothree_517.cfg
-------------------------------omitted some output-----------------------------
-rw-r--r-- nagios/users 999 2222-22-22 05:09 nagiosxi.123456789/conf.d/fourfive_10602.cfg
-rw-r--r-- nagios/users 967 2222-22-22 05:09 nagiosxi.123456789/conf.d/sixseven_504.cfg
-rw-r--r-- nagios/users 1047 2222-22-22 05:09 nagiosxi.123456789/conf.d/eightnine_10.cfg
-rw-r--r-- nagios/users 67755134 2222-22-22 05:09 nagiosxi.123456789/nagiosxi.tar.gz
-rw-r--r-- nagios/users 26194 2222-22-22 05:09 nagiosxi.123456789/hosts
-------------------------------------------------

i think it will work in two section basefile and then redirect as you mentioned in your solution

also can you please reply (solution) if i want to list files/folder

with FULL PATH
without FULL PATH (just filenames)

Regards
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: how to extract single Folder / File from .tar.gz

Post by scottwilkerson »

The backup actually contains what you showed, but it contains several other tar.gz files.
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: how to extract single Folder / File from .tar.gz

Post by scottwilkerson »

So for example to view the file in the inside nagios.tar.gz you could run

Code: Select all

tar -xzOf $backupfile $(basename $backupfile .tar.gz)/nagios.tar.gz | tar -ztvf -
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
zaji_nms
Posts: 616
Joined: Tue Oct 16, 2012 12:28 am

Re: how to extract single Folder / File from .tar.gz

Post by zaji_nms »

great its working fine

hopefully now last question in this thread...sorry to bother u....but may be useful for some time some user

its showing full path, how to do some trick with awk/sed to....

list only folders with PATH
list only folders without PATH
list only files without FULL PATH

Regards
User avatar
vtrac
Posts: 903
Joined: Tue Oct 27, 2020 1:35 pm

Re: how to extract single Folder / File from .tar.gz

Post by vtrac »

Here are couple examples:
list only folders with PATH (sort uniq):

Code: Select all

tar -zttf filename.tar.gz | awk '{print $6}' | sed  's/\(.*\/\)\(.*$\)/\1/g' | sort -u
list only files without FULL PATH:

Code: Select all

tar -zttf filename.tar.gz | awk '{print $6}' | sed  's/.*\///g'
Best Regards,
Vinh
Locked