Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Command Line Habits You Need to Break

· 7 min read · Home Tech

In this article
  1. Why Habits Matter More Than Syntax
  2. The Overuse of sudo
  3. Ignoring Tab Completion and History
  4. Relying on cd and ls When Faster Tools Exist
  5. Typing Long One-Off Commands Instead of Scripting
  6. Not Using Aliases for Frequent Commands
  7. Common Problems and How to Fix Them
  8. Safety First: Protecting Your Data
  9. Frequently Asked Questions

You need to break command line habits that waste time, risk data loss, or lock you into outdated workflows. These include overusing sudo, ignoring tab completion, relying on cd and ls when faster tools exist, and treating the shell as a place to type long, one-off commands instead of building reusable scripts.

Why Habits Matter More Than Syntax

The command line is a tool you use dozens or hundreds of times a day. Small inefficiencies compound. A habit that costs you two seconds per command, repeated fifty times a day, adds up to nearly an hour a month. More seriously, some habits—like using sudo for everything or deleting files with wildcards—can cause irreversible damage. Breaking these habits isn't about memorizing more commands; it's about changing how you think about the shell: as a programmable environment, not a typewriter.

The Overuse of sudo

The most dangerous habit is reaching for sudo before you understand what a command does. Running a command as root gives it full system access, so a typo like sudo rm -rf / var (with an accidental space) can delete critical system files. Even without typos, running regular commands as root can change file ownership, break permissions, and make your system unstable.

Break this habit by following a simple rule: only use sudo when you get a permission denied error, and only for the specific command that needs it. For most daily work—editing files in your home directory, running scripts, using package managers—you don't need root. If you find yourself using sudo frequently, investigate why. Often it's because you've created files with the wrong ownership or you're working in a directory that should be owned by your user.

Ignoring Tab Completion and History

Many users type full paths and long filenames manually, which is slow and error-prone. The shell's tab completion is one of its most powerful features. Pressing Tab completes file names, directories, commands, and even command options. Pressing Tab twice shows all possible completions. This not only saves time but also prevents typos.

Similarly, your shell keeps a history of commands you've run. Instead of retyping a long command, press Ctrl+R to search your history interactively. Type a few characters and the shell shows the most recent matching command. You can also use !! to repeat the last command, and !$ to reference the last argument of the previous command. These shortcuts are available in bash, zsh, and most other shells.

Relying on cd and ls When Faster Tools Exist

The classic habit of cd into a directory, ls to see its contents, then cd somewhere else is inefficient. Modern shells and tools offer faster navigation. For example, in zsh, you can set up directory aliases or use the autocd option to just type the directory name to change into it. Bash 4+ supports shopt -s autocd as well.

For listing files, ls is fine, but consider using tree to see a directory structure at a glance. More importantly, you often don't need to cd at all. Commands like find, grep, and awk can operate on files in other directories without changing your current location. For example, instead of cd /var/log && grep error *.log, you can run grep error /var/log/*.log directly. This keeps your working directory stable and reduces the chance of making a mistake in a different directory.

Typing Long One-Off Commands Instead of Scripting

If you find yourself typing the same long command repeatedly—maybe with slight variations—you should turn it into a shell function or a script. This is a habit that separates casual users from productive ones. A shell function can be defined in your .bashrc or .zshrc and then called by a short name. For example, if you often need to find the largest files in a directory, you could define:

function bigfiles() { du -ah $1 | sort -rh | head -10; }

Then just type bigfiles /home to see the ten largest files under /home. This saves time and reduces typos. For more complex tasks, write a proper script with a shebang line and make it executable. Scripts are also easier to debug and share.

Not Using Aliases for Frequent Commands

Aliases are simple shortcuts that can save you dozens of keystrokes a day. Common examples include alias ll='ls -alF', alias gs='git status', or alias ..='cd ..'. The key is to create aliases for commands you actually use. Don't overdo it—aliases that are too clever can confuse you when you switch to a different machine. But a few well-chosen aliases can make your workflow much smoother.

To see all your current aliases, type alias. To make an alias permanent, add it to your shell's startup file (.bashrc, .zshrc, etc.). Remember that aliases are expanded before the command runs, so they work best for simple command replacements. For more complex logic, use a function.

Common Problems and How to Fix Them

  • Accidental file deletion: Use rm -i (interactive) or better, use a trash command like trash (available on many systems) to avoid permanent deletion. Also, be careful with wildcards: rm *.txt is fine, but rm * .txt (with a space) will delete everything in the directory.
  • Running commands in the wrong directory: Always check your current directory with pwd before running destructive commands. Use absolute paths or carefully constructed relative paths.
  • Piping to less and forgetting to quit: Press q to exit. This isn't a bad habit per se, but it can be confusing for new users.
  • Using cat to view large files: Use less or head/tail instead. cat dumps the entire file to the terminal, which can flood your screen and slow down your terminal.

Safety First: Protecting Your Data

The command line gives you immense power, but with that power comes responsibility. Always double-check commands that delete, overwrite, or move files. Use ls to verify what a wildcard will match before you delete. For important operations, consider using cp -i (interactive copy) and mv -i (interactive move) to prompt before overwriting. These flags are available on most Unix-like systems.

Another safety habit is to use version control (like git) for your configuration files and scripts. If you break something, you can revert. Also, regularly back up your important files. The command line is not a place to be careless.

Frequently Asked Questions

Why is using sudo so dangerous?

Using sudo runs a command with root privileges, meaning it can modify or delete any file on the system. A single typo can render your system unbootable. Even without typos, running commands as root can change file ownership and permissions, causing applications to fail. The principle of least privilege suggests you should only use root when absolutely necessary.

How do I stop being afraid of the command line?

Start by learning a few safe commands like ls, pwd, and cd. Use tab completion to avoid typos. Practice in a virtual machine or a container where you can't break anything important. Remember that most commands have a --help flag and manual pages (man). The more you use it, the more comfortable you'll become.

What's the difference between an alias and a function?

An alias is a simple text substitution that works well for short commands. A function is a block of shell code that can accept arguments and contain logic. For example, alias ll='ls -alF' is an alias, while a function that takes a directory name and lists its largest files is a function. Use aliases for simple shortcuts, functions for anything more complex.

Is it better to use a GUI file manager or the command line?

It depends on the task. For simple file operations, a GUI might be faster. But for batch operations, complex searches, or tasks that need to be repeated, the command line is often more efficient. The best approach is to be comfortable with both and choose the right tool for the job.

How can I recover a file I accidentally deleted with rm?

If you're using a filesystem that supports snapshots (like btrfs or ZFS), you might be able to restore from a snapshot. Otherwise, recovery is difficult and often impossible. That's why it's crucial to use rm -i or a trash utility. Some systems have undelete tools, but they're not reliable. Prevention is the best strategy.

What are the best resources to learn advanced command line skills?

The man pages are your first resource. Online tutorials and books like "The Linux Command Line" by William Shotts are excellent. Practice by automating small tasks you do regularly. The more you use the shell, the more you'll learn. Also, read other people's scripts to see how they solve problems.