{"authors":["utzer"],"canonical_url":"https://newblog.utzer.de/2023/07/03/ocr-all-pdfs-in-a-folder-by-cron/","categories":["Linux"],"content":"I will not write a long explanation, just this short one.\n\nThe cronjob looks like this:\n\n```\n* * * * * sh -c 'flock -n /tmp/my.lockfile /home/user/scripts/ocr_pdf_scan.sh'\n```\n\nThis 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.\n\nThe cron job runs every minute, so in crontab it has \\* \\* \\* \\* \\* (five stars in front of the command).\n\nIn the ocr\\_pdf\\_scan.sh I have this:\n\n```\n#!/bin/bash\n\nfind /home/user/scans/ocr -type f \\( -iname \"*.pdf\" -and -not -iname \"*_ocr.pdf\" \\) | while read file ; do OLDTimestamp=$(stat -c \"%Y\" \"$file\") \u0026\u0026 ocrmypdf -q -l deu+eng --rotate-pages --rotate-pages-threshold 8 -c -s \"$file\" /home/user/scans/ocr_ready/\"$(basename \"$file\" \".pdf\")_ocr.pdf\" \u0026\u0026 touch -d @$OLDTimestamp /home/user/scans/ocr_ready/\"$(basename \"$file\" \".pdf\")_ocr.pdf\" \u0026\u0026 rm \"$file\" ; done\n\nexit 0\n```\n\nEdit 31.07.2023: Added the stat/touch part to reset the modification time after ocrmypdf processed the file.\n\nThis 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.\n\nI tried this with output to the same folder as well, it also works, but for some reason I set it up like this.\n","content_format":"markdown","date":"2023-07-03T08:27:56Z","description":"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 preve","html_url":"https://newblog.utzer.de/2023/07/03/ocr-all-pdfs-in-a-folder-by-cron/","json_url":"https://newblog.utzer.de/2023/07/03/ocr-all-pdfs-in-a-folder-by-cron/index.json","lastmod":"2023-07-03T08:27:56Z","markdown_url":"https://newblog.utzer.de/2023/07/03/ocr-all-pdfs-in-a-folder-by-cron/index.md","media":[],"schema_version":1,"tags":["cli","cron","ocr","ocrmypdf"],"title":"OCR all PDFs in a folder by cron","type":"blog_post"}