Ten Commands to Get Comfortable With the Terminal
The command line has a reputation. Black window, blinking cursor, one wrong keystroke and you've deleted your computer. I understand why it feels that way, but most of what you'd do in a terminal day to day is looking around and moving files, the same stuff you do in Finder or File Explorer, just by typing.
These work on macOS (open the Terminal app), on Linux, and on Windows if you use WSL or Git Bash. Windows PowerShell understands several of them as aliases too, though not all of them behave identically.
Two things before we start. First, press Tab a lot. Start typing a file or folder name and hit Tab, and the shell finishes it for you. It saves typing and catches typos. Second, if something is running and you want it to stop, press Ctrl+C. That's your escape hatch.
1. pwd: where am I?
pwd
Prints the folder you're currently in ("print working directory"). In a terminal you're always somewhere, and when you're confused, this is the first thing to run.
2. ls: what's here?
ls
ls -l
ls -la
Lists what's in the current folder. -l gives the long version with sizes, dates and permissions. -a includes hidden files, the ones whose names start with a dot. You can also point it somewhere else: ls ~/Downloads.
3. cd: go somewhere
cd Documents
cd ..
cd ~
cd -
Changes folder. .. means "the folder above this one." ~ is your home folder. cd - jumps back to wherever you just were, which is more useful than it sounds. Running cd with nothing after it also takes you home.
4. mkdir: make a folder
mkdir notes
mkdir -p projects/site/images
The -p option creates any missing folders along the way, so you can build a whole path in one go.
5. cp: copy
cp report.txt report-backup.txt
cp -r photos photos-backup
Copies a file. Use -r (recursive) to copy a whole folder and everything in it. One warning: if the destination file already exists, cp will overwrite it without asking. Add -i and it'll check with you first.
6. mv: move or rename
mv draft.txt final.txt
mv final.txt ~/Documents/
There's no separate rename command. Renaming is just moving a file to a new name in the same place. Like cp, it overwrites silently unless you add -i.
7. rm: delete (carefully)
rm old-file.txt
rm -i old-file.txt
rm -r old-folder
This is the one that earns the terminal its scary reputation, so let's be clear about it. rm does not use the Trash. Deleted means gone. -r deletes a folder and everything inside it.
Good habits: run ls on the same path first to see exactly what you're about to remove. Use -i while you're learning so it asks before each file. Be very wary of wildcards like * until you're comfortable, and don't paste rm commands from the internet that you don't fully understand. If you want an empty folder gone, rmdir only works on empty folders, which makes it a safe choice.
8. cat and less: read a file
cat notes.txt
less big-log-file.log
cat dumps a file's whole contents to the screen. Great for short files. For anything long, use less, which lets you scroll with the arrow keys or space bar, search by typing / followed by a word, and quit with q. You'll use q to exit a lot of terminal programs, so it's worth remembering.
9. grep: find text
grep "error" app.log
grep -i "todo" notes.txt
grep -rn "api_url" ~/projects/site
Searches for text inside files and prints the matching lines. -i ignores case. -r searches through a whole folder. -n shows line numbers. Once you get used to grep you'll wonder how you ever found anything.
It also works on the output of other commands, using the pipe character |, which sends one command's output into another:
ls -l | grep ".pdf"
That pipe is the real superpower of the command line. Small tools, chained together.
10. man: read the manual
man ls
Every command above has more options than I've shown. man opens the built-in manual for a command (scroll and quit just like less). Many commands also respond to --help with a shorter summary. When you want to know what a flag does, this beats searching the web, because it's the documentation for the version actually installed on your machine.
Where to go from here
Honestly, just use it. Next time you need to find a file or rename a batch of things, try it in the terminal first. Use the up arrow to scroll back through previous commands instead of retyping them. history shows everything you've run, and clear (or Ctrl+L) wipes the screen when it gets cluttered.
The one rule I'd hold onto: read a command before you hit Enter, especially anything with rm, sudo, or something you copied from a website. Everything else, you can poke at freely. Listing, reading and moving around can't hurt anything.

Comments