Visit Our UNIX and Linux User Community


Create multiple zip files each containing 50 xml files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create multiple zip files each containing 50 xml files.
# 1  
Old 01-12-2014
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
# 2  
Old 01-13-2014
Better to go with a script.

Could not recall any direct command.
This User Gave Thanks to panyam For This Post:
# 3  
Old 01-13-2014
Much appreciated if you could you provide pseudo code or actual.

Thanks
Rakesh
# 4  
Old 01-13-2014
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
This User Gave Thanks to rbatte1 For This Post:
# 5  
Old 01-14-2014
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 :
------------------
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 Smilie

Last edited by Franklin52; 01-14-2014 at 03:15 AM.. Reason: Please use code tags
This User Gave Thanks to Rakesh Thobula For This Post:
# 6  
Old 01-15-2014
Your script is very good Smilie
Instead of the while loop you can use printf
Code:
DIR=$(printf "%04d" $DIR)

All in one:
Code:
# increment DIR and pad with leading zeros
DIR=$(printf "%04d" $((DIR+1)))


Last edited by MadeInGermany; 01-15-2014 at 05:41 AM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-16-2014
Thanks and it worked ok. I removed while loop from my code.

Previous Thread | Next Thread
Test Your Knowledge in Computers #893
Difficulty: Easy
sed uses regular expressions, such as .* (zero or more of any character).
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (1 Reply)
Discussion started by: b.saipriyanka
1 Replies

2. UNIX for Beginners Questions & Answers

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (2 Replies)
Discussion started by: b.saipriyanka
2 Replies

3. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

4. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

5. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

6. Shell Programming and Scripting

Zip Multiple files to One .zip file in AIX system

Hi I have a requirement in unix shell where I need to zip multiple files on server to one single .zip file. I dont see zip command in AIX and gzip command not doing completely what I want. One I do .zip file, I should be able to unzip in my local Computer. Here is example what I want... (9 Replies)
Discussion started by: RAMA PULI
9 Replies

7. Programming

help need in the perl script that create one xml file form multiple files.

Hi every one, Please excuse me if any grammatical mistakes is there. I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this</p> file1:bvr.xml ... (0 Replies)
Discussion started by: veerubiji
0 Replies

8. UNIX for Dummies Questions & Answers

Zip multiple files and copy to help

Hi All, I have a set of large files ~ 500_900Mb I have generated and I'd like to quickly zip and copy them to a new folder elsewhere ... Can anyone suggest a quicky ?? Cheers :) (3 Replies)
Discussion started by: pawannoel
3 Replies

9. AIX

ZIP multiple files and also specify size of zip file

I have to zip many pdf files and the size of zip file must not exceed 200 MB. When size is more than 200 MB then multiple zip files needs to be created. How we can achieve this in UNIX? I have tried ZIP utility but it takes a lot of time when we add individual pdfs by looping through a... (1 Reply)
Discussion started by: tom007
1 Replies

Featured Tech Videos