Icon for post type: learned Images to Kirby Content Directories with Bash Permalink: Images to Kirby Content Directories with Bash Bookmark Icon

While working on a new section for my blog, I needed to convert a directory of images into a Kirby content file structure. I‘ve had some prior scripts from when I moved my blog from WordPress to Kirby and modified one of them to do exactly that. The below script creates a content text file and moves the source images into their repectively created subdirectory.

The image source directory looks like this:

image-source
  -- image1.jpg
  -- image2.png
  -- image3.jpg
  -- …

and the final resulting structure looks like this:

image-source
|-- 1_image1-1567760562
   -- foo.txt
   -- image1.jpg
|-- 2_image2-1567760562
   -- foo.txt
   -- image2.png
|-- 3_image3-1567760562
   -- foo.txt
   -- image3.jpg

With this structure, the folders can be moved into a Kirby content directory and will appear as listed pages. If you prefer to create unlisted or draft pages, remove ${i}_ from the script.

The script is pretty barebones and does not account for spaces in filenames or other things. It does add the required Kirby content file as foo.txt which contains a field with the filename, but if needed you can specify the fieldname and other content as required.

#!/bin/bash

# This script iterates over every JPG or PNG file in the directory and moves it into its own directory.
# The resulting directories will be named as e.g.: 1_img_0395-1567760562
# The above directory name consists of: iterator_filename-timestamp
# The file itself will not be renamed and only moved into its repective directory
# Save this script as e.g. `createdirs.sh` in the source images directory
# Run the script with `sh createdirs.sh` 

i=1;
for file in *.jpg *.png; do
  dir=${file%%.*}
  add=$(date "+%s")
  mkdir -p "${i}_${dir}-$add"
  mv "$file" "${i}_${dir}-$add"
  echo "imagefilename: $file" > ${i}_${dir}-$add/foo.txt
  ((i++))
done

The script is provided as is and hopefully will help someone to achieve something similar. Let me know if you found it useful or have any questions, in which case I‘ll try to help


Leave a comment

Replied on your own website? Send a webmention!

Comments