Here is a simple shell script for Linux that runs quickly and can convert multiple text file types to PDF with one command.

Getting Started

Check to make sure you have LibreOffice installed. On major distributions like Debian/Ubuntu or RHEL/Fedora, it will be. If not, issue the following command or install it with your GUI package manager.

Debian/Ubuntu-based distros:

sudo apt update && sudo apt install libreoffice -y

    RHEL/Fedora-based distros:

    sudo dnf install libreoffice -y

    Code

    #!/bin/bash
    
    # Introductory information
    echo "batch_pdf is a simple program that will allow you to quickly batch convert text files to PDF."
    echo "You will be asked to enter the absolute path of the directory containing files to be converted." 
    echo "Allowed file types are .txt, .doc, .docx, .odt, and .md."
    
    # dir_location is the directory of files to be converted
    read -p "Type or copy/paste (Ctrl-Shift-V) absolute path here: " dir_location
    
    # Change directory to the user-input file location
    cd $dir_location
    
    # Call soffice binary to convert any file within the directory that ends with the following extensions
    soffice --convert-to pdf {*.docx,*.doc,*.odt,*.md,*.txt}
    
    exit

    You can copy/paste this code directly into any editor on Linux. I usually use Nano for simplicity and so here are some instructions. First, open a terminal and create a new file in the directory where you want to save your scripts. Of course, use the file path that applies to your directory structure.

    sudo nano /home/andrew/Documents/Scripts/bash_pdf.sh

    If you’ve already copied the code block above, use Ctrl-Shift-V to copy it into Nano. Then, Ctrl-X to quit and y to save.

    Last step is to make the file executable:

    sudo chmod +x /home/andrew/Documents/Scripts/bash_pdf.sh

    To run the file, open a terminal from the directory where the script is stored. In most file managers you can just right-click and look for an option such as “Open in Terminal.”

    Here is my Documents directory showing 4 text files of different types:

    …and here is the same location after running the script:

    That’s it — enjoy quickly converting files!!

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *