Free Sample
+ Collection

Linux Shell Scripting Cookbook

Sarath Lakshman

Get straight into writing scripts with this recipe-packed cookbook. Ideal as an end-to-end beginner’s guide or as a "dip-in" reference for experts, it helps you gain control over Linux through the powerful shell scripting language.
RRP $26.99
RRP $44.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781849513760
Paperback360 pages

About This Book

  • 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

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.

Table of Contents

Chapter 1: Shell Something Out
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
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 filenames based on extension
Renaming and moving files in bulk
Spell checking and dictionary manipulation
Automating interactive input
Chapter 3: File In, File Out
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
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!
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
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
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
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
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

What You Will Learn

  • 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.


Read More