keepcalculator

06 Numbers and Powers

Terminal Tricks You’ll Wish You Knew Earlier

Card No. 06·51392 · Updated · Standard formula, shown below

This article covers practical command-line tricks that save time and prevent frustration, from history shortcuts and process management to remote server navigation. You’ll learn concrete commands you can use immediately, along with the context for when each one helps most.

Why the Terminal Still Matters

Graphical interfaces are great, but the terminal remains the fastest way to do many tasks—bulk renaming files, searching through logs, connecting to servers, or automating repetitive work. The learning curve is real, but a handful of lesser-known tricks can make the difference between dreading the command line and genuinely enjoying it. This guide focuses on bash and zsh, the two most common shells on Linux and macOS, and notes where Windows PowerShell differs.

History Shortcuts That Save Minutes Daily

Most people press the up arrow to recall a previous command, but that only gets you the last one. These history tricks give you far more control:

  • Ctrl+R – Reverse search. Start typing any part of a previous command and it will find the most recent match. Press Ctrl+R again to cycle further back. This is the single most useful history shortcut.
  • !! – Repeats the last command entirely. Use it with sudo: sudo !! reruns the previous command with elevated privileges.
  • !$ – Expands to the last argument of the previous command. Example: mkdir newproject then cd !$ takes you straight into it.
  • !* – Expands to all arguments of the previous command. Useful when you need to run a different command on the same files.
  • history | grep pattern – Shows all commands matching a pattern, with numbers. Then run !number to re-execute that exact command.

These work in bash and zsh. PowerShell uses Get-History and Invoke-History instead, but the Ctrl+R search works there too.

Process Management: Finding and Killing

When an application freezes or a script runs away, you need to find and stop it quickly. The classic approach is ps aux | grep firefox to find the process ID, then kill 1234. But there are faster ways:

  • pgrep firefox – Returns just the PID(s) without the full ps output.
  • pkill firefox – Kills the process by name directly. Be careful: pkill -f matches the full command line, so pkill -f 'python script.py' is more precise.
  • kill -9 – Force kills a process that ignores the normal termination signal. Only use this when a regular kill fails, because it doesn’t let the program clean up.
  • Ctrl+Z – Suspends the current foreground process. Then bg sends it to the background, or fg brings it back. This is handy when you started a long task and need the prompt back.
  • jobs – Lists all background jobs for the current shell session.

On Windows, the equivalents are tasklist and taskkill in PowerShell or Command Prompt.

Navigating and Managing Files Efficiently

Moving around the filesystem is the most common terminal activity, yet many people don’t know these time-savers:

  • cd – – Goes back to the previous directory. Repeating it toggles between two directories, which is great when you’re working across two locations.
  • pushd and popd – pushd /path changes directory and saves the current one on a stack. popd returns to the last saved directory. This is better than cd - when you need to go back multiple levels.
  • Tab completion – Type the first few letters of a file or directory and press Tab. Press Tab twice to see all matches. This works for commands too.
  • Alt+. – Inserts the last argument of the previous command. Press it repeatedly to cycle through earlier commands’ last arguments.
  • mkdir -p a/b/c – Creates nested directories in one go. Without -p, mkdir fails if any parent doesn’t exist.
  • rm -rf – Deletes a directory and everything inside it. Warning: there is no undo. Double-check the path before pressing Enter.

Searching Files and Output

Finding a string inside a file is a daily task, but the basic grep command only scratches the surface:

  • grep -r pattern . – Recursively searches all files in the current directory. Add -i for case-insensitive, -n to show line numbers, and -l to list only filenames.
  • grep -v pattern – Inverts the match, showing lines that do NOT contain the pattern. Useful for filtering out comments or blank lines.
  • | less – Pipes long output into a pager so you can scroll up and down. Press q to quit. This works with any command, not just grep.
  • tail -f filename – Follows a file as it grows, showing new lines in real time. Essential for watching log files. Press Ctrl+C to stop.
  • find . -name ‘*.txt’ – Finds files by name. The -name option is case-sensitive; use -iname to ignore case.

Remote Servers: SSH Tricks

If you connect to remote servers, these SSH features will change your workflow:

  • ssh user@host ‘command’ – Runs a single command on the remote machine and returns the output. No interactive session needed.
  • SSH keys – Set up passwordless login with ssh-keygen and ssh-copy-id user@host. This is both more secure and faster than typing a password each time.
  • scp file user@host:/path – Copies a file to a remote server. Use scp -r for directories.
  • rsync -avz source/ user@host:/dest/ – Synchronizes directories, only transferring changed files. The -a preserves permissions, -v is verbose, -z compresses.
  • ssh -L 8080:localhost:80 user@host – Creates a tunnel that forwards your local port 8080 to port 80 on the remote server. Useful for accessing web interfaces on servers without exposing them publicly.

Customizing Your Prompt and Aliases

Your shell can be tailored to show exactly what you need and to shorten repetitive commands:

  • alias ll=’ls -alF’ – Creates a shortcut. Put aliases in your ~/.bashrc or ~/.zshrc file so they load every time.
  • export PS1=’\u@\h:\w$ ‘ – Changes the prompt to show username, hostname, and current directory. Many prebuilt themes exist, but this simple one is a good start.
  • export HISTSIZE=10000 – Increases the number of commands saved in history. The default is often 1000, which fills up quickly.
  • shopt -s histappend – In bash, appends history to the file instead of overwriting it. This prevents losing history when you have multiple terminals open.

After editing your config file, run source ~/.bashrc to apply changes immediately.

Frequently Asked Questions

What is the difference between bash and zsh?

Bash is the default on most Linux distributions and older macOS versions. Zsh is the default on macOS since Catalina (2019) and offers better autocompletion, spelling correction, and theme support. Most bash commands work in zsh, so learning one transfers to the other. You can check your shell with echo $SHELL.

Is it safe to use rm -rf?

It is safe if you are certain of the path you are deleting. The danger is that rm -rf does not ask for confirmation and permanently deletes files. Always type the full path, avoid wildcards like * unless you are sure, and consider using rm -rf -- to prevent accidentally treating a filename starting with a dash as an option.

How do I undo a command I just ran?

There is no general undo. If you deleted a file, you may recover it from a backup or a trash folder if you used a graphical tool. The best prevention is to use mv to a trash directory instead of rm, or to use version control for code. For destructive commands, double-check before pressing Enter.

Why does my terminal show a different Python version than I expect?

This usually happens because multiple Python installations exist on your system. The command which python shows which one is first in your PATH. You can use alias python='python3' to force a specific version, or use a version manager like pyenv to control which Python is active.

What is the best way to learn terminal commands?

Start with the commands you use daily—navigating, copying, moving, and viewing files. Then practice one new command each day. The manual pages (man command) are comprehensive but dense. Online tutorials and cheat sheets are helpful, but nothing beats typing commands yourself. Keep a personal cheat sheet of commands you discover.

How do I make a command run in the background?

Add an ampersand at the end: long_running_command &. The shell returns the prompt immediately. You can check on it with jobs, bring it to the foreground with fg, or send it to the background with bg. For tasks that should survive closing the terminal, use nohup command & or a tool like tmux.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →