I have many zip files (think 100-150) in a folder. Each zip file has multiple files with different file extensions. I know how to write a bash for loop to unzip all the contents of these files.
What I want to do is this.... Use 7z (or some other) to unzip each zip file and given the contents of that zip file the same file name as the zip file.
This is what I have currently.
#!/bin/bash
for i in *.zip;
do
echo $i #For debugging purpose
7z x $i &
done
Edit 2:
#!/bin/bash
for i in *.zip;
do
fbname=$(basename "$i" .zip);
fem_fileName=$(unzip -l $i | grep .fem | awk '{print $4}')
echo $fbname
echo $fem_fileName
$unzip $i
7z e $i *.fem -y
#echo $fbname
#echo $fem_fileName
mv $fem_fileName $fbname
done
The newest issue is: what if the zip file I am operating on has multiple sub-directories? How do I have 7z or other utility recursively check for "folder in folder in zip file"?
Zip_file:
|----Folder_1
|------------Folder_2
|--------------------Contents_to_extract
Contents_to_extract > change file name to > zip_file
I don't think
7z
has a way to rename files while extracting (like, say,tar
does). You can, instead, extract to a folder, and rename everything in that folder to match the filename:I found a solution. It was catalyzed by the small comment discussion I had with @muru.
Please read the comments carefully in the bash script below.
For the moment, this works fine. There could be logical issues within the zip files being looked into... I have not encountered them YET.