PacktLib: Linux Shell Scripting Cookbook

Linux Shell Scripting Cookbook


About the Author

About the Reviewers


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

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

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

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

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

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

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

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

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