简体   繁体   English

Bash 脚本可自动压缩不同的子文件夹并将压缩文件存储到具有日期名称的特殊文件夹中

[英]Bash script to automatically zip different subfolders and store the zipped files into a special folder with dated names

My directory structure is thus:我的目录结构是这样的:

\myproject\
     src\
     include\
     zipdir\
     .vscode\
     auto7zip.bat
     

There are specific files from within .vscode\\ subfolder that I would like to archive, and not the entire folder.我想存档.vscode\\子文件夹中的特定文件,而不是整个文件夹。

On windows, the auto7zip.bat file has the following content:在 Windows 上, auto7zip.bat文件具有以下内容:

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

Set TODAY=%date%
ECHO %TODAY%
    
set path="c:\Program Files\7-Zip";%path%
set projectdir=%CD%
set "zipdirsrc=%projectdir%\src"
set "zipdirinc=%projectdir%\include"
set "zipdirothers=%projectdir%\.vscode"
set "movedir=%projectdir%\zipdir"

pushd %zipdirsrc%
ECHO Zipping all contents of %zipdirsrc% and moving archive src to %movedir%
7z a -tzip "%movedir%\src_%TODAY%.zip" -r "%zipdirsrc%\*.*" -mx5
ECHO SRC Task Complete ...

pushd %zipdirinc%
ECHO Zipping all contents of %zipdirinc% and moving archive include to %movedir%
7z a -tzip "%movedir%\include_%TODAY%.zip" -r "%zipdirinc%\*.*" -mx5
ECHO INCLUDE Task Complete ...

pushd %zipdirothers%
ECHO Zipping all other contents of %zipdirothers% and moving archive others to %movedir%
7z a -tzip "%movedir%\others_%TODAY%.zip" -r "%zipdirothers%\*.json" "%zipdirothers%\Makefile" "%zipdirothers%\Makefile*.mk" "%zipdirothers%\windows.vcxproj" "%zipdirothers%\nbproject\" -mx5
ECHO OTHERS Task Complete ...

EXIT /B 0

This batch file, if run today (July 2nd, 2021) on windows with 7zip installed creates include_02-07-2021.zip which is the zipped version of include\\ , src_02-07-2021.zip which is the zipped version of src\\ and others_02-07-2021.zip which is the zipped version of the specific files from the .vscode\\ directory.如果今天(2021 年 7 月 2 日)在安装了7zip Windows 上运行此批处理文件,则会创建include_02-07-2021.zipinclude\\的压缩版本, src_02-07-2021.zipsrc\\的压缩版本和others_02-07-2021.zip ,它是.vscode\\目录中特定文件的压缩版本。 These zipped files are created automatically inside the zipdir\\ directory.这些压缩文件是在zipdir\\目录中自动创建的。

Is there an equivalent bash script that when run from within \\myproject\\ accomplishes the same thing on linux distributions, in particular, ubuntu ?是否有等效的 bash 脚本,当从\\myproject\\运行时,它在linux发行版(特别是ubuntu上完成相同的事情?

This should do what you need.这应该做你需要的。

#!/bin/bash

today=$(date +'%d-%m-%Y')
echo $today

movedir=../zipdir

function backup() {
    if [[ -n $2 ]]; then
        newname="$2"
    else
        newname="$1"
    fi
    cd "$1"
    [[ -z $custom ]] && custom="./*"
    echo "Zipping all contents of ./$1 and moving archive $newname to $movedir"
    zip "$movedir/${newname}_$today.zip" -r $custom  -9
    unset custom
    cd ..
}

unset custom

backup src
backup include
custom='./*.json ./Makefile ./Makefile*.mk ./windows.vcxproj ./nbproject/'
backup .vscode others

Update 1:更新 1:

  • Removed irrelevant part of answer删除了答案的不相关部分
  • Updated script to exclude root directory from archive by changing into relevant directory (since request was clarified by OP).更新脚本以通过更改为相关目录从存档中排除根目录(因为请求已由 OP 澄清)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM