E 2 D N I T D WW II CC KK EE DD CCOO OO LL I O N SS HH EE LL LL SS CC RR II PP TT SS 11 00 11 SS CC RR II PP TT SS LL II NN UU XX OO SS XX FF OO RR ,, ,, (cid:85)(cid:202) UU NN II XX AA NN DD SS YY SS TT EE MM SS (cid:85)(cid:202)(cid:22)(cid:147)(cid:62)(cid:125)(cid:105)(cid:202)(cid:171)(cid:192)(cid:156)(cid:86)(cid:105)(cid:195)(cid:195)(cid:136)(cid:152)(cid:125)(cid:202)(cid:62)(cid:152)(cid:96)(cid:202)(cid:105)(cid:96)(cid:136)(cid:204)(cid:136)(cid:152)(cid:125)(cid:202)(cid:204)(cid:156)(cid:156)(cid:143)(cid:195) D A V E T A Y L O R A N D B R A N D O N P E R R Y (cid:220)(cid:62)(cid:195)(cid:202)(cid:62)(cid:202)(cid:86)(cid:156)(cid:152)(cid:204)(cid:192)(cid:136)(cid:76)(cid:213)(cid:204)(cid:156)(cid:192)(cid:202)(cid:204)(cid:156)(cid:202)(cid:9)(cid:45)(cid:12)(cid:202)(cid:123)(cid:176)(cid:123)(cid:202)(cid:49)(cid:32)(cid:22)(cid:56)(cid:93)(cid:202)(cid:62)(cid:152)(cid:96)(cid:202)(cid:133)(cid:136)(cid:195)(cid:202)(cid:195)(cid:156)(cid:118)(cid:204)(cid:220)(cid:62)(cid:192)(cid:105)(cid:202) (cid:136)(cid:195)(cid:202)(cid:136)(cid:152)(cid:86)(cid:143)(cid:213)(cid:96)(cid:105)(cid:96)(cid:202)(cid:136)(cid:152)(cid:202)(cid:62)(cid:143)(cid:143)(cid:202)(cid:147)(cid:62)(cid:141)(cid:156)(cid:192)(cid:202)(cid:49)(cid:32)(cid:22)(cid:56)(cid:202)(cid:96)(cid:136)(cid:195)(cid:204)(cid:192)(cid:136)(cid:76)(cid:213)(cid:204)(cid:136)(cid:156)(cid:152)(cid:195) (cid:31)(cid:156)(cid:152)(cid:156)(cid:176)(cid:202)(cid:22)(cid:152)(cid:202)(cid:133)(cid:136)(cid:195)(cid:202)(cid:118)(cid:192)(cid:105)(cid:105)(cid:202)(cid:204)(cid:136)(cid:147)(cid:105)(cid:93)(cid:202)(cid:133)(cid:105)(cid:202)(cid:105)(cid:152)(cid:141)(cid:156)(cid:222)(cid:195)(cid:202)(cid:220)(cid:192)(cid:136)(cid:204)(cid:136)(cid:152)(cid:125)(cid:202)(cid:147)(cid:156)(cid:96)(cid:213)(cid:143)(cid:105)(cid:195)(cid:202)(cid:118)(cid:156)(cid:192)(cid:202) (cid:85)(cid:202)(cid:1)(cid:202)(cid:60)(cid:22)(cid:42)(cid:202)(cid:86)(cid:156)(cid:96)(cid:105)(cid:202)(cid:143)(cid:156)(cid:156)(cid:142)(cid:213)(cid:171)(cid:202)(cid:204)(cid:156)(cid:156)(cid:143)(cid:202)(cid:204)(cid:133)(cid:62)(cid:204)(cid:202)(cid:192)(cid:105)(cid:171)(cid:156)(cid:192)(cid:204)(cid:195)(cid:202)(cid:204)(cid:133)(cid:105)(cid:202)(cid:86)(cid:136)(cid:204)(cid:222)(cid:202)(cid:62)(cid:152)(cid:96)(cid:202)(cid:195)(cid:204)(cid:62)(cid:204)(cid:105) (cid:85)(cid:202)(cid:1)(cid:202)(cid:9)(cid:136)(cid:204)(cid:86)(cid:156)(cid:136)(cid:152)(cid:202)(cid:62)(cid:96)(cid:96)(cid:192)(cid:105)(cid:195)(cid:195)(cid:202)(cid:136)(cid:152)(cid:118)(cid:156)(cid:192)(cid:147)(cid:62)(cid:204)(cid:136)(cid:156)(cid:152)(cid:202)(cid:192)(cid:105)(cid:204)(cid:192)(cid:136)(cid:105)(cid:219)(cid:105)(cid:192) (cid:85)(cid:202)(cid:1)(cid:202)(cid:195)(cid:213)(cid:136)(cid:204)(cid:105)(cid:202)(cid:156)(cid:118)(cid:202)(cid:204)(cid:156)(cid:156)(cid:143)(cid:195)(cid:202)(cid:118)(cid:156)(cid:192)(cid:202)(cid:220)(cid:156)(cid:192)(cid:142)(cid:136)(cid:152)(cid:125)(cid:202)(cid:220)(cid:136)(cid:204)(cid:133)(cid:202)(cid:86)(cid:143)(cid:156)(cid:213)(cid:96)(cid:202)(cid:195)(cid:105)(cid:192)(cid:219)(cid:136)(cid:86)(cid:105)(cid:195)(cid:202)(cid:143)(cid:136)(cid:142)(cid:105)(cid:202) PRAISE FOR THE FIRST EDITION OF WICKED COOL SHELL SCRIPTS “A must for any new or intermediate-level Linux administrator. This is the book that every other publisher tries to imitate.” —LINUXWORLD “I can’t give this book enough praise and high enough recommendation. Quite simply, this is the book that I wish I would’ve had when I was just learn- ing Linux.” —STEVE SUEHRING, LINUXWORLD EDITOR “A great resource for intermediate to experienced shell programmers.” —WEBDEVREVIEWS “If you’re interested in the command line and shell scripting, this is an excellent book which will extend your knowledge and provide more than a hundred ready-to-run scripts.” —MACBLOG.COM “There are a few books in the [computer] field that remain useful for years and years after their publishing date. Wicked Cool Shell Scripts is one of those fortunate few.” —THE BRAINSHED “Incredibly fun (really!) and chock full of scripts.” —SLASHDOT W I C K E D C O O L S H E L L S C R I P T S 2 N D E D I T I O N 101 Scripts for Linux, OS X, and UNIX Systems by Dave Taylor and Brandon Perry San Francisco WICKED COOL SHELL SCRIPTS, 2ND EDITION. Copyright © 2017 by Dave Taylor and Brandon Perry. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. 20 19 18 17 16 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-602-8 ISBN-13: 978-1-59327-602-7 Publisher: William Pollock Production Editor: Laurel Chun Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editor: Liz Chadwick Technical Reviewer: Jordi Gutiérrez Hermoso Additional Technical Reviewers: Therese Bao, Mark Cohen, Matt Cone, Grant McWilliams, and Austin Seipp Copyeditor: Paula L. Fleming Compositors: Laurel Chun and Janelle Ludowise Proofreader: James Fraleigh Indexer: Nancy Guenther For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; [email protected] www.nostarch.com The Library of Congress has catalogued the first edition as follows: Taylor, Dave. Wicked cool shell scripts / Dave Taylor. p. cm. ISBN 1-59327-012-7 1. UNIX (Computer file) 2. UNIX Shells. I. Title. QA76.76.O63T3895 2004 005.4'32--dc22 2003017496 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. About the Authors Dave Taylor has been in the computer industry since 1980. He was a contributor to BSD 4.4 UNIX, and his software is included in all major UNIX distributions. He is an award-winning public speaker and has written thousands of magazine and newspaper articles. He is the author of more than 20 books, including Learning Unix for OS X (O’Reilly Media), Solaris 9 for Dummies (Wiley Publishing), and Sams Teach Yourself Unix in 24 Hours (Sams Publishing). A popular columnist for Linux Journal magazine, he also maintains a customer tech support and gadget reviews website, askdavetaylor.com. Brandon Perry started writing C# applications with the advent of the open source .NET implementation Mono. In his free time, he enjoys writing modules for the Metasploit framework, parsing binary files, and fuzzing things. About the Technical Reviewer Jordi Gutiérrez Hermoso is a coder, mathematician, and hacker-errant. He has run Debian GNU/Linux exclusively since 2002, both at home and at work. Jordi is involved with GNU Octave, a free numerical com- puting environment largely compatible with Matlab, and with Mercurial, a distributed version-control system. He enjoys pure and applied math- ematics, skating, swimming, and knitting. Lately he’s been thinking a lot about greenhouse gas emissions and rhino conservation efforts. BRIEF CONTENTS Acknowledgments .................................................xxiii Introduction...................................................... xxv Chapter 0: A Shell Scripts Crash Course................................... 1 Chapter 1: The Missing Code Library..................................... 9 Chapter 2: Improving on User Commands ................................ 51 Chapter 3: Creating Utilities .......................................... 79 Chapter 4: Tweaking Unix ........................................... 97 Chapter 5: System Administration: Managing Users......................... 117 Chapter 6: System Administration: System Maintenance...................... 145 Chapter 7: Web and Internet Users .................................... 173 Chapter 8: Webmaster Hacks........................................ 199 Chapter 9: Web and Internet Administration.............................. 217 Chapter 10: Internet Server Administration ............................... 235 Chapter 11: OS X Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Chapter 12: Shell Script Fun and Games ................................ 273 Chapter 13: Working with the Cloud................................... 299 Chapter 14: ImageMagick and Working with Graphics Files .................. 313 Chapter 15: Days and Dates......................................... 329 Appendix A: Installing Bash on Windows 10 ............................. 341 Appendix B: Bonus Scripts .......................................... 345 Index ......................................................... 355