# # Printing multiple files
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Printing multiple files

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© August 2013 Anthony Lawrence

2013/08/01

I have 25 .txt files that I would like to print. They are named 1.txt, 2.txt and so on. How can I do this?

I assume that "lpr *.txt" isn't suitable, either because there are other .txt files that you don't want to print or because you want the jobs to come out of the printer in a specific order.

You could do something like this:


lpr [1-9].txt 1[0-9].txt 2[0-5].txt
 

Or perhaps even:

lpr `ls *.txt | sort -n`
 

Too much typing? Assuming a current Bash, you can do:

lpr {1..25}.txt
 

That braces trick is even more powerful than that; see Bash 3.00 brace expansion.

Maybe you want some visual clue as to what's happening because it's 250 files, not 25. In that case, a loop will do it. You could use:

for i in {1..25} 
do echo "Now printing $i.txt"
lpr $i.tx
done
 

or

x=1;while [ $x -lt 26 ]; do  echo "Now printing $x.txt;lpr $x.txt; x=$((x+1));done
 

For conditions where it isn't ".txt" but something like file25foo, use "file${x}foo".

When it gets even more complicated, Bash extended globbing can be useful:

shopt -s extglob
lpr !([A-z]*.txt)
 

That prints everything that doesn't match [A-z]*.txt.

Test out patterns with "echo" before wasting a bunch of paper:

$ echo +(1*|3*)
1.txt 10.txt 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 18.txt 19.txt 3.txt
 

Let's not forget where you want to add a title or whatever:

for in in {1..25} 
do
(echo "    This is $i.txt";cat $i.txt) | lpr
done
 

And of course we could get MUCH fancier if desired.

Errors

If your script is expecting to find certain files but does not, what happens?

for i in *.foo
do
lpr $i
done
 

That spits back '*.foo' and lpr would complain:

lpr: Error - unable to access "*.foo" - No such file or directory
 

That might be annoying. You could suppress stderr:

for i in *.foo
do
lpr $i 2>/dev/null
done
 

Or just avoid missing files:

for i in *.txt *.foo
do 
if test -e $i
 then lpr $i
fi
done
 

A shorter version:

for i in *.txt *.foo
do 
test -e $i && echo $i
done
 

What if some of those files were not text? That could make a mess of your printer output tray, couldn't it? There's a "file" command that could help and also avoid wasting time on empty files.

for i in *.txt *.foo
do
test -n "`file $i | grep text`" && lpr $i
done
 

That avoids everything but 1.txt and 10.txt in this directory:

$ file *
1.txt:  ASCII text
10.txt: ASCII text
11.txt: empty
12.txt: JPEG image data, EXIF standard
13.txt: empty
14.txt: empty
15.txt: empty
16.txt: empty
17.txt: empty
18.txt: empty
19.txt: empty
2.txt:  empty
20.txt: empty
21.txt: empty
22.txt: empty
23.txt: empty
24.txt: empty
25.txt: empty
3.txt:  empty
4.txt:  empty
5.txt:  empty
6.txt:  empty
7.txt:  empty
8.txt:  empty
9.txt:  empty
 

Putting some of these ideas together could result in a pretty powerful script that would do exactly what you want in every case. However, I will add this: once it gets even slightly complicated, I turn to Perl. It's not that you can't do complex jobs with shell scripts, but that twisting around shell script limitations can get quite painful. I like to avoid that pain.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Printing multiple files


Inexpensive and informative Apple related e-books:

Take Control of IOS 11

Sierra: A Take Control Crash Course

Photos for Mac: A Take Control Crash Course

Take control of Apple TV, Second Edition

Photos: A Take Control Crash Course





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





Principles have no real force except when one is well-fed. (Mark Twain)




Linux posts

Troubleshooting posts


This post tagged:

Linux

Printing

Scripting

Shell



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode