I need to uncompress sevral tar.gz files that are located insides sevral folders using a shell script.
Folder tree looks like this :
/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 :
!/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 :
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
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:-
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.
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.
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
!/bin/bash
liste_fichier="/folder/$1/*"
for fichier in $liste_fichier do
tar -xzvf $fichier/*.tar.gz --directory $fichier/
done
exit
Assuming that your code actually starts #!/bin/bash then you can actually simplify this to:-
#!/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.
#!/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
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
[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
Without fully understanding what going on here, the first thing to do would be drop the double quotes around $liste_rep in the first for loop, and replace $liste_rep with $rep in the second. Try that and report back.
---------- Post updated at 06:40 PM ---------- Previous update was at 06:35 PM ----------
Finally I found out the solution, Thank you Rudic
#!/bin/bash liste_rep=folder/*
echo "LISTE REP :" $liste_rep
for rep in $liste_rep
##$liste_rep without "" double quotes
do
echo "AFFICHE REP :"$rep
for fichier in $rep/*.tar.gz
### use $rep instead of $liste_rep > varibale $liste_rep just list all folders
### $rep shows folder/001 ... 003
do
echo "FICHIER :" $fichier
### $fichier shows complete path to tar.gz files in each sub-folder
tar -zxvf $fichier --directory $rep/
### -- directory uncompress inside -> $rep = folder/001 with '/' so folder/001/...003/
done
done
exit