Mindfulness

How to Eliminate Bad Bash Habits

Updated · By the Relaxmid editors

Reading time
5 min
Sections
8
Topic
Mindfulness

Start by listing your current bash habits, then replace each one with a safer or more efficient alternative. This takes about 20–30 minutes of focused review and a few weeks of practice. You need a terminal, a text editor, and a willingness to slow down.

Identify Your Bad Habits

Bad bash habits are often shortcuts that feel faster but lead to errors, data loss, or wasted time. Common ones include using rm without checking, ignoring command history, and not using tab completion. Write down every command you use regularly, then mark the ones that feel risky or inefficient.

Symptom Cause Fix
Accidentally deleted a file rm without confirmation Use rm -i or trash command
Repeatedly typing long paths Not using tab completion Press Tab to auto-complete
Re-running same command with small changes Not using history expansion Use !! or Ctrl+R
Overwrote a file by mistake Using > instead of >> Check redirection before pressing Enter

Replace Risky Commands

Some bash commands are dangerous if used carelessly. Replace them with safer versions that ask for confirmation or allow undo.

  1. Use rm -i instead of rm – This prompts you before each deletion. Add an alias in your .bashrc file: alias rm='rm -i'.
  2. Use trash instead of rm for important files – The trash command moves files to a trash folder, so you can recover them. Install it if needed, then alias rm to trash for everyday use.
  3. Avoid mv and cp without checking – Use mv -i and cp -i to get a prompt before overwriting. Add aliases: alias mv='mv -i' and alias cp='cp -i'.
  4. Use mkdir -p for nested directories – This creates parent directories as needed, preventing errors when you try to create a path that doesn’t exist yet.

These aliases take effect immediately after you reload your configuration with source ~/.bashrc. They do not slow you down much, but they prevent costly mistakes.

Improve Your Command History

Your command history is a powerful tool if you use it correctly. Many people ignore it or clear it too often, losing the ability to recall past commands.

  1. Use Ctrl+R for reverse search – Press Ctrl+R and type a keyword to find a previous command. This is faster than scrolling through history.
  2. Use !! to repeat the last command – If you need to run the same command again, just type !! and press Enter. Add sudo before it with sudo !! when you need elevated permissions.
  3. Use !$ to reuse the last argument – For example, if you typed cat file.txt, then vim !$ opens file.txt in vim.
  4. Increase your history size – In your .bashrc, set HISTSIZE=10000 and HISTFILESIZE=20000 to keep more commands. This helps you find that exact command from weeks ago.
  5. Ignore duplicate commands – Add HISTCONTROL=ignoredups to avoid cluttering your history with repeats.

Using history properly saves time and reduces the chance of retyping a long command incorrectly.

Master Tab Completion

Tab completion is the single most time-saving habit you can build. It reduces typos and speeds up navigation.

  1. Press Tab once to complete a command or filename – If there are multiple matches, press Tab twice to see a list.
  2. Use Tab for variables and options – For example, type cd ~/Doc and press Tab to complete to cd ~/Documents.
  3. Enable case-insensitive completion – Add bind 'set completion-ignore-case on' to your .bashrc so you don’t have to match exact case.
  4. Use Ctrl+A and Ctrl+E to jump to start and end of line – This is faster than pressing arrow keys repeatedly.

Once you get used to Tab, you’ll wonder how you ever worked without it. It also prevents errors from mistyped paths.

Break the Copy-Paste Habit

Copying commands from the web or from memory without understanding them is a common bad habit. It leads to running commands that do unexpected things.

  1. Read the command before running it – Look at each part and understand what it does. If you don’t know, search for it or use man.
  2. Test commands in a safe environment – Use a virtual machine or a container to try new commands without risking your system.
  3. Use echo to preview expansions – For example, before running rm *.txt, type echo *.txt to see which files will be affected.
  4. Avoid piping to sudo without need – Many commands don’t need sudo. Running with sudo can cause permission issues later.

Understanding each command gives you control. It also helps you remember the command better for next time.

Maintain Your New Habits

New habits need reinforcement. Set up your environment to support them and review your progress weekly.

  1. Review your aliases and functions monthly – Check your .bashrc and remove anything you don’t use. Keep it clean.
  2. Keep a cheat sheet of your most-used commands – Write them down in a text file or use a note app. This reinforces learning.
  3. Practice deliberately for 10 minutes a day – Use a terminal for a small task, like renaming files or searching history, to build muscle memory.
  4. When you catch yourself using an old bad habit, stop and redo it correctly – For instance, if you just typed rm file without -i, press Ctrl+C and retype with the safe version.

If you find yourself reverting, it’s normal. Habits take time to change. Be patient and keep practicing.

Bash Habits for Speed and Safety

Some habits are both fast and safe. Adopt these to get the best of both worlds.

  • Use Ctrl+U to clear the current line – This is faster than backspacing.
  • Use Ctrl+L to clear the screen – Instead of typing clear.
  • Use Alt+. to insert the last argument from the previous command – This is a quick way to reuse a filename.
  • Use Ctrl+W to delete the word before the cursor – Speeds up editing.
  • Use Ctrl+Y to paste what you cut – Works with Ctrl+W and Ctrl+U.

These shortcuts are safe and save seconds each time. Over a day, they add up to minutes saved.

What if I accidentally run a dangerous command?

If you realize immediately, press Ctrl+C to stop it. If it’s too late, check if you have a backup or use recovery tools. For rm, if you used trash, you can restore from the trash folder. For other commands, you may need to restore from a backup. This is why backups are essential.

How do I undo a bad mv or cp?

There is no built-in undo. If you overwrote a file, you might recover it from a backup or from the file system’s journal if it’s recent. To prevent this, always use -i flags and double-check the destination path.

Is it okay to use sudo for everything?

No. Using sudo unnecessarily can cause permission problems and security risks. Only use sudo when the command needs root privileges, like installing system packages or editing system files. For everyday tasks, run as your normal user.

Related

General information for everyday wellbeing, not medical advice. If something affects your health, sleep or mood for more than a short while, talk to a qualified professional. Read more

Spotted an error? Tell us and we will correct it (corrections policy).