SysTutorials

Printing Text Files from Linux Terminal with lp and CUPS

· updated April 11, 2026

The lp command is the standard tool for printing from the Linux command line. It interfaces with CUPS (Common Unix Printing System), the printing backbone on virtually all modern Linux distributions.

Basic Printing

Send a file to your default printer:

lp text-file.txt

Specify a particular printer:

lp -d printer-name text-file.txt

List available printers and identify the default:

lpstat -p -d

Formatting Options

Use the -o flag to control paper size, density, and margins. Margins are in points (72 points = 1 inch):

lp -o media=a4 -o cpi=12 -o lpi=7.2 \
   -o page-left=48 -o page-right=48 \
   -o page-top=48 -o page-bottom=48 text-file.txt

Common options:

Print on both sides with 2-up layout:

lp -o sides=two-sided-long-edge -o number-up=2 text-file.txt

View the current print queue:

lpstat -o

Cancel a specific job:

cancel job-id

Cancel all jobs for a printer:

cancel -a printer-name

Piping and Redirection

Print command output directly without creating a file:

cat text-file.txt | lp
ps aux | lp
dmesg | lp -o media=letter

Using enscript for Advanced Formatting

For more sophisticated text-to-print conversion, enscript preprocesses files before sending to the printer. It supports syntax highlighting, headers/footers, and complex layouts:

# Print with syntax highlighting (no header banner)
enscript -B -E text-file.txt

# Print 2 logical pages per physical sheet
enscript -B -2 text-file.txt

# Output to PostScript file first, then print
enscript -B text-file.txt -p output.ps
lp output.ps

Common enscript options:

Pipe directly to printer:

enscript -B -E - | lp

Creating a Print Wrapper Script

For repeated printing with consistent formatting, write a shell script:

#!/bin/bash
# save as ~/bin/print-formatted
# usage: print-formatted [printer] <file>

PRINTER="${1:-default}"
FILE="${2}"

if [ ! -f "$FILE" ]; then
    echo "Error: file '$FILE' not found"
    exit 1
fi

if [ "$PRINTER" = "default" ] && [ -z "$2" ]; then
    FILE="$1"
fi

lp -d "$PRINTER" \
    -o media=a4 \
    -o cpi=12 \
    -o lpi=7.2 \
    -o page-left=48 \
    -o page-right=48 \
    -o page-top=48 \
    -o page-bottom=48 \
    "$FILE"

Make it executable:

chmod +x ~/bin/print-formatted

Use it:

print-formatted myfile.txt
print-formatted my-printer myfile.txt

Installation and Troubleshooting

If lp is unavailable, install CUPS:

Debian/Ubuntu:

sudo apt install cups cups-client

RHEL/Fedora/Rocky/CentOS:

sudo dnf install cups cups-client

Start the CUPS daemon if it’s not running:

sudo systemctl enable cups
sudo systemctl start cups
sudo systemctl status cups

Access Denied Errors

If you can’t print, you may lack permission to access the printer. Add your user to the lpadmin group:

sudo usermod -aG lpadmin $USER

Log out and back in, or start a new shell session:

su - $USER

Verify group membership:

groups

Check CUPS Socket and Permissions

If the lpadmin group addition doesn’t resolve the issue, check that the CUPS socket is accessible:

ls -l /run/cups/cups.sock

The socket should have group lpadmin with read/write permissions. If permissions are wrong, CUPS may not have started properly—restart it:

sudo systemctl restart cups
← All posts