Create ZIP File for Each Folder Bash Command

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Kryptonyx
Level 1
Level 1
Posts: 10
Joined: Sat Mar 24, 2018 5:29 pm

Create ZIP File for Each Folder Bash Command

Post by Kryptonyx »

Hi Everyone,

I'm trying to create a zip file for each parent folders in Data

/Data
/Data/FolderA and everything in it
/Data/FolderB and everything in it
/Data/FolderC and everything in it

/Data/FolderA.bak
/Data/FolderB.bak
/Data/FolderC.bak

I tried the following:

Code: Select all

#!/bin/bash
for Folder in */
do
  7z a -tzip -mx1 "${Folder%}.bak"
done
I get FolderA.bak, FolderB.bak, FolderB.bak, and a 4th file which include the 3 folders & 3 bak it just created.

I also tried this one:

Code: Select all

#!/bin/bash
for Folders in $(find . -maxdepth 1 ! -name '.*' -type d)
do
  7z a -tzip -mx1 "${Folders%}.bak"
done
I get FolderA.bak, FolderB.bak, FolderB.bak but doesn't stop compressing itself

Can anyone help me?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: Create ZIP File for Each Folder Bash Command

Post by FreedomTruth »

I think you're forgetting to tell 7z *what* to compress, it's doing the current working directory each time (?). Using your first example, try:

Code: Select all

#!/bin/bash
for Folder in /Data/*
do
  if [ -d "$Folder" ]
  then
    7z a -tzip -mx1 "${Folder%}.bak" "$Folder/"
  fi
done
Locked

Return to “Scripts & Bash”