Yes… No and Other Corners of Coreutils

About a decade ago, I wanted to do some deep program analysis on GNU Coreutils. The analysis didn’t get too far, but, in the process, I did become familiar with some lesser-known Coreutils functionality that’s come in handy surprisingly often since then, shared below.

Automating Responding to Prompts

The yes program, by default, prints lines containing “y” endlessly to its output. This is meant to automatically drive a process that asks for repeated confirmations. Surprisingly, yes can take arguments, in which case, instead of “y,” yes prints its arguments, in order, separated by a single space between each. So, for example, you can use yes n to deny, rather than accept, all confirmation prompts.

My main use for yes with arguments has nothing to do with confirmation prompts, however. Rather, every now and then, some process that sends mail goes haywire, creating a mess that needs to be cleaned up via the mail program. Rather than open mail and press the d key over and over to delete everything, I run yes d | head -100 | mail.

Joining on Columns

The join program allows joining records by column, which can often eliminate the need to import data into another system like a database or spreadsheet.

Finding Common Lines

The diff program for finding lines that differ between two files is well known. When you want to know what lines two files have in common, use comm.

Sampling Lines

The shuf program outputs lines in a random order. This is useful, especially in combination with head, for getting a random sample.