I will not write a long explanation, just this short one.

The cronjob looks like this:

* * * * * sh -c 'flock -n /tmp/my.lockfile /home/user/scripts/ocr_pdf_scan.sh'

This will start sh (a shell, just like bash) and run the command after -c. The command flock will create a lock-file, to prevent that this cron job is started while there is still the previous cron jobs running.

The cron job runs every minute, so in crontab it has * * * * * (five stars in front of the command).

In the ocr_pdf_scan.sh I have this:

#!/bin/bash

find /home/user/scans/ocr -type f \( -iname "*.pdf" -and -not -iname "*_ocr.pdf" \) | while read file ; do OLDTimestamp=$(stat -c "%Y" "$file") && ocrmypdf -q -l deu+eng --rotate-pages --rotate-pages-threshold 8 -c -s "$file" /home/user/scans/ocr_ready/"$(basename "$file" ".pdf")_ocr.pdf" && touch -d @$OLDTimestamp /home/user/scans/ocr_ready/"$(basename "$file" ".pdf")_ocr.pdf" && rm "$file" ; done

exit 0

Edit 31.07.2023: Added the stat/touch part to reset the modification time after ocrmypdf processed the file.

This will search for all files ending in „*.pdf“ but not „*_ocr.pdf“, it will pipe these path and filenames through to the while read, which will execute for each found file the ocrmypdf for the path and file stored in $file and output to another folder, it will strip the .pdf from the filename and append „_ocr.pdf“ to the filename again.

I tried this with output to the same folder as well, it also works, but for some reason I set it up like this.

… is an easy to use and powerful tool, it can be used to replace part of file name (string1) with some other string. Something one can also do with some „sed“ hack, but this tool is much easier to use, especially for beginners.

You can get all information needed from „rename –help“ (here from Linux Mint):

rename --help
Usage:
    rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr
    [ files ]

Options:
    -v, -verbose
            Verbose: print names of files successfully renamed.

    -n, -nono
            No action: print names of files to be renamed, but don't rename.

    -f, -force
            Over write: allow existing files to be over-written.

    -h, -help
            Help: print SYNOPSIS and OPTIONS.

    -m, -man
            Manual: print manual page.

    -V, -version
            Version: show version number.

    -e      Expression: code to act on files name.

            May be repeated to build up code (like "perl -e"). If no -e, the
            first argument is used as code.

    -E      Statement: code to act on files name, as -e but terminated by
            ';'.

I did use this tool rename some files that contained the string „www“, which I had to replace with „web“, lets say there were files named like this:

www.example.com-97126.ccd
www.example.com-54852.ccd
www.example.com-87430.ccd
www.example.com-75413.ccd

For Ubuntu, maybe also Debian, based systems I use the following command to rename all above files:

rename -e 's/www/web/' *.ccd

After this the files are now called
web.example.com-….
….

The only problem is, the syntax varies from one Linux distribution to another, so check the syntax before you use rename.

For Arch Linux I found that something like this would work:

rename www web *.ccd

The expression „*.ccd“ defines which files to act on, so this will act on all files that match „*.ccd*, in the example above you could also use „www.example.com-*.ccd“ or something similar.

Main thing I wanted to get out there is, there is an easy tool to batch rename files instead of some complex if-for bash code. Which I tried to use before but failed at.

VM runterfahren

Image der VM vergrössern:

sudo qemu-img resize /var/lib/libvirt/images/vm_disk.img +15GB

Je nach Setup ist sudo nicht nötig, durch den Befehl wird die Diskimagedatei um 15 GB vergrößert.

VM starten

Weiter geht es in der VM auf der Konsole.

Partitioen mit parted anzeigen lassen:

parted -l

Ausgabe so oder ähnlich:
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 53,7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 53,7GB 53,7GB primary btrfs boot

Ich habe, bevor ich angefangen habe, die VM heruntergefahren und eine Kopie des Diskimage erstellt. Deswegen habe ich mich dann entschieden die Partition in der laufenden VM anzupassen, grundsätzlich sollte das bei Linux gehen. Auflösung, es hat geklappt. Also weiter.

Disk auswählen bei Start von Parted:

sudo parted /dev/vda

In parted the Partitionen nochmal anzeigen:

print

Ausgabe so oder ähnlich:
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 69,8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number Start End Size Type File system Flags
1 1049kB 53,7GB 53,7GB primary btrfs boot

In parted die Partition (1) vergrößern:

resizepart 1

Dann die Abfragen bestätigen, maximale Größe eingeben (max oder 100% scheinen nicht mehr zu funktionieren) und bestätigen das der Vorgang durchgeführt werden soll trotz das die Partition gemountet ist.

Zu guter Letzt noch Dateisystem (btrfs) vergrössern:

sudo btrfs filesystem resize max /

Anmerkung: Bei anderen Dateisystemen ist der Befehl ein anderer. Im Beispiel ist der Mountpoint „/“, ggf. muss die auch angepasst werden.