I have a directory structure that looks like this:
basedir/dir1/dir2/dir3/dir4/myzip/subdirs
I would like to create an archive of the myzip dir with all its subdirectories, but without including dir1 to dir4 in the zip archive, and save the resulting archive myzip.zip in basedir.
This doesn’t work as dir1 to 4 get included in the archive
cd basedir
zip -r myzip.zip dir1/dir2/dir3/dir4/myzip
I know I can do it this way
cd basedir/dir1/dir2/dir3/dir4
zip -r ../../../../myzip.zip myzip
cd ../../../..
# Need to go back to basedir for next commands
This also works
cd basedir
cp -r dir1/dir2/dir3/dir4/myzip myzip
zip -r myzip.zip myzip
rm -rf myzip
# Kind of annoying to have to copy then remove
Is there a more elegant way to do that? I was hoping I could add some arguments to the first command I typed to make it work the way I wanted in one line.
Isn’t what I want to do what most people want to do too? IMO they should only include subdirs by default in the zip command.
I don’t understand the point of the current implementation of zip, because all those do just the same thing:
zip -r myzip.zip dir1/dir2/dir3/dir4/myzip
zip -r myzip.zip dir1/dir2/dir3/dir4
zip -r myzip.zip dir1/dir2/dir3/
zip -r myzip.zip dir1/dir2/
zip -r myzip.zip dir1/
Wouldn’t it have been smarter to write the zip command so it includes only the subdirectories by default?
Then if you wanted dir1 to dir4 included you could just do:
myVersionOfZip -r myzip.zip dir1
If you wanted only the subdirs of myzip you would do:
myVersionOfZip -r myzip.zip dir1/dir2/dir3/dir4/myzip
You can avoid the cd ../../../.. if you use pushd.
Archival tools are designed to preserve path information, so I doubt that there’s a way around what you’re trying to.
That will make your script a little bit more elegant.