Terminal Automation: Cut Repetitive Work
In this article
This guide shows you how to reduce the repetitive typing and manual steps in your everyday terminal work. By creating aliases, shell functions, and simple scripts, you can turn multi-step commands into single keystrokes, automate file operations, and keep your environment consistent across machines. You'll need a basic comfort with the command line and a text editor, but no programming experience is required.
What You'll Need
- A Unix-like shell (bash, zsh, or fish) — most macOS and Linux systems have this by default.
- A text editor (nano, vim, VS Code, or any you prefer).
- Basic familiarity with navigating directories and running commands.
- Optional: Git for versioning your dotfiles (the config files we'll edit).
Step-by-Step
- Identify your most repeated commands. Open your terminal and run
history(orfc -lin zsh) to see your recent commands. Look for patterns: commands you run daily, long commands with many flags, or sequences you always chain together with&&. Make a short list of the top five. - Create aliases for single commands. Aliases are shortcuts for one command. Open your shell config file —
~/.bashrcfor bash,~/.zshrcfor zsh, or~/.config/fish/config.fishfor fish — in a text editor. Add lines likealias ll='ls -lah'oralias gs='git status'. Save the file and runsource ~/.bashrc(orsource ~/.zshrc) to apply changes immediately. Test withll. - Turn command sequences into functions. When you need multiple commands together, a function is more flexible. In your config file, add a function like this for bash/zsh:
For fish, usemkcd() { mkdir -p "$1" && cd "$1" }function mkcd; mkdir -p $argv[1]; and cd $argv[1]; end. This creates a directory and moves into it in one step. Save and source the file, then test withmkcd testdir. - Use history search to avoid retyping. Press
Ctrl+Rin bash or zsh to search your command history. Start typing a fragment and pressCtrl+Ragain to cycle through matches. Press Enter to run the selected command. This is faster than typing the whole thing again. - Write a simple script for multi-step tasks. For a task you do repeatedly, create a script. For example, to update your system and clean up logs, create a file
~/bin/update.shwith:
Make it executable with#!/bin/bash sudo apt update && sudo apt upgrade -y sudo journalctl --vacuum-time=7dchmod +x ~/bin/update.sh, then add~/binto your PATH in your config file (export PATH="$HOME/bin:$PATH"). Now you can runupdatefrom anywhere. - Automate file naming with date stamps. For backups or logs, use the date command. In a function or script, capture the date:
today=$(date +%Y%m%d), then use it in filenames, e.g.,cp data.csv backup_$today.csv. This prevents overwriting and keeps files organized. - Use keyboard shortcuts for navigation. Learn
Ctrl+A(go to start of line),Ctrl+E(end),Ctrl+U(delete line),Ctrl+K(delete to end), andCtrl+W(delete word). These reduce time spent editing long commands. - Create a dotfiles repository. Initialize a Git repo in your home directory (or a dedicated folder) and add your config files. Commit them and push to a private remote (like GitHub). On a new machine, clone the repo and symlink the files into place. This keeps your aliases and functions consistent everywhere.
- Use tab completion aggressively. Press
Tabto complete commands, file paths, and options. For git, enable completion for branches and tags. In bash, ensurebash-completionis installed; in zsh, it's built-in. This saves dozens of keystrokes daily. - Batch rename files with a loop. Instead of renaming one by one, use a for loop. For example, to add a prefix to all .txt files:
for f in *.txt; do mv "$f" "prefix_$f"; done. Test withechofirst to see what would happen.
Common Mistakes
- Overwriting existing aliases. Before creating an alias, check if the command already exists. For example,
alias ls='ls --color=auto'is fine, butalias cd='cd ..'would break navigation. Usetype commandto see what it currently is. - Forgetting to source the config. After editing, you must run
sourceor open a new terminal. Otherwise, changes don't take effect and you think they're broken. - Not quoting variables in scripts. If a filename has spaces, unquoted variables break. Always use
"$var"in scripts and functions. - Making scripts executable but not adding to PATH. If you run
./script.shevery time, you haven't saved keystrokes. Add the directory to PATH or create an alias. - Using aliases for complex logic. Aliases are for simple substitutions. If you need arguments or conditionals, use a function or script.
Tips
- Start small: add one alias a day and use it until it's muscle memory.
- Name aliases mnemonically:
gsfor git status,gafor git add,gcfor git commit. - Use
fcto edit the last command in your default editor, then run it withfc -s. - For long commands you use occasionally, use
Ctrl+Rinstead of an alias — it keeps your config clean. - Comment your aliases and functions in the config file so you remember what they do.
- Test scripts with
echobefore performing destructive actions like deleting or moving files.
Troubleshooting
- Alias not working after sourcing. Check for typos in the config file. Run
aliasto list active aliases. If it's not there, the file wasn't sourced or the line is commented out. - Function not recognized. Ensure the function name doesn't conflict with an existing command. Use
type function_nameto see if it's defined. Also check that the syntax matches your shell (bash vs. zsh vs. fish). - Script says 'Permission denied'. Run
chmod +x script.sh. Also ensure the shebang line (#!/bin/bash) is correct. - PATH changes not taking effect. After editing PATH, source the config. If you added
~/bin, verify the directory exists. Useecho $PATHto see if it's included. - Ctrl+R not searching history. In some shells, you need to enable history expansion. Ensure
HISTSIZEis set to a large number in your config. Also, check if you're in vi mode (pressEscthenCtrl+R).
Frequently Asked Questions
Q: Will aliases slow down my terminal?
A: No. Aliases are expanded before the command runs; they have zero performance impact.
Q: Can I use the same config on macOS and Linux?
A: Yes, but be careful with differences in commands (e.g., ls flags). Use conditional blocks in your config to handle OS-specific settings.
Q: How do I remove an alias temporarily?
A: Type unalias aliasname to remove it for the current session. To bypass an alias for one command, prefix with a backslash: \ls.
Q: What if I break my shell config?
A: Open a new terminal (it may still work) and edit the file with a plain text editor. If you can't open a terminal, use a recovery mode or a live USB to fix the file.
Q: Are there tools that do this automatically?
A: Yes, tools like fzf for fuzzy file finding, z for quick directory jumps, and autojump can further reduce typing. But the basics here work everywhere without extra installs.
Q: How do I keep my dotfiles private?
A: Use a private Git repository. If you use GitHub, you can make a repo private for free. Alternatively, use a local Git repo and sync via USB.