add files to a specafic directory inside zip file - linux

Let's say we have a zip file contains a directory named aq and in the current working directory we have files:
./
|- aq/a.txt
|- b.txt
When i use this command:
zip test.zip aq/* the a.txt file will be zipped into the aq directory that's inside the zip file
The question is how then can I add b.txt into the aq directory that's inside the test.zip file without putting the b.txt in the aq directory first which is in the current working directory like what I did with the a.txt?

Make a temp directory called, e.g. /tmp/$$/aq/, symlink into the temp directory and then do:
(cd /tmp/$$ && zip -r $ZIPDEST aq/)
i.e. zip using the temp dir. zip by default follows symbolic links, so it puts the file into the zip without making a copy.
This is pretty much how I construct complicated hierarchical zip files without copying everything to make the archive.
Tar has better options for renaming items as you're putting them into the archive, but you asked about zip.

Related

How to create a tar archive from an absolute path and package files as if they were relative?

On Linux, I am trying to create a .tar.gz archive from a different directory, that is I have a bash script that will be executed from a different directory. The script will package the folder, I will give the absolute directory of the folder say /home/user1/Documents/folder1 however when it packages the tar file, it puts the entire absolute directory in the archive, whereas I only want the relative one from folder1.
For example:
tar czf /home/user1/Documents/folder1.tar.gz /home/user1/Documents/folder1
This will create an archive but where the first folder will be home and then inside that user1 inside that documents and inside that the folder1, no other subfolders from other branches of course.
Also the console gives this error:
tar: Removing leading `/' from member names
I want it to be packaged as if I would execute the command from the same folder, so the only folder in the archive should be folder1, and inside that it's own subfolders.
So the archive inside should look just as if I would have executed this code from the same directory folder1 is in:
tar czf folder1.tar.gz folder1
You can use the -C option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P, but you often don't want that.

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.

Shell: How to move multiple files to a directory and zip that directory with different name?

Fore example, I have some files in my project, structure could be like this
Project/
-- file1.txt
-- file2.txt
-- build/
-- sub-folder/
I want to zip some files from my project and I can do that using ZIP command (build folder and some other files are excluded from zip)
zip -r build/project-04.05.2016 ./ -x *\build\*
After this new file is created:
build/project-04.05.2016.zip
In Mac Finder when I double click on this file it becomes unzipped like this:
build/project-04.05.2016/
----------- file1.txt
----------- file2.txt
----------- subfolder/
I would like to somehow zip this archive, so when it's unzipped, instead of "project-04.05.2016" I get a folder "project" with the same content. I was trying to rename the file to "project-04.05.2016" after it's zipped as "project", but when it's unzipped the results are the same. Maybe there's a way to first move the files to some temporary "project" folder and than to zip them to "project-04.05.2016.zip"? Thanks in advance.
Here is my current solution, it's a small script and I'm not too satisfied with it, but it works. I will accept if someone answers with more elegant solution. So, here it is:
#clear the contents of the previous build first
rm -r build/*
#copy required files to a temporary folder
rsync -av --exclude=build ./ build/project
#change current directory to build
cd build/
#zip files from build/project folder
zip -r "project-04.05.2016" project
#remove temporary folder
rm -r project/
#final zip file is at location:
#build/project-04.05.2016.zip

zip a list of files at command-line linux

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.

Resources