简体   繁体   English

Shell:如何将多个文件移动到一个目录并以不同的名称压缩该目录?

[英]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命令来完成(构建文件夹和其他一些文件不包含在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: 在Mac Finder中,当我双击该文件时,它将像以下这样解压缩:

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. 我想以某种方式压缩此存档,因此当解压缩该存档时,我会得到一个具有相同内容的文件夹“ project”,而不是“ project-04.05.2016”。 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. 在将其压缩为“项目”之后,我试图将文件重命名为“ project-04.05.2016”,但是当解压缩时,结果是相同的。 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"? 也许有一种方法可以先将文件移动到某个临时的“项目”文件夹,然后再将其压缩为“ project-04.05.2016.zip”? Thanks in advance. 提前致谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将目录中的大量zip文件移动到bash中指定数量的多个子目录中? - How do I move a large number of zip files in a directory to a specified number of multiple subdirectories in bash? 使用Shell压缩zip文件中目录中的文件 - Compress files in a directory in a zip file with the shell Shell脚本修改具有同一目录中多个文件的文件名的变量 - Shell script to modify variable with file name of multiple files in same directory Linux Bash:将多个不同的文件移动到同一目录 - Linux Bash: Move multiple different files into same directory Shell Script,将每 2 个文件移动到每个目录 - Shell Script, move each 2 files to each directory 在同一目录中移动多个文件,而无需每次都输入目录名(Linux) - Move multiple files in same directory without typing directory name each time (Linux) 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 如何根据 linux 中的名称将文件移动到新目录? - How do I move files to a new directory based on their name in linux? 在linux中,我怎样才能把zip具体的子目录改成自己的zip文件名,把父目录名和Z78E6221F6393D1356DZF2Z文件全部改成a393D1356DZF21的单目录? - 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? 根据目录名称移动文件(Linux / Debian /#!) - Move files based on directory name (Linux / Debian / #!)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM