7

In a linux command line, you zip a file by:

zip -mqj archive.zip file.txt

Now, I need to store 'file.txt' as 'file2.txt' in 'archive.zip', without renaming the file before zipping. When unzipped, the file should be called 'file2.txt'.

How can I store the file with a different name? Read through the MAN page and didn't find an answer.

2

Does creating a hard link to file.txt count?

ln file.txt file2.txt

Create file2.txt which points to the exact same inode as file.txt, without actually doubling the space

5

See https://stackoverflow.com/questions/16710341/linux-zip-command-add-a-file-with-different-name

The solution below is the exact copy of the answer of @mkrnr on stackoverflow

You can use zipnote which should come with the zip package.

First build the zip archive with the myfile.txt file:

zip archive.zip myfile.txt

Then rename myfile.txt inside the zip archive with:

printf "@ myfile.txt\n@=myfile2.txt\n" | zipnote -w archive.zip

(Thanks to Jens for suggesting printf instead of echo -e.)

A short explanation of "@ myfile.txt\n@=myfile2.txt\n":

From zipnote -h: "@ name" can be followed by an "@=newname" line to change the name

And \n separates the two commands.

1

Hy there, this is my first answer so I hope I've done everything correct :-)

Here's my solution to your problem, a nice one-liner:

cp file.txt file2.txt | zip -mqj archive.zip file2.txt

Hope I could help!

  • 3
    This is a good attempt, but the pipe is confusing. I think the person who asked didn't want to create a copy of the file first, but if he did, another way of doing this might be cp file.txt file2.txt && zip -mqj archive.zip file2.txt && rm -f file2.txt - this would clean up the temporary file2.txt that got created. – Matt Simmons May 31 '09 at 18:05
  • 2
    thanks for pointing this out - I would post this approach, too (but you already did, thanks!) cp file.txt file2.txt && zip -mqj archive.zip file2.txt (because of the -m switch the file already gets moved and there's no need to remove afterwards!) – hajowieland May 31 '09 at 18:09
  • Ah! Good call. I'm less familiar with zip than I am tar. thanks! – Matt Simmons May 31 '09 at 18:14
  • @Matt. Cant we just use mv instead of cp which wont create a copy, will rename the file, zip it and remove the copy to clear space. Just a thought. – Viky Jun 1 '09 at 4:37
  • @Viky - That would work fine, except one of the requests in the question was that we not rename the file. I think if we got more about the situation, a better answer would have presented itself, but as long as the person who asked the question is happy... – Matt Simmons Jun 1 '09 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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