Command Line

Ten Command Line Habits That Save Time Every Day

By ScriptChest Editorial Team · · 4 min read

The command line rewards small habits. None of the ideas below is advanced, and each takes only a few minutes to learn. Used every day, they remove a surprising amount of typing, waiting and second-guessing. The examples assume a Bash or Zsh shell on Linux or macOS, though most also apply elsewhere.

1. Lean on tab completion

Press Tab to finish file names, folder names and command names. Press it twice to see all the options when there is more than one match. Beyond saving keystrokes, completion prevents typing mistakes, because if the name completes, it exists. Many tools, such as Git, also ship completion scripts that suggest branch names and subcommands.

2. Search your history instead of retyping

Press Ctrl+R and start typing a fragment of an earlier command. The shell finds the most recent match, and pressing Ctrl+R again moves to older ones. The history command lists everything, and you can pipe it into grep to hunt for a long command you ran last month.

history | grep rsync

Two shortcuts pair well with this: !! repeats the previous command, and sudo !! repeats it with administrator rights when you forgot to add them.

3. Learn the line editing shortcuts

You do not have to hold the arrow key to move across a long command. These work in most shells:

  • Ctrl+A jumps to the start of the line.
  • Ctrl+E jumps to the end.
  • Ctrl+W deletes the previous word.
  • Ctrl+U deletes everything before the cursor.
  • Ctrl+L clears the screen.

Practise two or three first. They become automatic within a week.

4. Create aliases for commands you repeat

An alias is a short name for a longer command. Add lines to your shell startup file, such as ~/.bashrc, and reload it.

alias ll='ls -lah'
alias gs='git status'
alias ..='cd ..'

Keep aliases few and memorable. A hundred of them become another thing to forget. If a command needs arguments in the middle, write a small shell function instead.

5. Jump back to where you were

Typing cd - returns you to the previous folder, which is handy when you hop between two locations. For longer trails, pushd saves the current folder on a stack and popd returns to it. Plain cd with no argument goes home. These three cover most navigation without long paths.

6. Combine commands with pipes

The pipe sends the output of one command into the next, so small tools can be chained into something useful. Counting how many text files a folder holds, for example, takes one line.

ls *.txt | wc -l

Learn a short list of building blocks: grep to filter lines, sort and uniq to order and count, head and tail to trim, and wc to count. With these, you can answer many questions without opening a spreadsheet.

7. Use brace expansion and globbing

The shell expands patterns before a command even runs. Braces generate lists, and wildcards match names.

mkdir project/{src,docs,tests}
cp report.txt report.txt.bak
cp report.txt{,.bak}

The last two lines do the same thing. The brace form saves retyping long names, and it is a good place to learn how expansion works.

8. Make deletes less dangerous

The rm command does not use a recycle bin. A few habits lower the risk:

  1. Run ls with the same pattern first, to see exactly what will match.
  2. Use rm -i to be asked about each file when you are unsure.
  3. Never paste a destructive command you do not understand, especially one containing sudo or -rf.

Taking five seconds to look before deleting is cheaper than restoring from a backup.

9. Run long jobs without blocking your terminal

Add an ampersand to start a command in the background, and use Ctrl+Z followed by bg to push a running job aside. For work that must outlast your session, nohup or a terminal multiplexer keeps it alive. Combine this with command > output.log 2>&1 to save everything the job prints, so you can read it later.

10. Read the manual and the built-in help

Most commands accept --help, which prints a short summary. The man command shows the full manual, and pressing / inside it searches for a word. When you cannot remember an option, ten seconds with the manual is faster than a web search, and it matches the version installed on your machine.

Build the habits gradually

Do not try to adopt all ten at once. Pick one this week, use it until you stop thinking about it, then add another. Keep a small text file of shortcuts you want to remember and review it now and then. Over a few months, the terminal starts to feel less like a place where you type and more like a place where you think.

Frequently Asked Questions

Where should I put my aliases?

Put them in your shell startup file, usually ~/.bashrc for Bash or ~/.zshrc for Zsh. After editing, run source ~/.bashrc (or the Zsh equivalent) to load the changes into the current session.

Is it worth learning these on Windows?

Yes, with some differences. PowerShell has its own tab completion, history search and aliases, and Windows Subsystem for Linux provides a full Bash environment if you want the exact commands shown here.

How do I remember all these shortcuts?

You do not need to. Use one new shortcut each day for a week, and leave the rest until later. Repetition in real work builds memory far better than reading a list.

Related reading