I have a tree of folders where every folder contains between two and three jpg files. As I have plenty folders, I would like to develop a script to create recursively an Inkscape file in every folder containing the jpg files in it, and renaming the svg file with the name of the containing folder. How may I achieve this?


Find directories with JPG files and run inkscape on them:

find . -type d \
    -exec sh -c 'set -- "$1"/*.jpg; [ -e "$1" ]' sh {} ';' \
    -exec sh -c 'inkscape --export-plain-svg="$1/${1##*/}.svg" "$1"/*.jpg' sh {} ';'

This will look for directories under the current directory. If the directory contains at least one .jpg file, inkscape is invoked on this directory, picking up all .jpg files from within it and exporting an SVG file into it, with the name of the directory as its filename.

The output filename is constructed as $1/${1##*/}.svg where $1 is the first command line argument to the sh -c script (this is the path to the directory with the .jpg files), and where ${1##*/} will strip the path from the directory and leave just the directory name.

This is untested in as far as I don't have inkscape and I'm not familiar with the program (apart from reading its manual).

Given the following directory structure:

$ tree
|-- dirA
|   `-- 1
|       |-- file.jpg
|       |-- image.jpg
|       `-- picture.jpg
`-- dirB
    |-- 2
    |   |-- file.jpg
    |   |-- image.jpg
    |   `-- picture.jpg
    |-- 3
    |   |-- file.jpg
    |   `-- image.jpg
    `-- 4
        |-- file.jpg
        `-- image.jpg

6 directories, 10 files

This is what would have been executed:

inkscape --export-plain-svg=./dirA/1/1.svg ./dirA/1/file.jpg ./dirA/1/image.jpg ./dirA/1/picture.jpg
inkscape --export-plain-svg=./dirB/2/2.svg ./dirB/2/file.jpg ./dirB/2/image.jpg ./dirB/2/picture.jpg
inkscape --export-plain-svg=./dirB/3/3.svg ./dirB/3/file.jpg ./dirB/3/image.jpg
inkscape --export-plain-svg=./dirB/4/4.svg ./dirB/4/file.jpg ./dirB/4/image.jpg
  • Thanks Kusalananda for your quick answer. At first, it seemed to work. The script created the svg file keeping the name of the containing folder. However, only 1 jpg file was included and not all the files contained in the folder. What might be the problem and how can it be solved? – antecessor Sep 24 '17 at 14:14
  • @antecessor This may be something to do with how inkscape works when you give it multiple input files. I don't use this program. Could you tell me what the command should look like for a single folder? – Kusalananda Sep 24 '17 at 14:16
  • I don't really know how the command should look like... I've been checking the manual and I don't really know where to look at... I ran Inkscape from my terminal but nothing appears on it when importing images from the GUI... – antecessor Sep 24 '17 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.