alias  commands in Linux

alias commands in Linux

·

4 min read

In Linux, an alias is a command that executes another command or series of commands using a different name. Aliases are typically used to make it easier to run frequently used or complex commands by typing a shorter, easier-to-remember version. The alias command takes the following structure:

alias [alias-name]='[command]'
  • alias: Invokes the alias command.

  • alias-name: An alias is a user-defined string that acts as a reference to a command, with the exception of certain special characters and the words 'alias' and 'unalias'.

  • command: Specifies the command the alias references.

For example:

  1. alias ll='ls -al': This alias creates a new command called ll that is equivalent to running the ls -al command. You could use this alias to list the contents of a directory in long format by typing ll instead of ls -al.

  2. alias df='df -h': This alias creates a new command called df that is equivalent to running the df -h command. The -h flag is used to display the sizes of files in "human-readable" format (e.g., in MB or GB instead of bytes).

  3. alias grep='grep --color=auto': This alias creates a new command called grep that is equivalent to running the grep --color=auto command. The --color=auto flag tells grep to highlight the matching text in the output, making it easier to spot.

  4. alias shutdown='sudo shutdown -h now': This alias creates a new command called shutdown that is equivalent to running the sudo shutdown -h now command. The -h flag tells the shutdown command to halt the system, and the now argument tells it to do so immediately.

  5. alias update='sudo apt update && sudo apt upgrade -y': This alias creates a new command called update that is equivalent to running the sudo apt update and sudo apt upgrade -y commands. The update command updates the package manager's list of available packages, and the upgrade command installs any available updates for installed packages. The -y flag tells apt to assume "yes" as the answer to all prompts.

A few ways I use the alias command in my day to day development:

Copying trading bots from wine to my documents.

I currently work on Linux but need to do MQL5 programming from time to time. With MQL5, I can write trading bots for clients. However, to compile the MQL5 code to an .ex5 executable, I need to have MT5 installed. MT5 is a Windows and Mac program; therefore, I have to install wine on Linux. Wine is a compatibility layer that allows you to run Windows applications on Linux. It does this by translating the Windows application's calls into calls that the Linux operating system can understand and processing them on behalf of the application. This allows you to run Windows applications on Linux without installing a copy of the Windows operating system. Once the code is compiled into an executable, the files are stored in the wine drive. To copy the .ex5 to a specific folder, I would normally run the following command

cd /home/wamaitha/.wine/drive_c/Program\ Files/Deriv/MQL5

The MQL5 directory has a folder that holds all the trading bots I program. Therefore, I can copy all the .ex5 files to the EAS folder with the command below.

cp -r Experts/**/*.ex5 ~/Documents/FOREX/EAS

To make my work easier, I've written an alias command that does all this

alias fxeas="cd /home/wamaitha/.wine/drive_c/Program\ Files/Deriv/MQL5 && cp -r Experts/**/*.ex5 ~/Documents/FOREX/EAS"

Activating virtual environments

In Python when using virtualenv , to activate the environment on Linux we need to type the following command:

source venv/bin/activate

Then navigate back to the project folder

cd .. && cd ..

We can write an alias for this

alias activate="source venv/bin/activate && cd .. && cd .."

Permanent aliases

Using the alias command only applies to the current terminal session. To make the commands useful on other terminal sessions, you would need to edit the .bashrc file if running bash or the .zshrc if on zsh. I'm currently on zsh , therefore I add these commands at the end of the .zshrc file.

Launch the .zshrc using the command below

nano ~/.zshrc

Add the alias commands at the end of the file

alias activate="source venv/bin/activate && cd .. && cd .."
alias fxeas="cd /home/wamaitha/.wine/drive_c/Program\ Files/Deriv/MQL5 && cp -r Experts/**/*.ex5 ~/Documents/FOREX/EAS"

Run the command below to activate the changes for all terminal sessions.

source ~/.zshrc