Search code examples
linuxcommandziprename

Linux zip command: add a file with different name


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 file myfile.txt to a zip, but renaming it to myfile2.txt.


Solution

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