Linux Zip Command: Add a File with Different Name

Linux zip command: add a file with different name

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.

How to pack files into a ZIP file which have same name but different suffixes?

The task could be done with following batch file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cd /D "C:\folder" || exit /B
for %%I in ("video\*-video.*") do (
set "VideoFileName=%%~nxI"
setlocal EnableDelayedExpansion
set "CommonName=!VideoFileName:-video%%~xI=!"
"%ProgramFiles%\7-Zip\7z.exe" a -bd -bso0 -mx0 -y -- "!CommonName!.zip" "cover\!CommonName!-art.*" "image\!CommonName!-screen.*" "mix\!CommonName!-HDphoto.*" "video\!VideoFileName!"
endlocal
)
endlocal

The batch file defines with the first two command lines the required execution environment which is:

  • command echo mode turned off
  • command extensions enabled
  • delayed expansion disabled

The next command changes the current directory to C:\folder. If that command fails because of the directory does not exist or a UNC path is used instead of a path starting with a drive letter and a colon, the batch file processing is exited without any further message than the error message output by command CD.

The FOR loop searches in subdirectory video for non-hidden files matching the wildcard pattern *-video.*. The file name with file extension of a found video file matching this wildcard pattern is assigned to the environment variable VideoFileName.

Next delayed expansion is enabled as required to make use of the file name assigned to the environment variable VideoFileName. Please read this answer for details on what happens in background on execution of setlocal EnableDelayedExpansion and later on execution of corresponding endlocal.

A case-insensitive string substitution using delayed expansion is used to remove from video file name -video left to the file extension and the file extension itself which can be .mp4, .mpg, .mpeg, etc. VideoFileName was defined with file extension in case of the file name itself contains anywhere the string -video which should not be removed by the string substitution. For example My Home-Video-video.mp4 assigned to VideoFileName results in My Home-Video getting assigned to the environment variable CommonName because of taking also the file extension into account on string substitution.

Next 7-Zip is executed with the command a and the switches as posted in question to create or add to a ZIP file in current directory with common name part of the files in the four directories and file extension .zip the video file name and the other image files from the other three directories with whatever file extension the images have in the other three directories.

Then endlocal is executed to restore the previous environment with delayed expansion disabled again.

The commands setlocal EnableDelayedExpansion and endlocal are used inside the FOR loop to be able to process correct also a files collection with a common name like Superman & Batman (+ Robin!) containing an exclamation mark.

The video files in subdirectory video define which files to pack into a ZIP file. So all image files in the three image directories are ignored for which no video file exists in directory video.

Note: The batch file is not capable processing correct video file names which contain an exclamation mark in the file extension. I doubt that this limitation is ever a problem as I have never seen a video file extension with an exclamation mark.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • echo /?
  • endlocal /?
  • exit /?
  • set /?
  • setlocal /?

See also single line with multiple commands using Windows batch file for an explanation of conditional operator ||.

how to zip two different file extension

You can just append files and file matches to the end of the zip command to add more files:

zip -j -r ... /data/*.jtl /data/*.log ...
  • http://linux.die.net/man/1/zip

linux how to add a file to a specific folder within a zip file

If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:

zip -g xxx.zip folder/file

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.

In linux, how can I zip specific sub directories into their own zip files named parent directory name and output them all into a single directory?

I think you've mostly described the steps. You just need to put them into a script. Something like:

#!/bin/bash

cd /srv/www
for domain in *; do
(
cd $domain/html &&
zip -r /srv/web_backup/$domain.zip . .htaccess
)
done

Breaking that down a bit:

# change into the /srv/www directory
cd /srv/www

# for each domain in this directory (we're largely assuming here that
# this directory contains only directories representing domains you
# want to backup).
for domain in *; do

# a (...) expression runs the included commands in a subshell, so
# that when we exit we're back in /srv/www.
(
# change into the html directory and, if that was successful, update
# your zip file.
cd $domain/html &&
zip -r /srv/web_backup/$domain.zip .
)

done

This is only one way of tackling this particular problem. You could also write something like:

#!/bin/sh

find /srv/www/* -maxdepth 0 -type d -print0 |
xargs -0 -iDOMAIN sh -c 'cd "DOMAIN/html" && zip -r "/srv/www/DOMAIN.zip" .

That does roughly the same thing, but makes clever use of find and xargs.



Related Topics



Leave a reply



Submit