Updated Tue Feb 14 20:00:47 EST 2023

Command-line Tips and Tricks

I keep remembering things that I meant to mention but they slipped my mind. Or things that I should have mentioned but didn't even think about. This page will be an on-going list. With luck it will help a bit.

If you discover something that you think would help everyone, let me know and I'll add it here.

Terminating a program

You can type at most programs and see what they do; for example

$ tr A-Z a-z
Hello World
hello world
But how do you say you're done?

You can type control-C to kill the program; no output is produced, and the program just dies.

Or you can type control-D to send the program an "end of file" signal, which is equivalent to it encountering the end of the input when it's reading from a file instead of the keyboard. Use this when you want the program to do its computation and print its results

A few programs, notably more and less, recognize q as a signal to quit.

Don't type so much

If you type part of the name of a file in a command-line, then push tab, the rest of the filename will be typed for you. If there's more than one file that matches, push tab twice and you'll get a list of possible continuations. As I mentioned before, this didn't see to work under some circumstances, but "it works for me."

You can work your way back up the list of previously typed commands in a terminal window with the up-arrow key (and back down with the down-arrow). When you get to the one you want, push Return and the command will be executed. No need to retype things.

You can also edit the command before it gets run; use the left and right arrows to get to the right place in the line, then backspace to get rid of characters and type to add new ones.

Most of the commands in the studio files can be copied and pasted; you definitely don't want to retype them, which is a recipe for doing too much work while making mistakes. Copy, paste, modify if necessary, then push Return.

Look before you leap

And after as well. Get into the habit of running pwd to be sure of where you are before you make a change like unzipping a .zip file.

Run the ls command to see what is in the directory before you do anything else.

After you run a command that creates files or folders, or does something that might change the directory contents, run ls again.

Be VERY careful with the rm command, which does not ask for confirmation before it removes things. And be doubly careful about using wildcards with rm. There are entertaining and occasionally sad stories about misguided rm commands.

RTFM

The man command prints the manual "page" for a command. Unfortunately what used to be one-pagers have become more like long-form essays. And commands that started with one or two options have grown multitudes, often even more cryptic.

The apropos command will sometimes suggest what command you should be thinking of; it is sometimes useful when you can't remember the proper name.