Linux printing อาจเป็นความท้าทาย โดยเฉพาะสำหรับ new users ใน tutorial นี้ เราแนะนำคำสั่ง lp ใน Linux และแสดงวิธีใช้สำหรับ basic printing operations ครอบคลุมการพิมพ์ portrait และ landscape mode, single และ multiple copies และอื่นๆ
คำสั่ง lp: การแนะนำพื้นฐาน
ใน Linux Common UNIX Printing System (CUPS) เป็น layer ที่จัดการ printer และ printing options/services รวมถึง printers, printing jobs, และ queues
CUPS layer มี options หลายอย่างช่วย setup และใช้ printer บน Linux system คำสั่ง lp หรือ "Line Printer" เป็นหนึ่งในคำสั่งภายใน CUPS layer
lp submit ไฟล์เพื่อพิมพ์ ส่วน lpq หรือ "Line Printer Queue" ใช้ดู print jobs ใน queue
เหมือน Linux terminal commands อื่นๆ lp รองรับ options ต่างๆ มาดูบางอย่าง
วิธีติดตั้งคำสั่ง lp
บางกรณี อาจไม่มี lp command ติดตั้งในระบบ ต้องติดตั้ง lp ก่อนใช้
ตรวจสอบว่ามี lp installed หรือไม่ ด้วยคำสั่ง which:
which lp
หากมี lp installed คำสั่งจะแสดง file path ตัวอย่าง output:
/usr/bin/lp
หากไม่มี lp command ติดตั้ง ใช้ apt package manager ติดตั้ง:
sudo apt install lprng
Syntax ทั่วไปสำหรับใช้ lp command:
lp -d [printer-id] [filename]
แทนที่ [filename] ด้วย path ไปยัง target file
ใช้ lp command แสดง printers ที่พร้อมใช้งาน
ใช้คำสั่ง lpstat ด้วย -a option แสดง printers ที่เชื่อมต่อและพร้อมใช้งาน และ -d option แสดง default printer Syntax:
lpstat -a | awk '{print $1}'
คำสั่งควรคืนชื่อ printers ที่พร้อมใช้งาน
ใช้ lp command พิมพ์ไปยัง printer เฉพาะ
กรณีส่วนใหญ่มีแค่ 1 printer เชื่อมต่อ จะใช้แค่ lp ตามด้วย filename พิมพ์ไป default printer
แต่บางกรณีมีหลาย printers ต้องใช้ -d option พิมพ์ไป printer เฉพาะ
ตัวอย่าง พิมพ์ "/home/debian/mte/hello.txt" ไป HP Ink-Tank 310 printer:
lp -d "Ink-Tank-310" /home/debian/mte/hello.txt
ใช้ lp command แสดง print queue
แสดง print queue จาก command line ใช้คำสั่ง lpq
หมายเหตุ: หากคำสั่งไม่มีในระบบ ติดตั้ง cups-bsd package:
sudo apt install cups-bsd
หลังติดตั้ง package แล้ว รันคำสั่ง:
lpq -P [printer-name]
คำสั่งควรให้ output:
lpq -P "Ink-Tank-310" Ink-Tank-310 is ready no entries
แสดงว่า printer ที่ระบุไม่มี printing jobs สามารถพิมพ์เอกสารได้
ใช้ lp command พิมพ์ multiple copies
พิมพ์จำนวน copies เฉพาะของไฟล์ ใช้ -n option ของ lp command Syntax:
lp -n
ตัวอย่าง พิมพ์ 10 copies ของ "hello.txt" ไป printer "HP-ink-jet 315":
lp -d "Ink-Tank-310" -n 10 hello.txt
ใช้ lp command พิมพ์ portrait หรือ landscape
ใช้ lp command พิมพ์ใน 2 orientations หลัก: portrait และ landscape ด้วย -o option
หมายเหตุ: –o option versatile มาก นอกจากระบุ print job orientation ยังตั้งค่า paper size ได้
พิมพ์ตัวอย่างก่อนหน้าใน portrait orientation:
lp -d "Ink-Tank-310" –n 10 -o portrait hello.txt
พิมพ์ landscape ระบุ "landscape" ใน syntax:
lp -d "Ink-Tank-310" -n 10 -o landscape hello.txt
เหมือนที่กล่าว –o มี options อื่นๆ เช่น ตั้งค่า paper size
พิมพ์ไฟล์ก่อนหน้าใน letter size ระบุ size ด้วย lp command และ "media" attribute ตัวอย่าง syntax:
lp -d "Ink-Tank-310" –n 10 -o portrait -o media=letter hello.txt
lp command สำหรับ single หรือ double-sided printing
ใช้ lp command ด้วย "sides" attribute ระบุ single- หรือ double-sided printing jobs
"sides" attribute มี 2 key values:
- two-sided-short-edge – landscape mode
- two-sided-long-edge – portrait mode
ตัวอย่าง พิมพ์ two-sided page สำหรับตัวอย่างก่อนหน้า syntax:
lp -d "Ink-Tank-310" –n 10 -o portrait -o media=letter –o sides=two-sided-long-edge hello.txt
lp command options
options อื่นๆ ของ lp command:
| Option | Operation |
|---|---|
| -E | Forces encryption สำหรับ server connections |
| -- (marks the end of options) | ใช้พิมพ์ไฟล์ที่ชื่อขึ้นต้นด้วย dash (-) |
| -d (destination) | ใช้พิมพ์ไฟล์ไป destination printer |
| -U (username) | ระบุ username สำหรับเชื่อมต่อ server |
| -h | ระบุ server hostname |
| -m | ส่ง notification (email) หลัง job เสร็จ |
| -n (copies) | ระบุจำนวน document print copies (1-100) |
| -q (priority) | ตั้งค่า priority ของ printing jobs ใน queue 1=lowest, 50=default, 100=highest |
| -i (job-id) | ระบุ current job ที่จะ modify |
| -t (name) | ตั้งชื่อให้ job |
| -o ("name=value [name=value …]") | ตั้งค่า job options (หนึ่งหรือหลาย) |
เรียนรู้ lp supported options ทั้งหมด ได้ที่ CUPS man pages
สรุป
Tutorial นี้แสดงวิธีทำงานกับ lp command ใน Linux พิมพ์จาก terminal หากมีปัญหาตั้งค่า printer ดู tutorial ตั้งค่า printer ใน Linux