DEV Community

Cover image for 10 Terminal Commands I Wish I Knew 5 Years Ago
Info Inlet
Info Inlet

Posted on

10 Terminal Commands I Wish I Knew 5 Years Ago

For about five years I used the terminal like a very expensive notepad.

I typed every command out by hand. I pressed the up arrow 30 times to find something I ran yesterday. I Googled "how to kill process on port 3000" so many times that Google probably autocompletes it for me by now.

None of the commands below are secret. Most of them have existed for decades. But nobody sat me down and showed me, so I learned them one embarrassing moment at a time.

The rule for this list: each one has to kill something I used to retype, re-scroll or re-Google every week. If it's just "cool", it didn't make the cut.

Works in bash and zsh on macOS and Linux unless I say otherwise.

1. Ctrl + R: search your command history

Stop hammering the up arrow.

Press Ctrl + R and start typing any part of a command you ran before:

(reverse-i-search)`dock': docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

Press Ctrl + R again to cycle through older matches. Enter runs it, → (right arrow) puts it on the line so you can edit first.

This one alone probably saves me 10 minutes a day. That long docker or ssh command you ran last Tuesday? It's three keystrokes away.

2. cd -: jump back to where you were

cd ~/projects/api/src/routes
cd /var/log
cd -        # back to ~/projects/api/src/routes
cd -        # back to /var/log
Enter fullscreen mode Exit fullscreen mode

It's the "previous channel" button for your terminal. Perfect when you're bouncing between a project folder and a config or log folder.

3. sudo !! and !$: reuse the last command

You've done this:

apt install htop
# Permission denied
Enter fullscreen mode Exit fullscreen mode

Instead of retyping the whole thing:

sudo !!
Enter fullscreen mode Exit fullscreen mode

!! means "the entire last command". And its little sibling !$ means "the last argument of the last command":

mkdir -p src/components/forms
cd !$       # cd src/components/forms
Enter fullscreen mode Exit fullscreen mode

Once these are in your fingers, you'll use them without thinking.

4. fc: edit the last command in your editor

You just typed a 200-character curl command with headers and a JSON body, and it has a typo somewhere in the middle.

fc
Enter fullscreen mode Exit fullscreen mode

This opens the last command in your $EDITOR (vim, nano, VS Code, whatever you set). Fix it, save, close, and it runs. No more holding the left arrow key for 15 seconds.

Tip: export EDITOR="code --wait" if you want it to open in VS Code.

5. lsof -i :3000: who is using my port?

The classic:

Error: listen EADDRINUSE: address already in use :::3000
Enter fullscreen mode Exit fullscreen mode

Find out what's holding it:

lsof -i :3000
Enter fullscreen mode Exit fullscreen mode

And kill it in one line:

lsof -ti :3000 | xargs kill
Enter fullscreen mode Exit fullscreen mode

-t prints only the process ID, which is exactly what kill wants. (Add -9 to kill only if it refuses to die.) I have this aliased as killport now.

6. du -sh * | sort -h: what is eating my disk?

Your laptop says the disk is full. Where did it go?

du -sh * | sort -h
Enter fullscreen mode Exit fullscreen mode
4.0K    README.md
 12M    src
1.8G    node_modules
6.3G    .docker-cache
Enter fullscreen mode Exit fullscreen mode

-s gives one total per item, -h makes it human-readable, and sort -h sorts those human sizes correctly (so 1.8G comes after 12M). Run it in your home folder once. You'll find something you forgot about.

7. rg (ripgrep): search code ridiculously fast

rg "TODO"
rg "useEffect" --type ts
rg -i "api_key" -g '!node_modules'
Enter fullscreen mode Exit fullscreen mode

It's like grep -r, but much faster, and it skips .gitignored files (like node_modules) by default. Output is grouped by file, with line numbers and highlighting.

Install: brew install ripgrep or apt install ripgrep.

8. fzf: fuzzy-find anything

fzf takes any list and turns it into an interactive, searchable menu.

vim $(fzf)                          # pick a file, open it
git checkout $(git branch | fzf)    # pick a branch, switch to it
Enter fullscreen mode Exit fullscreen mode

The real win: after you install its shell integration, it upgrades your Ctrl + R (from #1) into a fuzzy, full-screen history search, and Ctrl + T drops a fuzzy-picked file path right onto your command line.

Install: brew install fzf, then follow the one-line shell setup it prints.

9. jq: read JSON like a human

API responses come back as one giant unreadable line. Pipe them into jq:

curl -s https://api.github.com/repos/cli/cli | jq '.stargazers_count'
Enter fullscreen mode Exit fullscreen mode
cat users.json | jq '.[] | {name, email}'
Enter fullscreen mode Exit fullscreen mode

Just jq '.' on its own pretty-prints and colors any JSON. I use that version ten times a day.

10. xargs: turn output into arguments

xargs takes the output of one command and feeds it as arguments to another. This is where the terminal starts to feel like a superpower.

Delete every local git branch that's already merged into main:

git branch --merged main | grep -vE '^\*|main$' | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

Find and delete every .DS_Store in a project:

find . -name ".DS_Store" -print0 | xargs -0 rm
Enter fullscreen mode Exit fullscreen mode

(-print0 and -0 handle file names with spaces safely.)

Read the first example slowly once. List branches → filter out the current one and main → delete the rest. Three small tools, one job. That's the whole Unix philosophy in one line.

Bonus round

  • tldr tar: like man, but just the 5 examples you actually need. (brew install tlrc or npm i -g tldr)
  • Ctrl + A / Ctrl + E: jump to the start / end of the line. Ctrl + U clears everything before the cursor, Ctrl + W deletes one word.
  • pbcopy / pbpaste (macOS): cat id_ed25519.pub | pbcopy puts your SSH key on the clipboard. On Linux, xclip or wl-copy do the same.

The real lesson

None of these made me a "10x developer". What they did was remove friction. Every retyped command, every Google search for the same port error, every 30 presses of the up arrow is a tiny context switch that pulls you out of the actual problem.

Automate the boring repetition, so your attention goes to the parts that need a human. That's the idea behind most good tools, and it's what we're trying to do with xenition too. But you can start with Ctrl + R tonight.

Top comments (4)

Collapse
 
favoritedevx profile image
Davi Schulze •

Thanks for the tutuorial man ;)

Collapse
 
infoinlet1 profile image
Info Inlet •

Thanks Davi, glad it helped! šŸ™Œ

If you only try one tonight, make it Ctrl + R. Once that's in your fingers, the up arrow starts to feel slow.

Which one was new to you?

Collapse
 
ba796 profile image
brent alexis •

These should be printed and "duck taped" to the side of a monitor!

Collapse
 
infoinlet1 profile image
Info Inlet •

Ha, that's honestly the best way to learn them, Brent! šŸ˜„

A sticky note with just Ctrl + R, cd - and sudo !! covers about 80% of the time it saves me. Once those stick, the tape can come off.

Which ones would make your sticky note?