Linux Shell Scripting Cookbook

There is a newer version of this book available - Linux Shell Scripting Cookbook, Second Edition
Linux Shell Scripting Cookbook
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $71.98    Print cover: $44.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Master the art of crafting one-liner command sequence to perform tasks such as text processing, digging data from files, and lot more
  • Practical problem solving techniques adherent to the latest Linux platform
  • Packed with easy-to-follow examples to exercise all the features of the Linux shell scripting language
  • Part of Packt's Cookbook series: Each recipe is a carefully organized sequence of instructions to complete the task as efficiently as possible

Book Details

Language : English
Paperback : 360 pages [ 235mm x 191mm ]
Release Date : January 2011
ISBN : 1849513767
ISBN 13 : 9781849513760
Author(s) : Sarath Lakshman
Topics and Technologies : All Books, Other, Cookbooks, Linux Servers, Open Source

Table of Contents

Chapter 1: Shell Something Out
Chapter 2: Have a Good Command
Chapter 3: File In, File Out
Chapter 4: Texting and Driving
Chapter 5: Tangled Web? Not At All!
Chapter 6: The Backup Plan
Chapter 7: The Old-boy Network
Chapter 8: Put on the Monitor's Cap
Chapter 9: Administration Calls
  • Chapter 1: Shell Something Out
    • Introduction
    • Printing in the terminal
    • Playing with variables and environment variables
    • Doing math calculations with the shell
    • Playing with file descriptors and redirection
    • Arrays and associative arrays
    • Visiting aliases
    • Grabbing information about terminal
    • Getting, setting dates, and delays
    • Debugging the script
    • Functions and arguments
    • Reading the output of a sequence of commands
    • Reading "n" characters without pressing Return
    • Field separators and iterators
    • Comparisons and tests
    • Chapter 2: Have a Good Command
      • Introduction
      • Concatenating with cat
      • Recording and playback of terminal sessions
      • Finding files and file listing
      • Playing with xargs
      • Translating with tr
      • Checksum and verification
      • Sorting, unique and duplicates
      • Temporary file naming and random numbers
      • Splitting files and data
      • Slicing file names based on extension
      • Renaming and moving files in bulk
      • Spell checking and dictionary manipulation
      • Automating interactive input
      • Chapter 3: File In, File Out
        • Introduction
        • Generating files of any size
        • Intersection and set difference (A-B) on text files
        • Finding and deleting duplicate files
        • Making directories for a long path
        • File permissions, ownership, and sticky bit
        • Making files immutable
        • Generating blank files in bulk
        • Finding a symbolic link and its target
        • Enumerating file type statistics
        • Loopback files and mounting
        • Creating ISO files, Hybrid ISO
        • Finding difference between files, patching
        • head and tail – printing the last or first 10 lines
        • Listing only directories – alternative methods
        • Fast command-line navigation using pushd and popd
        • Counting number of lines, words, and characters in a file
        • Printing directory tree
        • Chapter 4: Texting and Driving
          • Introduction
          • Basic regular expression primer
          • Searching and mining "text" inside a file with grep
          • Column-wise cutting of a file with cut
          • Frequency of words used in a given file
          • Basic sed primer
          • Basic awk primer
          • Replacing strings from a text or file
          • Compressing or decompressing JavaScript
          • Iterating through lines, words, and characters in a file
          • Merging multiple files as columns
          • Printing the nth word or column in a file or line
          • Printing text between line numbers or patterns
          • Checking palindrome strings with a script
          • Printing lines in the reverse order
          • Parsing e-mail addresses and URLs from text
          • Printing n lines before or after a pattern in a file
          • Removing a sentence in a file containing a word
          • Implementing head, tail, and tac with awk
          • Text slicing and parameter operations
          • Chapter 5: Tangled Web? Not At All!
            • Introduction
            • Downloading from a web page
            • Downloading a web page as formatted plain text
            • A primer on cURL
            • Accessing Gmail from the command line
            • Parsing data from a website
            • Image crawler and downloader
            • Web photo album generator
            • Twitter command-line client
            • define utility with Web backend
            • Finding broken links in a website
            • Tracking changes to a website
            • Posting to a web page and reading response
            • Chapter 6: The Backup Plan
              • Introduction
              • Archiving with tar
              • Archiving with cpio
              • Compressing with gunzip (gzip)
              • Compressing with bunzip (bzip)
              • Compressing with lzma
              • Archiving and compressing with zip
              • squashfs – the heavy compression filesystem
              • Cryptographic tools and hashes
              • Backup snapshots with rsync
              • Version control based backup with Git
              • Cloning hard drive and disks with dd
              • Chapter 7: The Old-boy Network
                • Introduction
                • Basic networking primer
                • Let's ping!
                • Listing all the machines alive on a network
                • Transferring files
                • Setting up an Ethernet and wireless LAN with script
                • Password-less auto-login with SSH
                • Running commands on remote host with SSH
                • Mounting a remote drive at a local mount point
                • Multi-casting window messages on a network
                • Network traffic and port analysis
                • Chapter 8: Put on the Monitor's Cap
                  • Introduction
                  • Disk usage hacks
                  • Calculating execution time for a command
                  • Information about logged users, boot logs, and failure boot
                  • Printing the 10 most frequently-used commands
                  • Listing the top 10 CPU consuming process in a hour
                  • Monitoring command outputs with watch
                  • Logging access to files and directories
                  • Logfile management with logrotate
                  • Logging with syslog
                  • Monitoring user logins to find intruders
                  • Remote disk usage health monitor
                  • Finding out active user hours on a system
                  • Chapter 9: Administration Calls
                    • Introduction
                    • Gathering information about processes
                    • Killing processes and send or respond to signals
                    • which, whereis, file, whatis, and loadavg explained
                    • Sending messages to user terminals
                    • Gathering system information
                    • Using /proc – gathering information
                    • Scheduling with cron
                    • Writing and reading MySQL database from Bash
                    • User administration script
                    • Bulk image resizing and format conversion

                    Sarath Lakshman

                    Sarath Lakshman is a 23 year old who was bitten by the Linux bug during his teenage years. He is a software engineer working in ZCloud engineering group at Zynga, India. He is a life hacker who loves to explore innovations. He is a GNU/Linux enthusiast and hactivist of free and open source software. He spends most of his time hacking with computers and having fun with his great friends. Sarath is well known as the developer of SLYNUX (2005)—a user friendly GNU/Linux distribution for Linux newbies. The free and open source software projects he has contributed to are PiTiVi Video editor, SLYNUX GNU/Linux distro, Swathantra Malayalam Computing, School-Admin, Istanbul, and the Pardus Project. He has authored many articles for the Linux For You magazine on various domains of FOSS technologies. He had made a contribution to several different open source projects during his multiple Google Summer of Code projects. Currently, he is exploring his passion about scalable distributed systems in his spare time. Sarath can be reached via his website

                    Code Downloads

                    Download the code and support files for this book.

                    Submit Errata

                    Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                    - 5 submitted: last submission 30 Dec 2013

                    Errata type: Code | Page number: 12

                    The output of:

                    echo -e "1\t2\t3" 123

                    This should be:

                    echo -e "1\t2\t3" 1 2 3

                    Errata type: Technical | Page number: 12 | Errata Date: 12 Feb 2012

                    In Printing colored output after echo command - Here \e[1;31 should be changed to \e[1;31m


                    Errata type: Technical | Page number: 22 | Errata Date: 13 Feb 2012

                    line which says:
                    Note that cat: a1: Permission denied does not appear because it belongs to stdin
                    stdin must be changed to stderr


                    Errata type: Code | Page number: 28,34

                    On page number 28 and 34, replace $@ with "$@"

                    Errata type: Code | Page number: 19

                    echo "10^10 | bc # square"  should be changed to echo "10^2" | bc # square


                    Sample chapters

                    You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                    Frequently bought together

                    Linux Shell Scripting Cookbook +    ADempiere 3.6 Cookbook =
                    50% Off
                    the second eBook
                    Price for both: $44.10

                    Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                    What you will learn from this book

                    • Explore a variety of regular usage tasks and how it can be made faster using shell command
                    • Couple frequently used commands like grep, find, sed, awk
                    • Write shell scripts that can dig data from web and process it with few lines of code
                    • Perform and automate tasks such as automating backups and restore with archiving tools
                    • Understand file systems, file types and manipulations
                    • Create and maintain file/folder archives, compression formats and encrypting techniques with shell
                    • Set up Ethernet and Wireless LAN with the shell script
                    • Monitor different activities on the network using logging techniques

                    In Detail

                    GNU/Linux is a remarkable operating system that comes with a complete development environment that is stable, reliable, and extremely powerful. The shell being the native interface to communicate with the operating system is capable of controlling the entire operating system. There are numerous commands on Linux shell which are documented but hard to understand. The man pages are helpful but they are very lengthy and it does not give any clues on key areas where commands can be used. Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential.

                    Linux Shell Scripting Cookbook is a collection of essential command-line recipes along with detailed descriptions tuned with practical applications. It covers most of the commands on Linux with a variety of usecases accompanied by plenty of examples. This book helps you to perform complex data manipulations involving tasks such as text processing, file management, backups and more with the combination of few commands.

                    Linux Shell Scripting Cookbook shows you how to capitalize on all the aspects of Linux using the shell scripting language. This book teaches you how to use commands to perform simple tasks all the way to scripting complex tasks such as managing large amounts of data on a network.

                    It guides you on implementing some of the most common commands in Linux with recipes that handle any of the operations or properties related with files like searching and mining inside a file with grep. It also shows you how utilities such as sed, awk, grep, cut can be combined to solve text processing related problems. The focus is on saving time by automating many activities that we perform interactively through as browser with a few lines of script.

                    This book will take you from a clear problem description to a fully functional program. The recipes contained within the chapter will introduce the reader to specific problems and provide hands-on solutions.

                    This practical book will help you gain control over all the aspects of Linux using the powerful shell scripting language


                    This book is written in cookbook style and it offers learning through recipes with examples and illustrations. Each recipe contains step-by-step instructions about everything necessary to execute a particular task. The book is designed so that you can read it from start to end for beginner's or just open up any chapter and start following the recipes as a reference for advanced users.

                    Who this book is for

                    If you are a beginner or an intermediate user who wants to master the skill of quickly writing scripts to perform various tasks without reading the entire man pages, this book is for you. You can start writing scripts and one-liners by simply looking at the similar recipe and its descriptions without any working knowledge of shell scripting or Linux. Intermediate/advanced users as well as system adminstrators/ developers and programmers can use this book as a reference when they face problems while coding.

                    Code Download and Errata
                    Packt Anytime, Anywhere
                    Register Books
                    Print Upgrades
                    eBook Downloads
                    Video Support
                    Contact Us
                    Awards Voting Nominations Previous Winners
                    Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                    Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software