LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   wants to extract the tar.gz files (https://www.linuxquestions.org/questions/linux-desktop-74/wants-to-extract-the-tar-gz-files-927388/)

rajini23 02-03-2012 06:17 AM

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..

MTK358 02-03-2012 07:40 AM

Are all of these directories with .gz files in them under one common directory, or are they just randomly spread around?

tronayne 02-03-2012 07:47 AM

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.

rajini23 02-05-2012 10:24 PM

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...

tronayne 02-06-2012 06:49 AM

Quote:

Originally Posted by rajini23 (Post 4594753)
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.

rajini23 02-07-2012 06:08 AM

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


Thanks you very much..:) :)

b0uncer 02-07-2012 07:54 AM

Quote:

Originally Posted by tronayne (Post 4592842)
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.

rajini23 02-09-2012 04:36 AM

Quote:

Originally Posted by b0uncer (Post 4595933)
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...

tronayne 02-09-2012 11:32 AM

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.


All times are GMT -5. The time now is 06:12 AM.