Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Uncompress several tar.gz files inside several folders


👤 Login to reply

    #1  
Old 1 Week Ago
shellX shellX is offline
Registered User
 
Join Date: Jul 2018
Last Activity: 13 July 2018, 3:54 AM EDT
Location: Paris - France
Posts: 5
Thanks: 1
Thanked 2 Times in 1 Post
Uncompress several tar.gz files inside several folders

Hi,

I need to uncompress sevral tar.gz files that are located insides sevral folders using a shell script.


Folder tree looks like this :
Code:
/folder/001
/folder/002
/folder/003

Inside each folder dossier (001,002,003) we can find 1 or several tar.gz files.


I've scripted something like this :
Code:
!/bin/bash
liste_fichier="/folder/*"
for fichier in $liste_fichier do
tar -xzvf $fichier/*.tar.gz --directory $fichier/
done
exit

Issue:
This scripts only works with one tar.gz file inside the folder.
When it finds more then one tar.gz file it crashes the message tar : <path/file.tar.gz> : not found in archive


Some other tests:
-I've tested the code below inside one folder(001) containing several tar.gz.file :
Code:
for i in *.tar.gz 
do 
tar -xzvf $i 
done

It works.


I try to run it inside my script without success. To many loops I guess

Any "good" ideas are welcome.
Thank you

Last edited by shellX; 1 Week Ago at 10:08 AM.. Reason: Added CODE & ICODE tags
Sponsored Links
    #2  
Old 1 Week Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2018, 11:57 AM EDT
Location: Lancashire, UK
Posts: 3,588
Thanks: 1,594
Thanked 706 Times in 633 Posts
Wold you be better with the find command? You can ask it to locate files of the name format you need and call a command for each one found.

Perhaps this will get you started:-
Code:
find /base/path/to/files -name "*.tar.gz" -print -exec tar -tzvf {} \;

Beware that if the files found contain full path archives, you might overwrite any file on your server. Additionally, if you are extracting, you might find that your current directory gets all the extracted files into it if they use relative paths. I have set the command to just list the contents for now so you can check what it will try to do.



I hope that this helps,
Robin
Sponsored Links
    #3  
Old 1 Week Ago
shellX shellX is offline
Registered User
 
Join Date: Jul 2018
Last Activity: 13 July 2018, 3:54 AM EDT
Location: Paris - France
Posts: 5
Thanks: 1
Thanked 2 Times in 1 Post
Hi,

That's the point, I must extract the file in the same folders.
Besides it doesn't work.
eRR mEssage : missing parameters for "-exec"

Last edited by shellX; 1 Week Ago at 10:52 AM..
    #4  
Old 1 Week Ago
Peasant's Unix or Linux Image
Peasant Peasant is offline Forum Advisor  
Registered User
 
Join Date: Mar 2011
Last Activity: 21 July 2018, 6:52 AM EDT
Posts: 1,197
Thanks: 34
Thanked 370 Times in 318 Posts
What is your operating system and shell used ?
Gnu find printf option can be of help if you operating system supports it.

Following should fill you requirement regardless of the paths used in archives (absolute or relative).

Code:
find /folder/00[1-3] -name '*.tar.gz' -printf 'tar -xvzf %h/%f -C %h\n' | sh

You can match folders with regex or input them along each other.
Code:
find /folder001 /folder002 /folder003 -name '*.tar.gz' -printf 'tar -xvzf %h/%f -C %h\n' | sh

More portable solution could include a awk program which will generate those commands instead of gnu find printf or some xargs magic possibly.
Be sure to get back, if you do not have gnu find.


Hope that helps.
Regards
Peasant.
Sponsored Links
    #5  
Old 1 Week Ago
shellX shellX is offline
Registered User
 
Join Date: Jul 2018
Last Activity: 13 July 2018, 3:54 AM EDT
Location: Paris - France
Posts: 5
Thanks: 1
Thanked 2 Times in 1 Post
@PEASANT

Hi,

This solution would be great for a small number of folders.
But unfortunnatelly "for me" I've something like 1000 sub-folders with tar.gz files to uncompress.

I cannot type the names of 1000 folders and subfolders in a script.

That's why my idea in the beggining was to accept a variable "$1"to store the path to the folders containing the tar.gz files
This way the script could be executed automatically for each folder,
(e.g.) below

Code:
!/bin/bash 
liste_fichier="/folder/$1/*" 
for fichier in $liste_fichier do 
tar -xzvf $fichier/*.tar.gz --directory $fichier/ 
done 
exit

Moderator's Comments:
Uncompress several tar.gz files inside several folders
Please wrap all code, files, input & output/errors in CODE tags (or ICODE tags for in-line snippets)
It makes it far easier to read and preservers spaces for indenting and/or fixed-width data.

Last edited by rbatte1; 1 Week Ago at 11:53 AM.. Reason: Added CODE tags
Sponsored Links
    #6  
Old 1 Week Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 20 July 2018, 11:57 AM EDT
Location: Lancashire, UK
Posts: 3,588
Thanks: 1,594
Thanked 706 Times in 633 Posts
Assuming that your code actually starts #!/bin/bash then you can actually simplify this to:-
Code:
#!/bin/bash

[ -z "$1" ] && echo "On doit donner un dossier." && exit

for ficher in /folder/$1/*
do
   echo ; echo "$ficher" ; sleep 1                                 # Remove this if you like, it is just to illustrate the loop
   tar -xzvf $fichier/*.tar.gz --directory $fichier/
done

The shell will expand the directory name with wildcard to make a long list for your for loop.


I've assumed French language for output from your use of ficher. Apologies if that is incorrect.



Does that help?
Robin

Last edited by rbatte1; 1 Week Ago at 12:01 PM.. Reason: Added a debug line to show progress through the file list
Sponsored Links
    #7  
Old 1 Week Ago
shellX shellX is offline
Registered User
 
Join Date: Jul 2018
Last Activity: 13 July 2018, 3:54 AM EDT
Location: Paris - France
Posts: 5
Thanks: 1
Thanked 2 Times in 1 Post
Hi Robin

That code is very good.

The issue is it doesn't work when there are more then one tar.gz file in the folder.

--> Lets suposse $1 = folder
--> The content of my folder "folder" is
Code:
[shellx] $ ls -l folder
total 16
drwxr-xr-x 2 shellx mrv 4096 11 juil. 14:36 001
drwxr-xr-x 2 shellx mrv 4096 11 juil. 14:37 002
drwxr-xr-x 2 shellx mrv 4096 11 juil. 14:37 003
drwxr-xr-x 2 shellx mrv 4096 11 juil. 14:34 004

--> sub-folders (001, 002, 003, 004) content
Code:
[shellx] $ ls  folder/*
folder/001:
CARDV022A0.tar.gz  CARDV02976.tar.gz  CARDV02A0.tar.gz  CARDV976.tar.gz

folder/002:
CARDV02976.tar.gz  CARDV976.tar.gz

folder/003:
CARDV022A0.tar.gz  CARDV02976.tar.gz  CARDV976.tar.gz

folder/004:
CARDV02976.tar.gz  CARDV976.tar.gz



-->I've set 2 loops in the new script

Code:
#!/bin/bash
liste_rep="folder/*"
folder="$(ls folder)"
echo "LISTE REP :" $liste_rep
        for rep in "$liste_rep"
        do
                echo "AFFICHE REP :"$rep
                #for fichier in "$(ls /folder/*)" # test
                for fichier in $liste_rep/*.tar.gz
                do
                        #cd $liste_rep
                        echo "FICHIER :" $fichier
                        tar -zxvf $fichier   --directory $liste_rep/
                done
        done
exit


-->Variables content verified
Code:
[presta2] $ echo $liste_rep
folder/001 folder/002 folder/003 folder/004
-(jeu. juil. 12 10:13:41)--(mrvpgsa001:~)-
[presta2] $ echo $folder
001 002 003 004


Resultat d'execution
Code:
LISTE REP : folder/001 folder/002 folder/003 folder/004  --->> folder liste correct
AFFICHE REP :folder/*  --->> because $rep = "$list_rep"??? and should be $list_rep without " " ?
FICHIER : folder/001/CARDV022A0.tar.gz  --> Var fichier is correctly set
tar: folder/002 : non trouvé dans l'archive ---> How is it ? if  --> tar -zxvf $fichier
tar: folder/003 : non trouvé dans l'archive
tar: folder/004 : non trouvé dans l'archive
tar: Arrêt avec code d'échec à cause des erreurs précédentes
FICHIER : folder/001/CARDV02976.tar.gz
tar: folder/002 : non trouvé dans l'archive
tar: folder/003 : non trouvé dans l'archive
tar: folder/004 : non trouvé dans l'archive
tar: Arrêt avec code d'échec à cause des erreurs précédentes
FICHIER : folder/001/CARDV02A0.tar.gz
tar: folder/002 : non trouvé dans l'archive
tar: folder/003 : non trouvé dans l'archive
tar: folder/004 : non trouvé dans l'archive
tar: Arrêt avec code d'échec à cause des erreurs précédentes
FICHIER : folder/001/CARDV976.tar.gz

--> If uncomment line "#cd $liste_rep" inside second loop
--> we gett error message below

Code:
[presta2] $ ./decomp2.sh
LISTE REP : folder/001 folder/002 folder/003 folder/004
AFFICHE REP :folder/*
FICHIER : folder/001/CARDV022A0.tar.gz
tar (child): folder/001/CARDV022A0.tar.gz : la fonction open a échoué: Aucun fichier ou dossier de ce type
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
FICHIER : folder/001/CARDV02976.tar.gz
tar (child): folder/001/CARDV02976.tar.gz : la fonction open a échoué: Aucun fichier ou dossier de ce type
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now


Any ideas are welcome




Moderator's Comments:
Uncompress several tar.gz files inside several folders Please use CODE tags as required by forum rules!

Last edited by RudiC; 1 Week Ago at 11:59 AM.. Reason: Added CODE tags.
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to delete all the files and folders inside all the directories except some specific directory? Little Shell Programming and Scripting 7 07-29-2015 06:02 PM
Editing all files inside folders and updating pos 88-94 with continuous numbering. lancesunny Shell Programming and Scripting 6 12-11-2012 02:55 PM
How to tar files inside a script? rogerben Shell Programming and Scripting 9 08-02-2012 03:18 PM
uncompress tar.gz files gaddamshashank Shell Programming and Scripting 5 07-08-2010 12:04 PM
Cannot uncompress the tar.gz sbox Solaris 2 02-08-2010 02:48 AM



All times are GMT -4. The time now is 07:49 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?