How to merge ICS files for bulk import of calendar events

Have you ever wanted to add a bunch of events to your calendar all at once? If you have a bunch of individual calendar invite files (.ics), there’s no way to select a bunch of them an import them in one go (at least not in the Apple or Google calendars). But by poking around in the terminal, I figured out a way to do it!

In fairness, there are plenty of tools that will do something like this, but for privacy reasons, I don’t want to hand over my calendar to some unknown app or web service if I can avoid it.

The technique

It turns out ics files are just text files, and there is a (relatively) standardized way that they are formatted. ICS seems to be a pretty widely used format for calendar invites that will be accepted by most apps/services. If you have two ics files, “c1.ics” and “c2.ics”, you can just combine them into one file:

cat c1.ics c2.ics > comb.ics

(If you’re not confident with the command line, you can just open the files in a text editor and copy c2.ics into c1.ics). The result is not yet a valid ics file, since it will have a line that reads: END:VCALENDARBEGIN:VCALENDAR. A valid ics file will only have one pair of BEGIN/END:VCALENDAR statements (at the beginning and end of the file). So you need to go though an delete all the excess statements between each event.

Automate it

This sort of task is a perfect candidate for automation using a shell script. To merge some number of .ics files in a directory into one big file, you can use the following commands:

# combine all calendar files into a temporary file
cat *.ics > temp.txt
# now strip out the overlapping lines and make one big valid ICS file
cat temp.txt | grep -v DARBEG > comb.ics

You can now import all the events at once by importing comb.ics into your calendar app like you would any other ics file.

Some notes

  • You may want to create a separate “calendar” in your app to import the events into, in case something goes wrong (or make a backup). Then they will be easy to delete so you can start again.
  • My experience with apple and google calendars is that they will ignore duplicate entries, so if you have already imported c1.ics and you import a new file that contains the (exact) same entry, you won’t end up with a duplicate in your calendar.
  • In principle, you could also use the command line to do some bulk editing of the events (adding a time zone, for example).
  • Also, as a general rule, make backups before trying dumb stuff like this you find on the internet!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.