Score:0

Split files into multiple subfolders and zip each subfolder

mx flag

I am using Ubuntu 18.04.5 LTS.

I have created the following script to split files in a folder into different subfolders:

#!/bin/bash

dir_size=850
dir_name="images"
n=$((`find . -maxdepth 1 -type f | wc -l`/$dir_size+1))
for i in `seq 1 $n`;
do
    mkdir -p "$dir_name$i";
    find . -maxdepth 1 -type f | head -n $dir_size | xargs -i mv "{}" "$dir_name$i"
done

However, I would like to zip each subfolder after creation, so that I only get zipped subfolders in my final directory.

Any suggestions how to do this?

I appreciate your replies!

vanadium avatar
cn flag
`"$dir_name$i"` is a folder you created. So what prevents you, while in the `do...done` loop, from subsequently zipping the folder?
bac0n avatar
cn flag
Is there any particular reason for using `zip`?
Score:1
cn flag

You can use a for-loop to get a fixed number of items. a will increase with the number of c and b by every iteration.

#!/bin/bash

mapfile -td '' < <( \
    find -maxdepth 1 -type f ! -name '*.zip' -print0 \
)
((${#MAPFILE} == 0)) && exit 1

for ((a=0, b=0, c=850; a<${#MAPFILE[@]}; a+=c, b++)); do
    mkdir images$b && \
    mv -t $_ "${MAPFILE[@]:$a:$c}" && zip -mTr images$b images$b
done
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.