Create multiple zip files each containing 50 xml files.

Hi,

Is there a direct command or need to write a shell script for following requirement?

Everyday a folder is populated with approx 25k to 30k xml files. I need to create multiple zip files in the same folder each containing 50 xml files. The last zip file may or may not contain 50 xml files. Delete all the xml files once the zip files are created successfully.

Example :
If the folder has total of 156 xml files, then I need to create 4 zip files in same folder:
<date>01.zip (containing 50 xml files)
<date>02.zip (containint 50 xml files)
<date>03.zip (containing 50 xml files) and
<date>04.zip (containgin 6 xml files)

Thanks
Rakesh

Better to go with a script.

Could not recall any direct command.

1 Like

Much appreciated if you could you provide pseudo code or actual.

Thanks
Rakesh

To give us more detail, can you tell us:-

  • What have you tried so far?
  • What are you most comfortable coding?
  • What OS and version are you using?
  • What shell are you using?

Most importantly:-
What have you tried so far?
It's better for you if we suggest adjustments to your code so you learn how we got there and so that we can be sure we're going the right way.

Robin
Liverpool/Blackburn
UK

1 Like

Thanks Robin.

-- I tried zip command which worked for all files in the folder.
-- Just a novice in Shell scripting
-- Linux
-- bash

I will be trying to extend my code to include "for" for specific range of files to be zipped.

Rakesh

---------- Post updated at 05:05 PM ---------- Previous update was at 11:29 AM ----------

Finally achieved what I wanted.
Now folder will have only zip files. So one less work - no need of deletion of individual files in folder.

Here is the code :
------------------

#!/bin/sh
#NOW will hold the date format as : yyyyddmmhhmiss
NOW="$(date +"%Y%m%d%H%M%S")"
#LIMIT the number of files to go in each zip file
LIMIT=2
COUNT=0
#DIR is initialized with initial value 0001 and later in the code it is incremented. While loop is required to put leading zeros to DIR.
DIR=0001
#Start FOR loop for number of xml files in the folder
for FILE in *txt
do
#Add each file to zip with zip filename
zip -r $NOW-$DIR $FILE
let COUNT=$COUNT+1
#If the for loop reaches the Limit, then start putting files into new zip file
if [ $COUNT -eq $LIMIT ]
then
# base 10 ie. 10# is used in the code below to avoid error : value too great for base
let DIR=10#$DIR+1
#WHILE loop is needed to put the leading zeros to the variable DIR
while [ ${#DIR} -ne 4 ];
do
DIR="0"$DIR
done
COUNT=0
fi
done

-----------------

Thanks for supporting me in learning what I should do.

Posting this for the benefit of others.

Thanks
Rakesh :slight_smile:

1 Like

Your script is very good :b:
Instead of the while loop you can use printf

DIR=$(printf "%04d" $DIR)

All in one:

# increment DIR and pad with leading zeros
DIR=$(printf "%04d" $((DIR+1)))
2 Likes

Thanks and it worked ok. I removed while loop from my code.