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.
Related
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.
For example i have folder "admin" which contains folder "1" and two php files "index.php" and "page.php". I try to use tar -zcvf admin.tar.gz admin and got admin.tar.gz archive. If i open this archive, i can see archive contains "admin" folder and inside this directory is folder "1" and two php files.
I want to create tar.gz archive with all files and folders, but without parent folder. Create archive and it contains only folder "1" and two php files. How i can do it?
You can use the -C option:
tar -C admin -zcvf admin.tar.gz .
See man tar
-C, --directory=DIR
Change to DIR before performing any operations.
This option is order-sensitive, i.e. it affects all options that follow.
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.
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.
The zip command generally places the output zip file in the current directory. But, I'm wondering if there is an option that I'm overlooking in the man page that lets me specify the destination directory for the output archive file.
This created 'my_archive' in the current directory:
zip -rT my_archive dir_to_be_archived
But, I want to be able to place 'my_archive' under ./my_backups without having to 'cd' into that backups directory. Is there something like:
zip -rt my_archive dir_to_be_archived --destination ./my_backups
You can specify the destination path directly:
zip -r /path/to/destination.zip /files
Keep in mind that the destination should not be in the same directory as the files that you are zipping up.