Rename the files as they are extracted from the zip file as the name of the zip file itself
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:
#! /bin/bash
for i in *.zip;
do
echo $i # For debugging
filename="${i%.*}" # get filename without .zip
(
7z x -o"$filename" "$i"; # extract to directory named after zip file
cd "$filename"
shopt -s globstar
for i in "$filename"/**; do
# move everything in directory to parent folder with new name
[[ -f $i ]] || continue # only files
mv "${i}" ../"${filename}.${i##*.}" # but keep extension
done
cd ..; rm -r "$filename" # cleanup
)&
done
Related videos on Youtube
dearN
Updated on September 18, 2022Comments
-
dearN over 1 year
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
-
Terrance over 6 yearsDo you want to rename the files as they are extracted from the zip file as the name of the zip file itself?
-
dearN over 6 years@Terrance Yes. That is the case. excuse my english. I want all files in each zip file to be extracted and have the same name as the zip file itself.
-
-
dearN over 6 yearsI'll try this out. Currently, I updated my Q with my nouvelle attempt. However, what if the zip file itself has multiple folders within?
-
muru over 6 years@drN Add an example zip file structure and what you want at the end of it to the question.
-
dearN over 6 yearsadded a complex file structure to my question. tjhanks.
-
muru over 6 years@drN "and what you want at the end of it" as well
-
dearN over 6 yearsdone to the best of my english.
-
dearN over 6 yearsThank you for your input and disccussion. I found a solution. It is below.
-
muru over 6 years@drN see updated script