Smart Systems for Home

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

Terminal Automation: Cut Repetitive Work

· 6 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Identify your most repeated commands. Open your terminal and run history (or fc -l in 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.
  2. Create aliases for single commands. Aliases are shortcuts for one command. Open your shell config file — ~/.bashrc for bash, ~/.zshrc for zsh, or ~/.config/fish/config.fish for fish — in a text editor. Add lines like alias ll='ls -lah' or alias gs='git status'. Save the file and run source ~/.bashrc (or source ~/.zshrc) to apply changes immediately. Test with ll.
  3. 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:
    mkcd() {
      mkdir -p "$1" && cd "$1"
    }
    For fish, use 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 with mkcd testdir.
  4. Use history search to avoid retyping. Press Ctrl+R in bash or zsh to search your command history. Start typing a fragment and press Ctrl+R again to cycle through matches. Press Enter to run the selected command. This is faster than typing the whole thing again.
  5. 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.sh with:
    #!/bin/bash
    sudo apt update && sudo apt upgrade -y
    sudo journalctl --vacuum-time=7d
    Make it executable with chmod +x ~/bin/update.sh, then add ~/bin to your PATH in your config file (export PATH="$HOME/bin:$PATH"). Now you can run update from anywhere.
  6. 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.
  7. 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), and Ctrl+W (delete word). These reduce time spent editing long commands.
  8. 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.
  9. Use tab completion aggressively. Press Tab to complete commands, file paths, and options. For git, enable completion for branches and tags. In bash, ensure bash-completion is installed; in zsh, it's built-in. This saves dozens of keystrokes daily.
  10. 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 with echo first 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, but alias cd='cd ..' would break navigation. Use type command to see what it currently is.
  • Forgetting to source the config. After editing, you must run source or 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.sh every 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: gs for git status, ga for git add, gc for git commit.
  • Use fc to edit the last command in your default editor, then run it with fc -s.
  • For long commands you use occasionally, use Ctrl+R instead 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 echo before performing destructive actions like deleting or moving files.

Troubleshooting

  • Alias not working after sourcing. Check for typos in the config file. Run alias to 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_name to 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. Use echo $PATH to see if it's included.
  • Ctrl+R not searching history. In some shells, you need to enable history expansion. Ensure HISTSIZE is set to a large number in your config. Also, check if you're in vi mode (press Esc then Ctrl+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.