Linux zip command: add a file with different name

17,668

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.

Share:
17,668
Luis Andrés García
Author by

Luis Andrés García

Responsive Spanish Electrical and Computer Science Engineer (Telecoms Engineer).

Updated on June 05, 2022

Comments

  • Luis Andrés García
    Luis Andrés García almost 2 years

    I´d like to add a file in a zip file, with a different name, and avoiding the creation of a new file with the desired name. For example, I´d like to add the myfile.txt file to a zip, but renaming it to myfile2.txt.

    Thanks

  • Jens
    Jens almost 11 years
    Minor nitpick: echo -e is an unportable SysVism. I'd use printf "@ myfile.txt\n@=myfile2.txt\n" | ....
  • mkrnr
    mkrnr almost 11 years
    Good point. Modified my answer.
  • martin
    martin about 10 years
    I don't know why but I can make it work. See my question - stackoverflow.com/questions/22974139/how-to-use-zipnote-comm‌​and
  • qaisjp
    qaisjp about 8 years
    Note: if you want to do multiple filename changes, you need to follow each chunk by @ (comment above this line)\n
  • user877329
    user877329 over 7 years
    >The temporary file format is rather specific and zipnote is rather picky about it. It should be easier to change file names in a script. Does not yet support large (> 2 GB) or split archives.
  • Freedom
    Freedom about 6 years
    how about changing a directory name?
  • logo_writer
    logo_writer almost 5 years
    @Freedom Rather late, but I found that using relative paths like "@ myDir/file.txt\n@=anotherDir/file.txt\n" works. Not entirely sure if this is portable, although checking a random ZIP file I made without using the zip command revealed comments of the type "@ path/to/file.ext".
  • David Given
    David Given over 4 years
    zip 3.1 appears to be pretty uncommon (the one in Debian/Ubuntu is 3.0, and yes, it's 11 years old)...