Help with creating recursive unzip script

Hello. I am trying to create a script that recursively looks at my folder structure, and extracts any .7z files that have not already been extracted yet. I know that all .7z files only have a single .txt file in them, and the only .txt file in any directory would be from that .7z file.

I am using 7-zip to extract all of the .7z files.

Here is an example directory structure.

./Folder/
./Folder/Subfolder 1/file A.7z
./Folder/Subfolder 1/file A.txt
./Folder/Subfolder 2/file B.7z

In this example, you can see that within Subfolder 2, the .7z file has not been unzipped, while it has within Subfolder 1. The Folder names and 7z names are variable, along with the depth of the directories.

Here's what I have so far, but I think I'm on the wrong track. So far I have a script that lists all folders that include a .7z file, but I can't figure out the rest. If you have a more efficient solution, I am completely open to it.

find . -type d | while read FOLDER
	do
		if ls -A "$FOLDER" | grep .7z > /dev/null
		then
			echo "$FOLDER"
		fi
	done

Any thoughts??

Try something this..

find /pathname/ -type f -name *.7z | while read folder
do 
echo $folder
done

Pamu - That gets me about halfway where I want to be. This is definitely more efficient, so thanks!

The goal is to look into each folder with a .7z file. If that folder with the .7z also includes a .txt file, skip it. If it does not, then unzip the .7z file (which only includes a single .txt file) into that same folder.

Try something this...

find /pathname/ -type f -name *.7z |  while read folder
do 
fold_path=$(echo "$folder" | awk -F "/" '{$NF=""}1' OFS=/ )    #Get the folder path which has .gz files.

if [[ -e "$fold_path*.txt" ]]   #here we check for the presence of .txt files
then
echo ".txt file is present"
else
echo "do your unzip operations" #add your unzip code for $folder - which .gz file
fi
done

I hope this helps you...:slight_smile:

1 Like