Linux How to Add a File to a Specific Folder Within a Zip File

linux how to add a file to a specific folder within a zip file

If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:

zip -g xxx.zip folder/file

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.

How do I unzip a specific folder within a zipped file, and exclude some specific folder within it?

The exclude option works!

We just need to provide a path to the files to be excluded!


unzip main.zip "main_folder/folder2/*" -x "main_folder/folder2/*.git/*" -d path/to/some/directory

Create a dedicated folder for every zip files in a directory and extract zip files

"extract here" is merely a feature of whatever unzip wrapper you are using. unzip will only extract what actually is in the archive. There is probably no simpler way than a shell script. But sed, awk etc. are not needed for this if you have a POSIX-compliant shell:

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

(You MUST NOT escape the * or pathname expansion will not take place.) Be aware that if the ZIP archive contains a directory, such as with Eclipse archives (which always contain eclipse/), you would end up with ./eclipse*/eclipse/eclipse.ini in any case. Add echo before unzip for a dry run.

Loop through directory and zip specific folder without parent path

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


Related Topics



Leave a reply



Submit