Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a directory

main_folder
  first_folder
    second_folder
      empty.txt
      another_folder
        another_empty.txt

I want to zip second_folder, and I'm in main_folder, so I zipped with the command

zip -jr first_folder/zip_file first_folder/second_folder

The zip file contains

empty.txt
another_empty.txt

But I want

empty.txt
another_folder
  another_empty.txt

How can I have that?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

The -j (junk paths) flag you set is killing what you want since it strips out the folder paths. That being said, however, I don't really see a way to just do second+. From the Man Page:

You may want to make a zip archive that contains the files in foo, without recording the directory name, foo. You can use the -j option to leave off the paths, as in:
zip -j foo foo/*

From what I can tell, that is just how it works in "zip" (nothing in the man suggests otherwise).

Edit: I was a bit after the other guy as I was trying to find out how to do what you want.

Why don't you just navigate to the directory and zip it? If you want to end up back where you were, this will do it (although a waste of time imo).

cd first_folder/second_folder/ && zip -r name ./ && mv name.zip ${here} && cd -

(Thanks to @KarthikT for the "cd -" shortcut).

share|improve this answer
    
The -j option is there because I don't want the Zip stores the full path. I want it to only store starting with the files in second_folder. I think your suggestion about navigating to the directory works. Is there a way to do it without navigating? –  Mika H. Nov 5 '12 at 3:57
    
@nerdwaller you can just do cd - to get to old pwd –  Karthik T Nov 5 '12 at 3:59
    
@KarthikT That's assuming you're starting at ~/ though... –  nerdwaller Nov 5 '12 at 4:05
    
Yeah, but the -j option is what removes the file path, so you could have one item in each of 15 folders deep, and on extraction you would have 15 items in the one directory... I don't see one without navigating at this point. –  nerdwaller Nov 5 '12 at 4:09
1  
@nerdwaller i think you are missunderstanding cd and cd - here. cd - returns you to your here variable exactly –  Karthik T Nov 5 '12 at 4:21
show 5 more comments

From man page for zip

-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).

Just removing the -j should fix it.

share|improve this answer
    
Well, but then the Zip stores the full path. I want it to only store starting with the files in second_folder. –  Mika H. Nov 5 '12 at 3:52
    
yes i figured it was something like that.. y not cd ..../second_folder && zip -r * && cd - :) let me see if any more elegant option is present –  Karthik T Nov 5 '12 at 3:58
    
this i think indicates that there is no other option.. –  Karthik T Nov 5 '12 at 4:01
    
Thank you, Karthik T! –  Mika H. Nov 5 '12 at 4:40
    
you are welcome! –  Karthik T Nov 5 '12 at 6:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.