Images to Kirby Content Directories with Bash

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 be…


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream

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


This content originally appeared on foobartel.com :: Stream and was authored by foobartel.com :: Stream


Print Share Comment Cite Upload Translate Updates
APA

foobartel.com :: Stream | Sciencx (2019-09-09T22:00:00+00:00) Images to Kirby Content Directories with Bash. Retrieved from https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/

MLA
" » Images to Kirby Content Directories with Bash." foobartel.com :: Stream | Sciencx - Monday September 9, 2019, https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/
HARVARD
foobartel.com :: Stream | Sciencx Monday September 9, 2019 » Images to Kirby Content Directories with Bash., viewed ,<https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/>
VANCOUVER
foobartel.com :: Stream | Sciencx - » Images to Kirby Content Directories with Bash. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/
CHICAGO
" » Images to Kirby Content Directories with Bash." foobartel.com :: Stream | Sciencx - Accessed . https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/
IEEE
" » Images to Kirby Content Directories with Bash." foobartel.com :: Stream | Sciencx [Online]. Available: https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/. [Accessed: ]
rf:citation
» Images to Kirby Content Directories with Bash | foobartel.com :: Stream | Sciencx | https://www.scien.cx/2019/09/09/images-to-kirby-content-directories-with-bash/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.