How to Delete the Old Contents of a Zip File When Making a New Zip - linux

Currently I'm using the following command to zip all my folders and files with the exception of a few.
zip -r Archive.zip . -x '*.git*' '*.DS_Store*' '*.pyc*'
The problem with this command is it doesn't delete files in the older zip file that I deleted.
So I know that I can just run the rm command on the zip file before making a new one. However, I wanted to see if there's a way I can essentially combine the zip command above with an rm command.

You can delete a specific file from zip archive with -d flag
-d
--delete
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/\* \*.o

Related

Zip and Tar compress entire top directory and not (sub)directory

I'm trying to only zip the directory where I am exactly, this is part of a bigger bash script so I need to cd into the directory where I want to extract files and then exit.
However, using either tar or zip, the entire top directory path is recreated and not just the subdirectory that I'm interested in.
I get the following error:
zip warning: name not matched: $PWD/*
What I'm trying to do:
#Sub Directory and contents will be compressed
cd "$Sub_Dir"
Zipped_Files=$(basename "$Sub_Dir")
zip -r "$Zipped_Files".zip "$PWD/*"
#or
zip -j "$Zipped_Files".zip "$PWD/*"
#or
#tar -zcf "$Zipped_Files".zip "$PWD"
echo "Files have been compressed"
You have already cd into the sub dir, zip -r "$Zipped_Files".zip ./* should work.

Unzip wrongly: All files are scattered in the current directory

On CentOS, I wanted to unzip files in A.zip into ./A/. However, I didn't notice that there were hundreds of files in A.zip and I just use unzip A.zip. So now these extra files are all in the current directory. How could I solve this problem?
Thank you very much for any help!
You can try this -
unzip -Z1 is the zip info mode which basically returns the files which were zipped. The output is then piped onto other command which removes that file based on the input(from the previous command).
Assuming, first you take a proper backup of that folder.
unzip -Z1 t1.zip | xargs rm -f
If the zip files has folders inside of it then
unzip -Z1 t1.zip | xargs rm -rf
t1.zip is the zip file which I tested with.

How to zip a folder, but minus the files using zip command line

I am creating a zip file of my application tree, minus folders that have temporary files. For now I exclude the folders using -x option and manually created them with unzip. Is there a way with zip to exclude the files but include the folder (i.e. it would be an empty folder in the zip file?)
I am using
zip -r zipfile.zip . -x appsessions/\* workfolder/\*
but of course it excludes the folders and files in them. I would like to keep appsessions/ and workfolder/ in the zip file, but empty.
Give a try to this:
find . -type d -print | zip name.zip -#
Also check this answer: https://stackoverflow.com/a/13234936/1135424

Extract zip files contents and rename the directory dynamically

I have an application zip file created using Play Framework. It create the zip file with name A-1.0.zip. This zip file contains the directory with name A-1.0. (1.0 changes according to the version)
I wanted to extract the zip file and rename the folder from A-1.0 to A. So that my application init.d script finds the directory to start the application. This shuld be done dynamically using shell script.
Is there a way where i can extract all the zip files into A folder instead of extracting into A-1.0 and renaming?? Please help!
The following is what I tried....
unzip A-1.0.zip -d ~/A
(I know that it is very dumb of me to do this !!)
This extracted the file into ~/A/A-1.0/[contents]
I need to extract all the [contents] into ~/A instead of ~/A/A-1.0/. I dunno how to do this using command line.....
My init.d script searched for ~/A/bin/A -Dhttp.port=6565 -Dconfig.file=~/A/conf/application.conf to start the Play! application.
To make this script working, I extract all into A-1.0/ then I rename with mv ~/A-1.0 ~/A manually.
I didn't find any specific unzip option to perform this automatically, but managed to achieve this goal by creating a temporary symbolic link in order to artificially redirect the extracted files this way
ln -s A A-1.0
unzip A-1.0.zip
rm A-1.0
From the unzip man page it boils down to:
unzip A-1.0.zip 'A-1.0/*' -d /the/output/dir
^ ^
| |
| +- files to extract (note the quotes: unzip shall parse the wildcard instd of sh)
+- The archive
EDIT: This answer does not preserve subdirectories. It works fine if one doesn't have or need the subdirectory structure.
I found that you can combine the answer from #géza-török with the -j option mentioned by #david-c-rankin (in the comment below the question). Which leads to unzip -j A-1.0.zip 'A-1.0/*' -d /the/output/dir. That would only process the files inside A-1.0/ and output them straight into the given output directory.
Source: https://linux.die.net/man/1/unzip (look at -j)
I was looking to unzip all .zip files in the current directory into directories with names of the zip files (even after you rename them)
the following command is not elegant but works
cd to/the/dir
find *.zip | cut -d. -f1 | xargs -I % sh -c "unzip %.zip -d %; ls % | xargs -I # sh -c 'mv %/#/* %; rm -rf %/#'"

Zip including hidden files

In Linux I can zip all(except hidden files) in current directory by doing:
zip 1.zip *
But how do I include the hidden files?
EDIT: The correct way is zip -r 1.zip .
The commands shown in my previous answer below are incorrect because they also include the parent directory.
Have you tried this:
zip yourfile.zip sourcedir/* .*
or you in your case
zip 1.zip * .[^.]*
It should include all hidden files also.
Or you can add more simple
zip 1.zip ./
Just to be sure it is not forgotten since this is a forum for developers and a good number of us use git.
An easy way to get only what you want in the zip is to use git archive -o filename.zip branch
If you want to zip all files (+hidden files)
Kindly using: zip -r namefiles.zip .
The "." is all files in folder.
zip -r namefiles.zip "folder will zip"
On macOS 10.15.7 I had to separatelly add all dot leading files (\.*) and rest of the files (*):
zip -r file.zip \.* *
if you don't have rights to save zip file in current dir you can go to dir where you have rights and type
zip -r 1.zip /path/to/source/dir/.
However when if in .../some_dir you type
unzip 1.zip
then your files will be decompress into .../some_dir/path/to/source/dir/
zip -r 1.zip .* -x "../*"
Just doing zip -r 1.zip .* will include the parent folder as well so the trick is to exclude the parent folder using -x "../*"
If you'd like to save some subdirectory of the current directory recursively with hidden and regular files just type
$ zip -r backup_subdirectory.zip backup_subdirectory/. backup-subdirectory/*
And for unzipping:
$ unzip backup_subdirectory.zip
Or even simpler by using tar for creating an archive:
$ tar czvf backup_subdirectory.tar.gz backup_subdirectory/
And for extracting all files from the archive:
$ tar xzvf backup_subdirectory.tar.gz

Resources