Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a zip file without entire directory structure

Tags:

I am trying to create a zip file and want to preserve most of the directory structure, but not the rootdir as defined from the command line. The command I'm using is:

zip -r out.zip /foo/bar/

I'd like it to recurse through bar and add all files with preserved directory structure (which it does). However I do not want 'foo' to be the top level directory in the zip file created. I would like bar to be the top level directory.

Is there any easy way to go about this? I realize I could change directories before zipping to avoid the problem, but I'm looking for a solution that doesn't require this.

like image 394
Brandon Yates Avatar asked Mar 14 '12 21:03

Brandon Yates


People also ask

How do I create a zip file without a folder?

You can use -j . -j --junk-paths Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

How do you zip all the .TXT files in a directory assume the directory doesn't contain any folders?

Syntax : $zip –m filename.zip file.txt 4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

How do I zip multiple folders separately?

Right-click on the file or folder. Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.


2 Answers

This should do it:

cd /foo/bar/ 
zip -r ../out.zip *

The archive will be in /foo/out.zip

like image 104
Claude Schlesser Avatar answered Sep 17 '22 18:09

Claude Schlesser


I don't believe zip has a way to exclude the top level directory. I think your best bet would be to do something like: pushd /foo; zip -r out.zip ./bar; popd;

But this is exactly the sort of answer you said you didn't want.

like image 21
kbyrd Avatar answered Sep 19 '22 18:09

kbyrd