Angelucifer
Angelucifer

Reputation: 1

Bash script to archive sub-folders with 7zip without archiving archives

I am trying to write a bash script that will zip all sub folders into .cbz files, while leaving the top-level folders alone and that doesn't archive already zipped folders using 7zip.

Here is what I wrote:

#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
    7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
    rm -rf "$folder"
done

My current issue seems to be that it will archive already zipped folders, but everything else works fine.

The reason I specified a directory is to avoid accidentally archiving the contents of my home folder... again.

My intent for this script is to go into my MangaLib folder, and archive the contents of the folders inside it, without archiving those folders too, which is the purpose of the two wildcards in the address. Then, it should delete the original folder and leave only the .cba file.

Again, the issue is that I will routinely run this script to compress the subfolders of any new additions to my MangaLib folder, but the script will also compress the previously archived folders as well, which is not my intent.

Upvotes: 0

Views: 1182

Answers (1)

nit
nit

Reputation: 1359

Use file command to find file type. Then zip only the ones you need.

#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
    if ! `file $folder  | grep -i zip > /dev/null 2>&1`; then 
    ##Most of the zip utilities contain "zip" when checked for file type. 
    ##grep for the expression that matches your case

      7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
      rm -rf "$folder"

    fi
done

Upvotes: 0

Related Questions