Less is more

Searching through a file within the less pager

It is not uncommon when working from the command line to want to view a text file that is too long to fit on the screen. For those instances we can use a pager like less.

$ less some_rather_large_file

Navigating through the file is pretty straight forward. Press the enter or “j” key to scroll the document down one line and press the “k” key to scroll up one line. Press the space bar or “f” to scroll down one “page”, or screen, and press “b” to scroll up. To scroll by half a screen, press “d” (down) and “u” (up). Press “q” to exit less, and press “h” for help.

To search for text within your document press the slash key, “/”, then enter the text you’re looking for. This will search from the current cursor position forward toward the end of the document. Simply type the slash and enter to repeat your search. All instances of the search string that are currently visible on screen will be highlighted and the cursor will move to the next one. To search from the current location backward toward the beginning of the document, type a question mark, “?”, followed by the search text. Enter the question mark alone to repeat the previous search.

By default searches in less are case sensitive. If you want to search for text regardless of case, enter -i. This will switch less into case-insensitive mode until you enter -i again or exit less.

Interestingly, case insensitive searches only work if you enter your search text in all lowercase letters. If even one uppercase letter appears in your search string less will treat it as case sensitive.

Be seeing you.

Those were (not) the days

The cal command’s little secret

In my Linux and Unix classes I introduce the students to a nifty little CLI tool called cal. Run from the command line, it displays a calendar of the current month with today highlighted. If you have a *nix terminal handy, fire it up and let’s take this little baby for a test drive.

Run cal and you get the calendar for the current month. To see the calendar for the entire year, run cal 2018.

You can also ask cal for a specific month and year. To find the date for Thanksgiving in the year 2023, run cal 11 2023. Pass cal the month and year of your birth and you’ll see what day of the week was graced by that momentous occasion.

But do you want to see something cool? Run cal 9 1752. You might notice something wrong here—September of 1752 is missing eleven days! Is cal broken? Hardly. In September of 1752 the British Empire (finally) adopted the Gregorian calendar to correct an error that was, by that time, eleven days long. What’s cool is that a little Unix program already knew all that, so you don’t have to worry about accidentally scheduling a Tardis trip for London, September 8, 1752. That is, assuming the Tardis runs a version of Unix…

So, there’s some trivia for the local pub. Perhaps you can use it to win a few drinks. And watch out for blue police call boxes.

Be seeing you.

Praise the Lord and pass the arguments!

How to use xargs

One interesting utility in Unix is the little xargs command. It takes something from standard input and passes it as an argument to another command. For example, if a command expects a user name, you can get that name from somewhere and feed it to the command through xargs:

whoami | xargs passwd

The silly example above is also superfluous—passwd can accept a user name through standard input directly, so xargs isn’t needed here. And of course, passwd with no arguments would change the current user’s password anyway. But this is a simple way to illustrate what xargs does. Some commands don’t look for a required argument in the standard input stream, so they won’t read directly from the pipe. xargs takes whatever is in the pipe and sends it as an argument to the command that follows; in this case, passwd.

I personally use xargs in my .xinitrc file to set a random background wallpaper on my desktop when I start X:

find ~/pictures/wallpapers -type f | sort -R | tail -1 | xargs feh --bg-fill

The find command locates all the files in my wallpapers directory. The sort command puts the file names in random order and the tail command grabs only the last one in the list. xargs then passes that to feh, an image display utility, which sets the randomly selected file as my wallpaper. And so X greets me with a surprise every time I go GUI.

Be seeing you.

“You keep using that word. I don’t think that word means what you think it means.”

Random vs. urandom

I’ve been told for most of my professional life that /dev/random was “better” than /dev/urandom, that one was inherently more secure and sophisticated than the other. I bought it, because it made sense based upon the little I know about cryptography. And there’s the rub. Most of us aren’t experts in the field. This cool little article delves into some good explanations of what is meant by “random” and why it matters. It may turn some of your preciously-held notions upside down, but that’s okay. The day we stop learning is the day we start dying.

Be seeing you!

I’m just an ordinary average guy.

Load Average: those funny little numbers

I was going to write a little article on interpreting the load average in Unix and Linux machines, but I ran across this blog post and don’t think I could have said it better. Really, check it out.

When I’m logged into a graphical session on my FreeBSD system I use conky to monitor hardware utilization and performance (and the local weather and unread messages in my GMail inbox, etc.) But normally I work from the command line with ssh and tmux, so I have created an alias that I load in my login profile to let me get at the load average in a quick and easy fashion:

alias loads='sysctl vm.loadavg | cut -d" " -f3,4,5'

Now I just run loads any time I want to see how my cpu cores are doing.

Be seeing you.

The Baker Street Regulars

The Basics of Regular Expressions


Unix administrators have long used regular expressions to help them locate files, modify data, and manage system configurations. Tools like grep and sed are designed to process regular expressions to provide the administrator with exactly the information he wants. While versions of these tools have been ported to Windows, most Windows administrators are unaware that they exist. Because of the limitations of the Windows command shell, Windows administrators typically stick with slower, more complicated graphical tools to manage the operating system.

Enter PowerShell. PowerShell is designed to be a replacement for the standard Windows shell, and it is far more powerful and flexible than its predecessor. Among the many command enhancements PowerShell offers is built-in support for regular expressions. It borrows this capability heavily from Perl, a scripting language that was developed specifically for processing text.

Regular expressions are used to search for character sequences inside text strings or files. Programs that process regular expressions look for text that matches a given pattern. The components of a regular expression are not complicated, but the available combinations are many and varied, making it possible to perform some very sophisticated matches. Whether you’re administering Windows, Linux, or the Unix-based macOS, you should invest some time learning the cryptic syntax of regular expressions so that you can manage systems and automate common tasks.

This tutorial will introduce regular expressions. It is not aimed at a particular operating system. Students of both Linux and PowerShell will come away with a basic knowledge of how regular expressions work and how to craft their own. Specific tools such as Linux’s grep command and PowerShell’s -match operator are covered in those respective classes at Centriq Training.

Continue reading