zip a list of files at command-line linux - zip

If I have a list of files I want to zip, how can I pass the list to zip?
cookbook/application/views/index.php
cookbook/application/controller/index.php
cookbook/js/index.js
....
cookbook/css/index.css
To do the above list one by one at the command-line would be like zip -r my.zip cookbook/css/index.css, where my.zip is in the same root directory as cookbook

Try
zip -r# my.zip < listfile
The -# flag tells zip to read file names from stdin.

If all files are in the same folder, you don't need to type each file that you want to include in the archive. Just invoke the command and specify their common folder like this:
zip -r cookbook.zip cookbook
All files inside the cookbook directory will be included in the zip archive.

Related

How to use zip to generate a new archive but not just refresh files in the archive and add files into it, on linux?

Here, (on linux)
there is an existing archive named A.zip, which include File1 and File2:
A.zip:
File1
File2
and I run this command: zip A.zip File1 File3, then the archive A.zip becomes like:
A.zip:
File1
File2
File3
however, what I really want to get is a brand new archive A.zip! like:
A.zip:
File1
File3
I know it can be done by run rm A.zip and then run zip A.zip File1 File3, but it is not elegant and if I write these commands into a shell script so A.zip may not exist while the action to remove a non-existent file is not elegant as well.
Is there any options for me to get this done?
Use these option to works:
zip -FSr A.zip File1 File3
OPTIONS
-FS
Synchronize the contents of an archive with the files on the OS. Normally when an archive is updated, new files are added and changed files are updated but files that no longer exist on the
OS are not deleted from the archive. This option enables a new mode that checks entries in the archive against the file system. If the file time and file size of the entry matches that of
the OS file, the entry is copied from the old archive instead of being read from the file system and compressed. If the OS file has changed, the entry is read and compressed as usual. If
the entry in the archive does not match a file on the OS, the entry is deleted. Enabling this option should create archives that are the same as new archives, but since existing entries are
copied instead of compressed, updating an existing archive with -FS can be much faster than creating a new archive. Also consider using -u for updating an archive.
For this option to work, the archive should be updated from the same directory it was created in so the relative paths match. If few files are being copied from the old archive, it may be
faster to create a new archive instead.
Note that the timezone environment variable TZ should be set according to the local timezone in order for this option to work correctly. A change in timezone since the original archive was
created could result in no times matching and recompression of all files.
This option deletes files from the archive. If you need to preserve the original archive, make a copy of the archive first or use the --out option to output the updated archive to a new
file. Even though it may be slower, creating a new archive with a new archive name is safer, avoids mismatches between archive and OS paths, and is preferred.
-r
Travel the directory structure recursively; for example:
zip -r foo.zip foo
or more concisely
zip -r foo foo
In this case, all the files and directories in foo are saved in a zip archive named foo.zip, including files with names starting with ".", since the recursion does not use the shell's file-
name substitution mechanism. If you wish to include only a specific subset of the files in directory foo and its subdirectories, use the -i option to specify the pattern of files to be in‐
cluded. You should not use -r with the name ".*", since that matches ".." which will attempt to zip up the parent directory (probably not what was intended).
Multiple source directories are allowed as in
zip -r foo foo1 foo2
which first zips up foo1 and then foo2, going down each directory.
Note that while wildcards to -r are typically resolved while recursing down directories in the file system, any -R, -x, and -i wildcards are applied to internal archive pathnames once the di‐
rectories are scanned. To have wildcards apply to files in subdirectories when recursing on Unix and similar systems where the shell does wildcard substitution, either escape all wildcards
or put all arguments with wildcards in quotes. This lets zip see the wildcards and match files in subdirectories using them as it recurses.

how to add folder to subfolder inside existing zip file

I want to achieve the following (simple) task, but I don't know how...
I have a zip file like this, only containing some folders
dummy.zip:
/my/dummy/folder/stucture
how can I add folders to this dummy.zip file that the newly added files and dirs are located under "/my/dummy/folder/stucture" using the command line (linux)?
dummy.zip should look like this afterwards:
/my/dummy/folder/stucture/my/new/Dirs
I've made a screenshot to better illustrate what I mean
To append "archive" to an existing zip file you could use option -r:
zip -r9 dummy.zip dirs
You could crate your zip:
$ zip -9 dummy.zip file
And later you could add a full dir:
$ zip -r9 dummy.zip dirs
Or contents of the dir on the same root:
$ cd dirs
$ zip -r9 dummy.zip *
The -9 is the compression level, in this case, the maximum.
If you have the original folder which you generated the first zip from, you can add the folder/files you want and then use the option -u from the zip command.
This option will update the zip with the newly added folders and files, you can use it like:
$> mkdir /my/dummy/folder/stucture/my/new/Dirs
$> zip -u dummy.zip /my/dummy

unzip in current directory while preserving file structure

I'm in a directory and I have a zip containing files and directories.
I need to unzip that file, into current directory, but preserving the file structure.
unzip myfile.zip will create a myfile directory in current directory which is not what I want.
unzip -j myfile.zip will kill all the file strucure, which is not what I want.
unzip myfile.zip extracts files in the working directory by keeping path names from the zip file.
So if you get a subdirectory myfile it means it is part of the relative path of compressed files. Check it by listing the zip content
unzip -l myfile.zip
So you can unzip the file from the directory above, or, from the target directory unzip with -d option, where -d is the directory above
cd myfile
unzip myfile.zip -d ..
Dont select the folder while zipping.
For example
myfile/abc.txt and myfile/efg.txt
so while zipping select the files (abc.txt,efg.txt) and zip dont select the myfile folder to zip.
So that when you unzip the file, the parent dir for each file or folder will be the directory in which you unzip.
The myfile directory was zipped into the zip file when it was created and looking at the unzip options there isn't a way to do this without adding additional steps.
If this entire process is under your control you should look at either creating the zip without using including the parent directory or you could use an alternative like tar (to create and extract) which allows you to extract content from the repo with greater precision.

Can we add files to a zip archive with modified file names

I want to create a zip archive for some files in folder hierarchy.
for example:
abc.php_0973_890
newone.text_2344_870
I want to have:
abc.phpnewone.txt in my zip archive. Is there any way for doing this?
We can not do that as zip doesnot suuport anything like that, we can use tar for such things as it as options like: --transform which can be used to transform the output filename

trying to tar a directory lists the directory instead

so I did this
tar cvzf test.zip FP
with the intention of creating a zip of the directory FP
however, it instead lists the directories inside the zip
FP/
FP/php/
FP/php/pdf/
FP/php/docs/
FP/aspnet/
FP/aspnet/pdf/
FP/aspnet/docs/
how do I go about tarring the directory?
Your command is good indeed.
Listing appear when specifying v option (in 'cvzf')
You can check what a gzipped tar file contain by running
$ tar tzvf test.zip
By the way you should avoid to put .zip extension on a "gzipped" tar file. If you really want to make a zip, use 'zip' package instead.
I think it DID create it. The list is just the command being verbose (-v).

Resources