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.
Author by
Luis Andrés García
Responsive Spanish Electrical and Computer Science Engineer (Telecoms Engineer).
Updated on June 05, 2022Comments
-
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 almost 11 yearsMinor nitpick:
echo -e
is an unportable SysVism. I'd useprintf "@ myfile.txt\n@=myfile2.txt\n" | ...
. -
mkrnr almost 11 yearsGood point. Modified my answer.
-
martin about 10 yearsI don't know why but I can make it work. See my question - stackoverflow.com/questions/22974139/how-to-use-zipnote-command
-
qaisjp about 8 yearsNote: if you want to do multiple filename changes, you need to follow each chunk by
@ (comment above this line)\n
-
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 about 6 yearshow about changing a directory name?
-
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 thezip
command revealed comments of the type"@ path/to/file.ext"
. -
David Given over 4 yearszip 3.1 appears to be pretty uncommon (the one in Debian/Ubuntu is 3.0, and yes, it's 11 years old)...