How to use alias in Linux
The alias command can be used to simplify frequently used or complex commands on the command line into a new command. This article shows how to use alias and how to permanently store aliases.
Use
An alias helps to simplify a complex command on the command line.
Example
You can use the following command to display the amount of RAM used:
free -m -s 1 -c 60 | grep -B 1 Speicher >> memory.log &
The command checks every second for 60 seconds how much memory is available and in use in MiByte and writes the information to the file memory.log.
However, the command is quite complex. You could write a script for this and call it with bash when you need it.
THe definition of an alias is easier:
alias freemem="free -m -s 1 -c 60 | grep -B 1 storage >> memory.log &"
Now, the command can be called up by entering freemem
Definition request
If you have defined several aliases, it is easy to lose track of them. Calling alias without options or parameters will display a list of the defined aliases.
Integration at system startup
If you have defined an alias according to the instructions above, this definition only applies during the current session.
There are two ways to make an alias available after restarting: user-specific integration and global integration.
User-specific integration
For certain users, all alias commands in the /home-directory of each user are written to the ~/.bash_aliases file.
To ensure that the file is also called up after the user logs in, the corresponding call must still be present in ~/.bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Example ~/.bash_aliases
We stay in our alias freemem. In the file, the alias is stored as follows:
#! /usr/bin/env bash # A list with permanent aliases for the current user. alias freemem="free -m -s 1 -c 60 | grep -B 1 Speicher >> memory.log &"
Global integration
To make an alias user-independent, it must be stored in the /etc/bash.bashrc file. This requires root privileges.
| hint: You are editing a system-critical file. Please exercise extreme caution when editing! |
Example /etc/bash.bashrc
At the end of the file, you can enter the following lines:
# A list of global aliases. alias freemem="free -m -s 1 -c 60 | grep -B 1 Speicher >> memory.log &"
|
Author: Stefan Bohn Stefan Bohn has been employed at Thomas-Krenn.AG since 2020. Originally based in PreSales as a consultant for IT solutions, he moved to Product Management in 2022. There he dedicates himself to knowledge transfer and also drives the Thomas-Krenn Wiki. |
|
Translator: Alina Ranzinger Alina has been working at Thomas-Krenn.AG since 2024. After her training as multilingual business assistant, she got her job as assistant of the Product Management and is responsible for the translation of texts and for the organisation of the department.
|



