Unix unzip: how to batch unzip zip files in a folder and save in subfolders? - linux

Say if I have a folder 'images' and inside it there are 0001.zip to 9999.zip, I want to unzip all of them and save them in subfolder which has their file name, for example, 0001.zip will be unzipped and saved to /0001, 0002.zip will be unzipped and saved to /0002, I tried to do
unzip '*.zip'
but that extracts all files in current folder.

You could do something like this:
for file in *.zip; do
dir=$(basename "$file" .zip) # remove the .zip from the filename
mkdir "$dir"
cd "$dir" && unzip ../"$file" && rm ../"$file" # unzip and remove file if successful
cd ..
done
or, run it together on one line:
for file in *.zip; do dir=$(basename "$file" .zip); mkdir "$dir"; cd "$dir"; unzip ../"$file" && rm ../"$file"; cd ..; done
If you need/want to keep the original .zip files, just remove the && rm ../"$file" bit.

for zip in *.zip
do
unzip "$zip" -d "${zip%.zip}"
done

Related

Loop through directory and zip specific folder without parent path

I'm trying to make a bash script to loop through all folder in directories, and individually zip just the folder I want without all path and choose where to zip theme.
#!/bin/bash
for dir in /MyPersonalFolder/*/*/WhatIWantFolder
do
folder_number=$(basename ${dir%/*}) ### basename get the name of this folder [*] 'folder have numbers' /MyPersonalFolder/*/[*]/WhatIWantFolder
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
mkdir -p ./backup-theme/ && sudo mv "${folder_number}-theme".tar.gz $_ ### I use this to move zipped folder to specific directory if i can choose where to zip file in the zip command line it's better
done
I can zip the folder I want, but the output zip file comes with this content:
/MyPersonalFolder/0001/0001/WhatIWantFolder
But what I need is to output the file with this path:
0001/
|___WhatIWantFolder/
I tried to change "$dir" in this line
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "$dir"
with basename ${dir%/*}
sudo tar -cf "${folder_number}-WhatIWantFolder".tar.gz --absolute-names "basename ${dir%/*}"
tar not found the folder it's come with errors
tar: e0001: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
You can cd to another directory while running tar using the --cd option.
#!/bin/bash
for dir in MyPersonalFolder/*/*/WhatIWantFolder; do
parent="${dir%/*/*}"
subdir="${dir#*/*/*}"
outfile="backup-theme/${subdir/\//-}.tar.gz"
tar --cd "$parent" -cvzf "$outfile" "$subdir"
done
The directory structure of the tar file would look something like this:
tar -tf backup-theme/0001-WhatIWantFolder.tar.gz
0001/WhatIWantFolder/
0001/WhatIWantFolder/f1

Zipping contents of folder includes parent directories

zip -r $packageName.zip /home/ubuntu/backend/upgrade/temp
I expect $packageName.zip to have all the contents in temp folder. However, I'm getting /home/ubuntu/backend/upgrade/contents in my zip folder.
I've tried
pushd /home/ubuntu/backend/upgrade/temp
zip -j /home/ubuntu/backend/test.zip ./*
popd
and
pushd /home/ubuntu/backend/upgrade/temp
zip -r /home/ubuntu/backend/test.zip ./*
popd
This should work:
zip -rj $packageName.zip /home/ubuntu/backend/upgrade/temp
You need the recursive portion of the command combined with the relative paths.

Extracting a .tar file and creating a filelist

i have this little code here
for file in *.tar.gz;
do tar xzvf "${file}" && rm "${file}";
done
It extracts a tar.gz and deletes it. Now I have to create a filelist file (.fl) named like a substring from the .tar . For example, I have to delete the first 5 letters and the last 5 (the extension) from the name of the .tar.gz . And that for every .tar.gz that I extract.
Example:
I have a ABC_A.tar.gz with a ABC_A.xml in it.
I have to make a A.fl
and in that A.fl i have to write ABC_A.xml
Thanks in advance.
In your loop, you can do the following for each file:
# delete first five characters
name=${file:5}
# delete .tar.gz suffix
name=${file%%.tar.gz}
Use
for file in *.tar.gz
do
# this creates the file list into a .fl file:
tar tfz "${file}" > "${file:5:-5}.fl"
# this extracts and afterwards removes the tar archive:
tar xzvf "${file}" && rm "${file}"
done
You also can combine the two in one step:
for file in *.tar.gz
do
tar xzvf "${file}" > "${file:5:-5}.fl" && rm "${file}"
done

linux shell tar unwanted extra directories

I have the following problem:
I have directorties a/b/c and inside c many text files.
I want to make a .tar.gz file in drectory a/mydir with the c directory inside and then unzip it to that same directory to create a/mydir/c (with all the files inside)
I am at directory a and run: (shell)
~:$ tar -czf mydir/output.tar.gz b/c
~:$ tar -zxf mydir/output.tar.gz -c mydir
but the result is directories a/mydir/b/c (with the files inside)
The problem is I don't want directory b in the middle, just c with all its contents
This works for me. Create data
mkdir -p a/b/c
echo 42 > a/b/c/file.dat
Archive
tar zc -f c.tar.gz -C a/b c
created a/b/c directories, from directory a kindly try this command.
so the file under b/c/files were done out.tar.gz
new directory "mydir" create under "b" and files extracted too.
out.tar.gz removed from "a".
# tar -cvzf out.tar.gz b/c/* ; mkdir -p b/mydir ; tar -xvzf out.tar.gz -C b/mydir/ ; rm -rf out.tar.gz
Thanks!

Zip stating absolute paths, but only keeping part of them

zip -r 1.zip /home/username/the_folder
At here, when i unzip 1.zip, it will create /home/username/the_folder, from whichever folder i am unzipping from.
How do I zip, stating the full absolute paths, but make the zip only contain the folder structure starting at, in this case for instance, /home/username?
That way I could be at whatever path i wanted, unzip and it would just create the_folder, and not /home/username/the_folder.
Use this command:
cd path_under_folder_to_zip && \
zip -r 1.zip folder_to_zip >/dev/null && \
mv 1.zip my_current_path
Use relative path when specifying the file to zip.
cd /home/username
zip -r 1.zip ./the_folder
Then when you unzip, it'll be a relative path starting at whichever folder you're in when unzipping.
Just use the -j option, works on OSX, I don't know about linux.
zip -j -r 1.zip /home/username/the_folder
List item
How about this:
function zipExtraFolder {
if [ $# -lt 2 ]; then
echo "provide at least two arguments"
return
fi
folder=$2
mkdir del
echo cp -r `dirname $folder` del
cd del
echo zip -r ../$1 .
cd -
rm -rf del
}
Define above as a shell function in your .bashrc and you should be able to use it whenever you want.
The usage will be like below.
zipExtraFolder 1.zip /home/username/the_folder

Resources