APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Printing multiple files


Printing multiple files


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 this page was useful to you, please help others find it:  





Comments?




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!


Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Linux
       - MacOSX
       - Printing
       - Scripting
       - Shell















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)