LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 02-03-2012, 07:17 AM   #1
rajini23
Member
 
Registered: Sep 2011
Posts: 160

Rep: Reputation: Disabled
wants to extract the tar.gz files


hi,

have more than 50 folders in each folders i have some .gz files how do i extract all the fodler in same time...

Please suggest me any script kind of thing to execute .......in linux

i user CentOS..
 
Old 02-03-2012, 08:40 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Are all of these directories with .gz files in them under one common directory, or are they just randomly spread around?
 
Old 02-03-2012, 08:47 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062
All right, you extract from a gzipped tar archive with
Code:
tar xzf file_name
You can, if you want to see what's being extracted change xzf to xzvf.

Problem: if you just walk the tree and extract, they'll all extract into the directory you're in rather than the directory where they're found; probably don't want to do that.

So you need to use find (to find them) and cd (to get to the directory they're in and some other stuff:
Code:
#!/bin/bash
#
#       this isn't necessary by it doesn't hurt
WD=${PWD}
#       change "path" below to wherever
for file in $(find path -name '*.tar.gz')
do
        #       we need the name of the directory
        DIR=$(dirname ${file})
        #       cd there
        cd ${DIR}
        #       extrace the archive
        tar xzf ${file}
        #       and cd back to where we started
        cd ${WD}
done
Hope this helps some.

Last edited by tronayne; 02-03-2012 at 08:49 AM.
 
Old 02-05-2012, 11:24 PM   #4
rajini23
Member
 
Registered: Sep 2011
Posts: 160

Original Poster
Rep: Reputation: Disabled
Thanks For your update, my query is all the .gz files are there in different directories totally i have 50 dir on each directory i have 4 .gz files wants to extract those files...

Can i able to use the above script to extract those files...
 
Old 02-06-2012, 07:49 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062
Quote:
Originally Posted by rajini23 View Post
Can i able to use the above script to extract those files...
The shell program above will find all *.tar.gz files in a given tree and extract their content in place.

You need to change the word path in the following line to the parent directory of the directories where your stuff is stored or to a dot ( . ) (current directory) and cd some_directory_name to execute the program.
Code:
for file in $(find path -name '*.tar.gz')
When you do execute it, it's going to walk the entire tree below where you start from and extract files from every *.tar.gz archive it finds in place -- the extracted directories and files will be in the directory where the *.tar.gz file is found.

As your indicate, you've got 50 directories with who-knows-how-many compressed tar archives in them. You need to tell the shell program where to start looking somehow or other. That can be either the dot or an absolute path, but I'd use the dot and just cd to the directory tree I'm interested in.

Hope this helps some.
 
Old 02-07-2012, 07:08 AM   #6
rajini23
Member
 
Registered: Sep 2011
Posts: 160

Original Poster
Rep: Reputation: Disabled
Smile

Thanks a lot for your update ,i can able to extract all the 50 folders in my desktop....


Thanks you very much..
 
Old 02-07-2012, 08:54 AM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Quote:
Originally Posted by tronayne View Post
Code:
#!/bin/bash
#
#       this isn't necessary by it doesn't hurt
WD=${PWD}
#       change "path" below to wherever
for file in $(find path -name '*.tar.gz')
do
        #       we need the name of the directory
        DIR=$(dirname ${file})
        #       cd there
        cd ${DIR}
        #       extrace the archive
        tar xzf ${file}
        #       and cd back to where we started
        cd ${WD}
done
This kind of approach might do if you need to do fancy stuff per each found item in which the shell can help, but if the goal is to find and extract some gzipped tar archives, find can do it without relying on the shell's "loops" too, for example
Code:
find /search/path -name "*.tar.gz" -exec tar -xzf '{}' \;
To change to a specific directory, one could use the -C option to tar, for example. There are several ways around this, but for a simple case it's best to use a simple solution.
 
Old 02-09-2012, 05:36 AM   #8
rajini23
Member
 
Registered: Sep 2011
Posts: 160

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by b0uncer View Post
This kind of approach might do if you need to do fancy stuff per each found item in which the shell can help, but if the goal is to find and extract some gzipped tar archives, find can do it without relying on the shell's "loops" too, for example
Code:
find /search/path -name "*.tar.gz" -exec tar -xzf '{}' \;
To change to a specific directory, one could use the -C option to tar, for example. There are several ways around this, but for a simple case it's best to use a simple solution.
Thanks for your timely help i got work well. please clarify in shell script after extracting all the .tar.gz files can be moved to any specific directory...
 
Old 02-09-2012, 12:32 PM   #9
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062Reputation: 1062
Maybe this will help a little.

Lets say you have three directories in /usr/local; /usr/local/dirA, /usr/local/dirB and /usr/local/dirC. You want to create three separate tar archives of the content of those directories.

If you're working in your home directory, say, /home/yours and do this
Code:
/home/yours: tar czf dirA.tar.gz /usr/local/dirA
/home/yours: tar czf dirB.tar.gz /usr/local/dirB
/home/yours: tar czf dirC.tar.gz /usr/local/dirC
You would wind up with
Code:
/home/yours: ls *.tar.gz
dirA.tar.gz dirB.tar.gz dirC.tar.gz
The conents of each of those would have the leading / stripped off and, for example, dirA.tar.gz's contents would look like
Code:
usr/local/libA/file.name
You can retain the leading / with the -P option (so czPf rather than czf).

That's all well and good if you want to restore an archive to it's original location; however, if you want to extract the contents of the archive somewhere else, it's better to
Code:
cd /usr/local
tar czf dirA.tar.gz dirA
tar czf dirB.tar.gz dirB
tar czf dirC.tar.gz dirC
so you can change directory to wherever you want (and, you know, can write to) and extract the content of each archive; the directories (dirA, dirB and dirC) will be created when tar extracts. Note, too, that you do not want the P in this case.

It's generally easier to do things a step at time rather than all in one go if for no other reason than you have a chance to think about what you're trying to, eh?

Hope this helps some.

Last edited by tronayne; 02-09-2012 at 12:33 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Extract from `tar` archive as ordered in --files-from zx_ Linux - Newbie 6 02-04-2012 10:08 AM
Extract .tar.bz2 files cahardin76 Linux - Newbie 3 11-13-2010 11:35 PM
how to extract tar.gz files from user account on fedora linux veereddy Linux - Software 1 01-04-2009 11:22 PM
How can I extract only *.c files from a tar.gz? [KIA]aze Linux - Newbie 7 06-06-2007 05:39 AM
How to extract tar.bz2 files? akihandyman Linux - Newbie 3 12-21-2003 04:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 03:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration