Last updated on December 17, 2023
Table of Contents
Quick preview
Overview
On Window, we got used with Desktop shortcut which we can quickly open an application. In fact, opening an application always needs a command. On Window, Microsoft engineers made it shorter and easier by using shortcut. So how about on Linux? Yes, they are same. On Linux we also using command to open applications. This is an example:
sudo service mongod start
This command is used for start MongoDB . It is easy right, not so soon ! This is a customized command from Apache2 programmers. Sometime we need to execute an command with parameters to change our own configuration. Something is like this:
$ /opt/mongodb/4.4.3/bin/mongod --config /opt/mongodb/4.4.3/mongod-4.4.3.conf --dbpath ~/database/mongodb/4.4.3
Wow, this is crazy right. In this post, I won’t focus too much about what above command does. If you want to figure out, please refer my post about MongoDB [How to install multiple versions of MongoDB on Linux], but now I gonna talk about how can we make above command shorter. Yes, we have solution by using Alias. So what is Alias? Basically, an alias is shortcut to run command that you have to define by yourself. So the question is: “How to set alias for command?”. Let’s start at below. Cheers.
How to set alias for command in Linux
Welcome, you are continue to read my post. Thank you very much !
To set an alias, we need to find the user bash profile on Linux system. In common, they could be .bash_profile or .bashrc that would locate in ~/
which means /home/{USER}/
. To check they are existed, use following command
$ ls -la ~/.bash_profile
or
$ ls -la ~/.bashrc
If you see this, which means file exists.
root@a973853d25fc:/# ls -la ~/.bash_profile
-rw-r--r-- 1 root root 1644 Mar 29 15:46 /root/.bash_profile
OR
root@a973853d25fc:/# ls -la ~/.bashrc
-rw-r--r-- 1 root root 3106 Dec 5 2019 /root/.bashrc
Otherwise, you need to create them. There is several ways to create a file in Linux, please read [https://linuxize.com/post/create-a-file-in-linux/]
On my flavour, I always use ./bash_profile file to define my alias. Ok, let’s get it done.
1. Open ./bash_profile file by one of your favorite text editor, I am using nano.
$ sudo nano ~/.bash_profile
2. Add alias by the following syntax
alias {alias_name}="{command}"
For example:
alias cdwww="cd /var/www"
In above alias, I defined alias “cdwww” to run the command “cd /var/www”. Sometime I have to go in and go out directories regularly, some of them I usually do it like “cd /var/www”. So I make it faster by “cdwww”. It is easy to type and switch between directories right. 😀
3. Ok, now let’s see how does Alias help me to solve my problem with long start command of MongoDB. This is what I have to type everyday.
$ /opt/mongodb/4.4.3/bin/mongod --config /opt/mongodb/4.4.3/mongod-4.4.3.conf --dbpath ~/database/mongodb/4.4.3
Now, I put it in alias
alias startMongo="/opt/mongodb/4.4.3/bin/mongod --config /opt/mongodb/4.4.3/mongod-4.4.3.conf --dbpath ~/database/mongodb/4.4.3"
As you can see, I defined “startMongo” alias to run this command. Whenever I want to start my MongoDB, I just type simply $ startMongo
and it will work
root@a973853d25fc:/# startMongo
about to fork child process, waiting until server is ready for connections.
forked process: 261
child process started successfully, parent exiting
Hurray, it is so simple right. You can define as many as you want. If you want to see my daily using alias. Please refer at next part [Some of my daily using alias]
Oh, I forgot ! When you define a new alias or sometime your aliases do not work. You need to apply this by this command.
$ source ~/.bash_profile
or
$ source ~/.bashrc
4. There are some advanced definitions for Alias. There are some of standout example for this.
- Your commands have “” (double quote) in it, such as:
$ mongo -u root --db "awesome_database_name"
I won’t be able to define alias because of mixture of double quote like
alias mongoshell="mongo -u root --db "awesome_database_name""
It’s easy to solve, just replace your alias double quote to single quote, and you can use many double as you wish.
alias mongoshell='mongo -u root --db "awesome_database_name" --param2 "value_for_param2"'
And remember to apply your new alias $ source ~./bash_profile
- You need to pass more parameters of original command to your alias.
That is no problem at all, just pass parameters as original one does.
$ mongoshell --authenticationDatabase admin --port 27017
With above command, it will be like this if it’s not an alias
$ mongo -u root --db "awesome_database_name" --authenticationDatabase admin --port 27017
Some of my daily using alias
alias ll="ls -la" alias lash="ls -lash" alias cdvhost="cd /etc/apache2/sites-available/" alias cdwww="cd /var/www" alias addsite="/var/www/create-site.sh" alias startApache="service apache2 start" alias stopApache="service apache2 stop" alias statusApache="service apache2 status" alias restartApache="service apache2 restart" alias startphp56="service php5.6-fpm start" alias statusphp56="service php5.6-fpm status" alias startphp72="service php7.2-fpm start" alias statusphp56="service php5.6-fpm status" # 4.4.3 alias startMongo="/opt/mongodb/4.4.3/bin/mongod --config /opt/mongodb/4.4.3/mongod-4.4.3.conf --dbpath ~/database/mongodb/4.4.3" alias stopMongo="/opt/mongodb/4.4.3/bin/mongod --config /opt/mongodb/4.4.3/mongod-4.4.3.conf --dbpath ~/database/mongodb/4.4.3 --shutdown" alias nanoMongo="nano /opt/mongodb/4.4.3/mongod-4.4.3.conf" # 2.6.12 alias mongo26="/opt/mongodb/2.6.12/bin/mongo --port 27018" alias mongoRestore26="/opt/mongodb/2.6.12/bin/mongorestore --port 27018" alias mongoDump26="/opt/mongodb/2.6.12/bin/mongodump --port 27018" alias startMongo26="/opt/mongodb/2.6.12/bin/mongod --fork --dbpath ~/database/mongodb/2.6.12 --config /opt/mongodb/2.6.12/mongod-2.6.12.conf" alias stopMongo26="/opt/mongodb/2.6.12/bin/mongod --dbpath ~/database/mongodb/2.6.12 --shutdown" alias nanoMongo26conf="nano /opt/mongodb/2.6.12/mongod.conf" alias nanoHost="nano /etc/hosts" alias pecl="php -f /usr/bin/pecl" # GIT alias deploy="git pull origin master && ls -la" alias tailLog="tail -f /var/log/apache2/access.log" alias tailAccess="tail -f /var/log/apache2/error.log" alias tailMongo="tail -f /var/log/mongodb/mongod.log" alias tailMongo26="tail -f /var/log/mongodb/mongodb.log"
Hope it help for you.
Comments are closed.