Skip to the content.

Shell aliases and functions can significantly improve your command-line productivity by allowing you to execute complex commands with simple shortcuts. In this post, I’ll show you how to create two useful aliases: one for listing open ports and another for freeing up occupied ports.

What are Shell Aliases and Functions?

Aliases are shortcuts for commands, while functions allow for more complex logic including parameters. Both are defined in your shell’s configuration file (.bashrc for Bash, .zshrc for Zsh).

How to Add Aliases to Your Shell

  1. Open your shell configuration file:
    • For Bash: nano ~/.bashrc or nano ~/.bash_profile
    • For Zsh: nano ~/.zshrc or ~/.zprofile
  2. Add the alias or function definitions at the end of the file.

  3. Save and exit the editor.

  4. Reload your configuration:
    • source ~/.bashrc or source ~/.zshrc

Useful Aliases for Port Management

Alias for Listing Open Ports

alias ports="lsof -iTCP -sTCP:LISTEN -P -n"

This alias uses lsof (list open files) to display all TCP ports that are currently listening. The flags mean:

Usage: Simply type ports in your terminal.

Function for Freeing Up Ports

function freeport() {
  if [ -z "$1" ]; then
    echo "Usage: freeport <port>"
    return 1
  fi
  kill -9 "$(lsof -tiTCP:$1 -sTCP:LISTEN)"
}

This function takes a port number as an argument and forcefully terminates the process using that port. The lsof command finds the process ID, and kill -9 sends a SIGKILL signal.

Usage: freeport 8080 to free port 8080.

Warning: Use freeport carefully as it will terminate processes without saving data.

Conclusion

These aliases can save you time when working with network services and debugging port conflicts. Add them to your shell configuration and enjoy a more efficient command-line experience!