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.
- Use
rm -iinstead ofrm– This prompts you before each deletion. Add an alias in your.bashrcfile:alias rm='rm -i'. - Use
trashinstead ofrmfor important files – Thetrashcommand moves files to a trash folder, so you can recover them. Install it if needed, then aliasrmtotrashfor everyday use. - Avoid
mvandcpwithout checking – Usemv -iandcp -ito get a prompt before overwriting. Add aliases:alias mv='mv -i'andalias cp='cp -i'. - Use
mkdir -pfor 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.
- Use
Ctrl+Rfor reverse search – PressCtrl+Rand type a keyword to find a previous command. This is faster than scrolling through history. - Use
!!to repeat the last command – If you need to run the same command again, just type!!and press Enter. Addsudobefore it withsudo !!when you need elevated permissions. - Use
!$to reuse the last argument – For example, if you typedcat file.txt, thenvim !$opensfile.txtin vim. - Increase your history size – In your
.bashrc, setHISTSIZE=10000andHISTFILESIZE=20000to keep more commands. This helps you find that exact command from weeks ago. - Ignore duplicate commands – Add
HISTCONTROL=ignoredupsto 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.
- Press Tab once to complete a command or filename – If there are multiple matches, press Tab twice to see a list.
- Use Tab for variables and options – For example, type
cd ~/Docand press Tab to complete tocd ~/Documents. - Enable case-insensitive completion – Add
bind 'set completion-ignore-case on'to your.bashrcso you don’t have to match exact case. - Use
Ctrl+AandCtrl+Eto 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.
- 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. - Test commands in a safe environment – Use a virtual machine or a container to try new commands without risking your system.
- Use
echoto preview expansions – For example, before runningrm *.txt, typeecho *.txtto see which files will be affected. - Avoid piping to
sudowithout 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.
- Review your aliases and functions monthly – Check your
.bashrcand remove anything you don’t use. Keep it clean. - Keep a cheat sheet of your most-used commands – Write them down in a text file or use a note app. This reinforces learning.
- Practice deliberately for 10 minutes a day – Use a terminal for a small task, like renaming files or searching history, to build muscle memory.
- When you catch yourself using an old bad habit, stop and redo it correctly – For instance, if you just typed
rm filewithout-i, pressCtrl+Cand 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+Uto clear the current line – This is faster than backspacing. - Use
Ctrl+Lto clear the screen – Instead of typingclear. - Use
Alt+.to insert the last argument from the previous command – This is a quick way to reuse a filename. - Use
Ctrl+Wto delete the word before the cursor – Speeds up editing. - Use
Ctrl+Yto paste what you cut – Works withCtrl+WandCtrl+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
- Why Perfectionism Is Bad for You
- Is Yoga Bad for the Environment?
- How to Practice Self-Kindness on a Bad Day
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).