OS X Terminal Tips & Tricks
December 7, 2015 11:12 AM   Subscribe

I intend to teach an intro class to OS X terminal. Basic stuff like navigation, file creation and deletion, etc. What are some tips & tricks that might not be obvious? Assume I am an advanced user and will be teaching to CLI beginners to those only slightly more conversant. This is just going to be an informal "Lunch and learn" style class. Maybe an hour max.
posted by cjorgensen to Computers & Internet (35 answers total) 27 users marked this as a favorite
tab completion in the bash shell
command history via the up and down arrow keys
posted by thelonius at 11:13 AM on December 7, 2015 [6 favorites]

The two things that always save me in a CLI are tabbing to complete a filename, and pwd to figure out where I am. Under OSX, you can also show them dragging a folder into the terminal - useful for when you have a folder in Finder and you want to cd into it in Terminal.
posted by neilbert at 11:14 AM on December 7, 2015 [2 favorites]

You can arbitrarily position the cursor by option-clicking, rather than left-right arrowing through.

Depending on which shell, showing that you can type the first few letters of a command before up/down arrowing to filter your command history.

pbcopy and pbpaste to interact with the system clipboard.
posted by Maecenas at 11:19 AM on December 7, 2015 [1 favorite]

PBCopy/PBPaste for OSX-specific utility.
(Also, replacing the default terminal with iTerm2
posted by CrystalDave at 11:21 AM on December 7, 2015 [3 favorites]

ctrl+r to search history.

ctrl+a, ctrl+e, ctrl+u, ctrl+k, ctrl+l
posted by Mr. Papagiorgio at 11:25 AM on December 7, 2015 [5 favorites]

My personal favs include:
- ctrl-a for go to beginning of line and ctrl-e for go to end of line (also useful almost everywhere on a mac).
- save window group for when you've positioned your terms just the way you want them.
- use option as meta key (under preferences | profile | keyboard)
- limit scrollback to something large but not memory-eating large if you're viewing logs a lot.
posted by Phredward at 11:25 AM on December 7, 2015

You can drag a file from the Finder into a terminal window, and it inserts the full path to that file.

You can also insert a path by dragging the little icon that appears to the left of the window title in any open document window.
posted by alms at 11:30 AM on December 7, 2015 [1 favorite]

The Source Code Pro font works great in Terminal. You can download it for free and then set it to be your default font in Terminal preferences.
posted by alms at 11:32 AM on December 7, 2015

You can drag a file from the Finder into a terminal window, and it inserts the full path to that file.

Conversely, you can do `open ` to open that directory in Finder.


– Setting scrollback in preferences to "limit to available memory" instead of n rows.
– Ctrl+W to delete the previous word
– Maybe it might be worth introducing `>` to write stdout to files?
– Similarly, `|` for sending stdout from one program to another? e.g `date | pbcopy`

posted by ignignokt at 11:38 AM on December 7, 2015 [1 favorite]

I've used the command line to do some batch-renaming things that are basically impossible in the Finder without scripting so it might be good to have a few multi-step things like "How to remane every file in a folder" or "How to batch-replace the suffix on a folder of images from JPEG to JPG" I have a few old HTML files that I just edit by hand. Ssh in, navigate to the directory, edit with nano or pico.
posted by jessamyn at 11:51 AM on December 7, 2015 [1 favorite]

"|", ">" as mentioned, and some simple examples on how to use them. Piping into grep (no need to go into regexes).

The existence of commands "ll", "la" as aliases for "ls -a"/"ls -l". Also the -h flag to get human-readable file sizes.

Basic use of the "find" command.

How to write shell scripts (maybe just stick to a sequence of commands without any variables or control flow).
posted by vogon_poet at 11:56 AM on December 7, 2015 [1 favorite]

I don't know if this counts as "beginner," but almost daily I use rsync in order to back up/move file structures. Specifically, something like:

rsync -uavz *.foo user@remote.machine.com:~/bar/

along with what leaving the trailing slash does, etc.
posted by Betelgeuse at 12:18 PM on December 7, 2015 [1 favorite]

posted by ryanrs at 12:19 PM on December 7, 2015 [3 favorites]

Oh! And also the various useful flags on 'ls' is a great instruction. Whenever I type 'ls', my muscle memory basically always has me type 'ls -ltr' instead.
posted by Betelgeuse at 12:20 PM on December 7, 2015

say -v Zarvox "To be, or not to be, that is the question:
Whether 'tis Nobler in the mind to suffer
The Slings and Arrows of outrageous Fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep
No more; and by a sleep, to say we end
The Heart-ache, and the thousand Natural shocks
That Flesh is heir to? 'Tis a consummation
Devoutly to be wished. To die, to sleep,
To sleep, perchance to Dream; aye, there's the rub,
For in that sleep of death, what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause. There's the respect
That makes Calamity of so long life:
For who would bear the Whips and Scorns of time,
The Oppressor's wrong, the proud man's Contumely,
The pangs of despised Love, the Law’s delay,
The insolence of Office, and the Spurns
That patient merit of the unworthy takes,
When he himself might his Quietus make
With a bare Bodkin? Who would Fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscovered Country, from whose bourn
No Traveller returns, Puzzles the will,
And makes us rather bear those ills we have,
Than fly to others that we know not of.
Thus Conscience does make Cowards of us all,
And thus the Native hue of Resolution
Is sicklied o'er, with the pale cast of Thought,
And enterprises of great pitch and moment,
With this regard their Currents turn awry,
And lose the name of Action. Soft you now,
The fair Ophelia? Nymph, in thy Orisons
Be all my sins remembered."

posted by DigDoug at 12:25 PM on December 7, 2015 [2 favorites]

One I use all the time is "open ." to open a Finder window for the current folder. A fun but useless command is "say" for text-to-speech.

Also, enable the option to right-click on a folder in Finder and open a terminal window there: instructions.
posted by mshrike at 12:26 PM on December 7, 2015 [2 favorites]

Aliases. Also, consider handing out a printed quick reference with the basic basic stuff (ls, cd, pwd, cp, rm, mkdir, rmdir, sudo, etc.) for your students to take with them, since an hour is not very long to go from zero to having all that stuff memorized.
posted by contraption at 12:38 PM on December 7, 2015 [1 favorite]

'q' gets you out of a man page.
My windows brain took far too long to get to that point.. the first 500 times I used 'man'
posted by DigDoug at 12:42 PM on December 7, 2015

Break is Control+c, not Command+c.
posted by mshrike at 12:56 PM on December 7, 2015

This may be a little advanced, but learning and installing Homebrew isn't a bad idea.
posted by Ampersand692 at 1:26 PM on December 7, 2015 [2 favorites]

in addition to the above, things i use most are: man, cp, mv, cat, a text editor (pico is pretty friendly), top (and navigating top), kill, chmod, more, less (basic pipes).

making an executable command file.
making an application that executes a terminal command in applescript

The key is you want people to be learning something useful that they're remember because they use it. "Here's how you make a double click backup program using applescript and rsync" is useful. So is "Here's how to kill a runaway task in the terminal." Try to find solutionat to actual common problems rather than exercises to practice. To contrast "Here's how chmod works" is not useful, but "here's how you use chmod to fix your webpage" is.

definitely not beginner, but I do a lot of piping ls into sed for batch operations. i suppose if you find people are into that sort of thing, an intro to grep might be ok.

and maybe sudo rm -rf /
posted by yeolcoatl at 1:57 PM on December 7, 2015

cd - takes you to the previous folder you were in.

pbpaste is cool and all, but note it uses old style Mac CR ends-of-line, while all Unix tools expect LF. I don't use pbpaste except with pbpaste | tr '\015' '\012' to get 'real' text out of it.

Inconsolata is an even nicer terminal font than Source Code Pro.

Make sure no-one has the wildly annoying tcsh that used to be the default shell. Bash (for all of its faults) should be the default everywhere, unless you want to be twee and incompatible.

Oh, and yeolcoatl - from Yosemite on, even sudo won't remove system directories or files, so that old troll don't run no more.
posted by scruss at 2:06 PM on December 7, 2015 [1 favorite]

and maybe sudo rm -rf /

Ask MetaFilter is as helpful as you make it. Wisecracks don't help people find answers.

That said, I do like command line jokes.
posted by jessamyn at 2:12 PM on December 7, 2015 [1 favorite]

Jessamyn: That was not intended to be a wisecrack. Learning how you can (accidentally) damage your computer is just as important as learning how you can use it. "Here is a list of things you shouldn't do on the command line because they will cause problems." is a valid suggestion.
posted by yeolcoatl at 2:46 PM on December 7, 2015 [1 favorite]

It's good to start off with (seemingly) simple one word commands like 'date' and 'cal' or even 'say goat'.

Show them pico. Users can actually get the hang of that one in a few minutes, not like vi and emacs, and it's great for editing shell config files and git commit files. It shows them you can do actual work in a shell app.

I tend to define
alias bp='pico ~/.bash_profile'
which gets you right into editing your profile and setting up aliases, etc.

I find 'ls -Al' to be the most useful version way to ls, so I define it to 'l' with
alias l='ls -Al'

cat ~/.bash_history | grep whatever
is fantastic if you can't remember the clever thing you did with the whatever command last week that required 6 arguments and is a nice demo of how shell commands work together.

cd ..
Are ways to use cd that a new user wouldn't guess.
posted by w0mbat at 6:33 PM on December 7, 2015

When I use the terminal, it's most often for ps and kill or to use vi as a text editor. Which you should not teach them, because every text editor and word processor they use after learning that will infuriate them.

So definitely don't show them vimtutor to browse on their own later.
posted by ctmf at 7:23 PM on December 7, 2015

sed is super-handy, but you'd end up talking about regular expressions much sooner than you wanted to, I bet.
posted by ctmf at 7:25 PM on December 7, 2015

also, don't forget "cd -" which is mad useful.
posted by vogon_poet at 8:20 PM on December 7, 2015 [1 favorite]

hostname and whoami are super helpful if you're connecting to lots of machine.
posted by furtive at 9:52 PM on December 7, 2015

I end up using
top -oCPU
way more often than I should
posted by DigDoug at 5:17 AM on December 8, 2015 [1 favorite]

crontab -e
crontab -l
Cron+vi/nano+rsync to make timed backups
Tab tab to see all the completion options
nano -c filename
posted by hz37 at 8:46 AM on December 8, 2015

Super handy way to batch resize images in OS X terminal:

sips -Z 640 *.jpg
posted by furtive at 9:50 AM on December 8, 2015 [1 favorite]

a general stash of command line tips: commandlinefu. Look for the best rated ones, and you'll be pleasantly surprised.
posted by scruss at 5:24 AM on December 9, 2015

> That was not intended to be a wisecrack. Learning how you can (accidentally) damage your computer is just as important as learning how you can use it. "Here is a list of things you shouldn't do on the command line because they will cause problems." is a valid suggestion.

That was a wisecrack at best, if not a real attempt to get people to damage their systems as punishment for being ignorant of the commands and too trusting of strangers on the internet. "Here is a list of things you shouldn't do" would indeed be a fine contribution, but dropping a potentially catastrophic command without explanation into a thread full of helpful commands presented the same way is a bad idea for very obvious reasons. To any novices reading this thread, please be aware that typing "sudo rm -rf /" will wreck your OS.

sudo (do the following using full root admin privileges, rolling right over any permissions set to protect you from yourself) rm -rf (delete the following, including any subdirectories, ignoring any warnings) / (the root of the current filesystem, which on OS X is going to mean the whole system drive. It's tricky because the '/' doesn't look like much when in fact it's everything.
posted by contraption at 12:06 AM on December 14, 2015

explainshell.com is a handy tool for figuring out what all the arcane-looking commands you see posted online are actually doing, and can help you both to understand why they work, and to assay whether it would be a good idea to actually run them. It gives a notated view of any command you enter, as well as linking the man page for every program the command invokes.
posted by contraption at 12:10 AM on December 14, 2015 [2 favorites]

« Older Recommend wi-fi digital picture frame.   |   What makes a podcast successful and engaging to... Newer »
This thread is closed to new comments.