Creating Linux aliases for daily used Terminal commands - Starline Computer: Storage und Server Lösungen von erfahrenen Experten

Creating aliases for complex and daily used Terminal commands

How to simplify your life with aliases under Linux.

Creating aliases for complex and daily used Terminal commands

When scanning for SNMP devices on a local network I use the following command;

nmap -sU -p 161 -T4 -d -v -n -Pn --script snmp-interfaces 10.0.0.1-254 |  grep -E '(IP address|MAC address)'

In addition to requiring a solid memory, there are a lot of chances I will be making plenty of errors when typing this command. So, to avoid typo frustration and make things easier for myself, I programmed my Terminal environment to accept snmpscan as an alias for the above command.

An alias is a short, easy-to-remember word or phrase, which substitutes a long and/or a complex Unix command.  Some Terminal interactions are intricate and sometimes even case-sensitive. To avoid having to copy/paste lengthy commands from your cheat sheet, we discuss how to create easy-to-remember shortcuts for those commands.

Important: In this example we will create two aliases for the AnsiWeather command utility. For a detailed guide on how to install this cool utility on your Linux or macOS system please check here.

If you are not comfortable installing the AnsiWeather utility, you edit the examples below to run your preferred commands instead.

Adding command aliases on Debian/Ubuntu Linux

To add a system-wide alias that can be used by all logged in users you need to edit the file /etc/bash.bashrc to include your aliases. If you want those aliases to be available only for your user profile then you need to create, or edit, the ~/.bash_profile file in your home directory. The settings you add to those files get loaded once you launch your Terminal, or remotely log in via SSH.

Let’s take the first approach and make some globally available aliases. In your Terminal window do;

$ sudo nano /etc/bash.bashrc

Use the arrow keys (or CTRL+V) to scroll down all the way to the end of the file and paste your new aliases there.

Paste the following lines;

############################### My custom aliases ################################

alias weather='ansiweather -l Stuttgart,DE'

alias weather5='ansiweather -l Stuttgart,DE -u metric -s true -f 5 -d true'

############################### End custom aliases ###############################

Bash_Config

To save your changes press CTRL+X and confirm by pressing the “Y” button followed by the “Enter” button.

Now, to apply and use your new aliases you can either close then reopen your Terminal, or you can re-import the /etc/bash.bashrc file with the new modifications like so;

$ source /etc/bash.bashrc

To list all aliases available on the system, do;

$ alias

List_Aliases

Hinzufügen von Befehlsaliasen unter macOS

Adding command aliases on macOS

macOS Catalina and newer versions use the ZSH shell, while older versions use the BASH shell. So, depending on your version of macOS, you will need to edit the appropriate file.

This time let’s create aliases that apply only to your user profile.

For macOS Catalina, Big Sur, Monterey, and newer versions do;

$ nano ~/.zprofile

For older versions of macOS, do;

$ nano ~/.bash_profile

Paste the following lines at the end of either files;

############################### My custom aliases ################################

alias weather='ansiweather -l Stuttgart,DE'

alias weather5='ansiweather -l Stuttgart,DE -u metric -s true -f 5 -d true'

############################### End custom aliases ###############################

To save your changes press CTRL+X and confirm by pressing the “Y” button followed by the “Enter” button.

Now, to apply and use your new aliases you can either close then reopen your Terminal, or you can re-import the file you just edited with the new modifications like so;

For macOS Catalina, Big Sur, Monterey, and newer versions do;

$ source ~/.zprofile

For older versions of macOS, do;

$ source ~/.bash_profile

And you can list the new aliases by doing;

$ alias

Finally, if you want your aliases to be available to all logged in macOS users, then you do;

$ sudo nano /etc/zprofile  (note that there is no “.” at the beginning of the file name)

Tip: If you are working on a remote machine that you should not alter the configuration for and/or if you want those aliases to be cleared once you terminate your SSH session, then simply copy/paste your aliases into your SSH window. This will set up all your comfy aliases for that session only – provided that all the binaries those aliases trigger are installed on the remote machine. This can be quite useful when diagnosing or administering remote servers.

Example: pasting the following into your macOS Terminal will add three aliases;

$ alias cl='clear'\ alias toppy='top -U Maxwell'\

alias showdns='networksetup -getdnsservers Wi-Fi'\

alias snmpscan='sudo nmap -sU -p 161 -T4 -d -v -n -Pn --script snmp-interfaces 10.10.0.1-254 |  grep -E '\''(IP address|MAC address)'\'' '\

starline_logo_kontur_300
Enterprise Storage Solutions Team
Technik

Our experts are of course also experts in Linux, Ceph and ZFS