Skip to content
Advertisement

Shell: How to move multiple files to a directory and zip that directory with different name?

Fore example, I have some files in my project, structure could be like this

Project/
-- file1.txt
-- file2.txt
-- build/
-- sub-folder/

I want to zip some files from my project and I can do that using ZIP command (build folder and some other files are excluded from zip)

zip -r build/project-04.05.2016 ./ -x *build* 

After this new file is created:

build/project-04.05.2016.zip

In Mac Finder when I double click on this file it becomes unzipped like this:

build/project-04.05.2016/
----------- file1.txt
----------- file2.txt
----------- subfolder/

I would like to somehow zip this archive, so when it’s unzipped, instead of “project-04.05.2016” I get a folder “project” with the same content. I was trying to rename the file to “project-04.05.2016” after it’s zipped as “project”, but when it’s unzipped the results are the same. Maybe there’s a way to first move the files to some temporary “project” folder and than to zip them to “project-04.05.2016.zip”? Thanks in advance.

Advertisement

Answer

Here is my current solution, it’s a small script and I’m not too satisfied with it, but it works. I will accept if someone answers with more elegant solution. So, here it is:

#clear the contents of the previous build first
rm -r build/*

#copy required files to a temporary folder
rsync -av --exclude=build ./ build/project

#change current directory to build
cd build/

#zip files from build/project folder
zip -r "project-04.05.2016" project

#remove temporary folder
rm -r project/

#final zip file is at location:
#build/project-04.05.2016.zip
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement