"Dogs" of the Linux Shell
One incarnation of the so called 80/20 rule has been associated with software systems. It has been observed that 80% of a user population regularly uses only 20% of a system's features. Without backing this up with hard statistics, my 20+ years of building and using software systems tells me that this hypothesis is probably true. The collection of Linux command-line programs is no exception to this generalization. Of the dozens of shell-level commands offered by Linux, perhaps only ten commands are commonly understood and utilized, and the remaining majority are virtually ignored.
Which of these dogs of the Linux shell have the most value to offer? I'll briefly describe ten of the less popular but useful Linux shell commands, those which I have gotten some mileage from over the years. Specifically, I've chosen to focus on commands that parse and format textual content.
The working examples presented here assume a basic familiarity with command-line syntax, simple shell constructs and some of the not-so-uncommon Linux commands. Even so, the command-line examples are fairly well commented and straightforward. Whenever practical, the output of usage examples is presented under each command-line execution.
The following eight commands parse, format and display textual content. Although not all provided examples demonstrate this, be aware that the following commands will read from standard input if file arguments are not presented.
As their names imply, head and tail are used to display some amount of the top or bottom of a text block. head presents beginning of a file to standard output while tail does the same with the end of a file. Review the following commented examples:
## (1) displays the first 6 lines of a file head -6 readme.txt ## (2) displays the last 25 lines of a file tail -25 mail.txt
Here's an example of using head and tail in concert to display the 11th through 20th line of a file.
# (3) head -20 file | tail -10
Manual pages show that the tail command has more command-line options than head. One of the more useful tail option is -f. When it is used, tail does not return when end-of-file is detected, unless it is explicitly interrupted. Instead, tail sleeps for a period and checks for new lines of data that may have been appended since the last read.
## (4) display ongoing updates to the given ## log file tail -f /usr/tmp/logs/daemon_log.txt
Imagine that a dæmon process was continually appending activity logs to the /usr/adm/logs/daemon_log.txt file. Using tail -f at a console window, for example, will more or less track all updates to the file in real time. (The -f option is applicable only when tail's input is a file).
If you give multiple arguments to tail, you can track several log files in the same window.
## track the mail log and the server error log ## at the same time. tail -f /var/log/mail.log /var/log/apache/error_log
What is cat spelled backwards? Well, that's what tac's functionality is all about. It concatenates file order and their contents in reverse. So what's its usefulness? It can be used on any task that requires ordering elements in a last-in, first-out (LIFO) manner. Consider the following command line to list the three most recently established user accounts from the most recent through the least recent.
# (5) last 3 /etc/passwd records - in reverse $ tail -3 /etc/passwd | tac curly:x:1003:100:3rd Stooge:/homes/curly:/bin/ksh larry:x:1002:100:2nd Stooge:/homes/larry:/bin/ksh moe:x:1001:100:1st Stooge:/homes/moe:/bin/ksh
nl is a simple but useful numbering filter. I displays input with each line numbered in the left margin, in a format dictated by command-line options. nl provides a plethora of options that specify every detail of its numbered output. The following commented examples demonstrate some of of those options:
# (6) Display the first 4 entries of the password # file - numbers to be three columns wide and # padded by zeros. $ head -4 /etc/passwd | nl -nrz -w3 001 root:x:0:1:Super-User:/:/bin/ksh 002 daemon:x:1:1::/: 003 bin:x:2:2::/usr/bin: 004 sys:x:3:3::/: # # (7) Prepend ordered line numbers followed by an # '=' sign to each line -- start at 101. $ nl -s= -v101 Data.txt 101=1st Line ... 102=2nd Line ... 103=3rd Line ... 104=4th Line ... 105=5th Line ... .......
Today five randomly drawn entrants will win:
Rock, Paper, Scissors, Lizard, Spock mug and LJ subscription
Enter to Win
|December Daily Giveaways are Back!||Dec 01, 2015|
|December 2015 Video Preview||Nov 30, 2015|
|Take Control of Your PC with UEFI Secure Boot||Nov 30, 2015|
|Geek Hide-away in Guatemala - Stay for Free!||Nov 26, 2015|
|Microsoft and Linux: True Romance or Toxic Love?||Nov 25, 2015|
|Non-Linux FOSS: Install Windows? Yeah, Open Source Can Do That.||Nov 24, 2015|
- Take Control of Your PC with UEFI Secure Boot
- Cipher Security: How to harden TLS and SSH
- Non-Linux FOSS: Install Windows? Yeah, Open Source Can Do That.
- December Daily Giveaways are Back!
- Web Stores Held Hostage
- Firefox's New Feature for Tighter Security
- Microsoft and Linux: True Romance or Toxic Love?
- Geek Hide-away in Guatemala - Stay for Free!
- diff -u: What's New in Kernel Development
- PuppetLabs Introduces Application Orchestration