UNIX and the Shell
Whether you’re using a Linux laptop or workstation (or working on the command line in macOS!) or working on a cluster, you’ll need to be familiar with shell commands and scripting, which can save you a lot of time and typing.
You do not need to be an expert, but once your work moves to Hopper there is no graphical interface to fall back on, so you will need the basics.
Commands to Know
You need enough to move around a file system, look at files without opening them in an editor, and chain a few commands together:
| Task | Commands |
|---|---|
| Move around | cd, pwd, ls |
| Inspect files | cat, head, tail, less, wc |
| Move and copy | cp, mv, mkdir, rm |
| Find things | find, grep |
| Chain things | pipes (\|), redirection (>, >>) |
| Remote work | ssh, scp, rsync |
| Long-running jobs | tmux or screen |
| Disk usage | du -sh, df -h |
Two are worth learning early:
rsync, instead ofscp, for large or repeated transfers. It resumes interrupted transfers and skips files that have not changed, andrsync -avn(the-nis a dry run) shows what would be transferred before anything is copied.tmux, on any remote machine. Without it, a dropped SSH connection ends whatever you were running; with it, you reconnect and your session is still there.
No. The shell has no undo and no trash can, so rm -rf on the wrong path is permanent, and on a cluster without backups (which includes Hopper) any deleted file is gone for good. Check where you are with pwd before deleting anything, and use rm -i, which asks before each deletion, until you are comfortable.
Shell Scripts
A shell script is a file of commands you would otherwise type. Put any analysis step you have run more than twice into one, both to save typing and because the script records what you did. That record is part of reproducibility: “I ran some commands” does not describe a method, and a .sh file in the repository does.
Start scripts with #!/usr/bin/env bash and set -euo pipefail, which makes the script stop at the first error. Without that line, the script keeps going after a failed step, and it can still exit with success if its last command succeeds.
Your Slurm submission scripts on Hopper are shell scripts with extra #SBATCH directives at the top, so this is the same skill.
Learn More
- The Unix Shell from Software Carpentry, the standard introduction. Work through it once; it takes an afternoon.
- The Missing Semester of Your CS Education from MIT, covering the shell,
tmux, and the wider command-line toolchain. - ShellCheck finds bugs in a script you have written, including several kinds of quoting error that do not produce an error message.
- Explainshell explains what an unfamiliar command and each of its flags do, which helps when you inherit a script or copy one from a tutorial.
When You’re Stuck
The shell’s error messages are short, and its manual pages assume you already know what you are looking for. Try these in order:
man <command>or<command> --help. Manual pages are references written for people who know the command, so search for the flag you need instead of reading from the top.- Explainshell, when you have a command and do not know what it does. Paste it in and it explains each flag.
- ShellCheck, when a script misbehaves, since it catches quoting bugs that produce no error message.
- The Unix Shell, if what you’re missing is a concept and not a specific command.
- Someone in the group. Shell problems are often quick to solve in person, because the person helping can usually see the problem in your command.