bashterminal7zip

Trying to convert script so it copies subfolder contents into individual 7z archives


The issue I'm having is this, in the current directory I might have multiple folders like: "Aburner", "Btoads", "and "gigawing", each with multiple files inside of them. I had a working batch code that would grab the files in "Aburner" and put them in a zip with the name of the folder. I have a Bash version but it either zips everything into one folder or instead of just putting the files in "Aburner" into the archive it zips the folder as well so instead of "Aburner.7z/files" it is "Aburner.7z/Aburner(folder)/files". My Batch code that worked perfectly was:

for /d %%d in (*.*) do 7z a "%%d.7z" ".\%%d\*"

I'd really appreciate any help in getting this working, I'm kinda at my wits end and it is the last function I need to get working for my code to be complete.


Solution

  • An approach to zip all folders with 7z CLI to .7z archives in Bash excluding the parent folder itself:

    for dir in */; do
      (cd "${dir}" && 7z a "../${dir%/}.7z" ./)
    done
    

    Explanation:

    1. for dir in */ – for each folder in the current folder;

    2. cd "${dir}" – go to each folder on the current level;

    3. 7z a "../${dir%/}.7z" ./ – create an archive on the previous directory with all files included (recursively).