facebook youtube pinterest twitter reddit whatsapp instagram

Creating Command Aliases in GNU/Linux (Command Shortcuts)

As time goes on, you'll need to be super productive with the way you use commands in GNU/Linux, and one example is utilizing alias.

Using alias allows you to create an additional equivalent name for a command, for example, you can simplify commands to one word or few letters, say you want to simplify sudo apt install to only 'install', you use the following command:

alias install="sudo apt install"

Entering the above command will provide no output, but behind the scene, a new command 'install' is now an alias(equals) to 'sudo apt install'

Verify the alias by running the 'alias' command:

1. All Aliases

You should see the alias you just created, you'll also see additional aliases in the output, If you are using Ubuntu, it has some alias created by default, for example, l is the same as ls.

Now when you want to install any packages, you can simply do the following:

install nginx

Super simple right! you don't have to type 'sudo apt install nginx', if you are way too lazy, you can simplify it further:

alias i="sudo apt install"

Now by adding 'i <package name>, you can install anything pretty fast with less typing.

One thing to note before we go further is that, when you exit your terminal window, your aliases are wiped out, ouch!. Fortunately, you can retain them by editing the .bashrc file, this file is present in your home directory
and is read every time you start a new Terminal session.

We can add an alias in there, but it is recommended we add it into a new file: ~/.bash_aliases, this makes it easier to maintain.

Once you are done, reload your bashrc file using:

source ~/.bashrc

Now, that you understand alias, let's see some cool example:

Clear Screen by Typing 'c'

alias c=clear

Jump Into Apache Log Folder

alias apl="cd /var/log/apache2"

We can be creative with the above command, why not jump into the folder, and also list the files in there? See the below command:

alias apl="cd /var/log/apache2 && ls"

This would replace the first one because I am using the same name 'apl'

Jump Into Nginx Log Folder and List All the Files in the Folder

alias ngl="cd /var/log/nginx && ls"

View the Top Ten Cpu Consuming Processes:

alias cpu10='ps auxf | sort -nr -k 3 | head -10'

View the Top Ten Ram Consuming Processes:

alias mem10='ps auxf | sort -nr -k 4 | head -10'

Jump To Users Home Directory

alias home="cd ~"

Removing alias

To remove alias, you use the 'unalias' keyword, so, to remove alias whos name is home, I'll do:

unalias home

The best way to come up with a aliases, is to think of commands you use on a regular basis, and simplify it, take a look at your bash history for commands you use.

Gracias

Related Post(s)

  • Send Mail with Attachment Using Mutt in GNU/Linux

    Mutt is a powerful text&#x2d;based mail client for Unix/Linux operating systems. It features color support, message threading, MIME support...

  • Installing WP-CLI In a GNU/Linux Server

    WP-CLI is a command-line interface for WordPress. It can also be used with ClassicPress, as they are no differences in their usage, maybe just minimal if you are updating or downloading new ClassicPr

  • Mounting and Unmounting Cloud Storage With (Rclone) in GNU/Linux

    Ever wondered if you could mount your preferred cloud storage as a virtual drive on your system? Thanks to Rclone, you can mount and access different kinds of cloud storage from a file manager, I'll

  • Access A Web Page In GNU/Linux Using (Links) [Web Browser in Text Mode]

    Links is a text-mode World Wide Web (WWW) browses that can be used to view a web page either via a local (file://) or remote ((http:// or ftp://) URLs. To get started with Links, install it using the

  • Synchronize File With Cloud Storage Using [Rclone] In (GNU/Linux)

    Rclone is an open-source command-line program to sync files and directories to and from different cloud storage providers. It preserves timestamps, and if you are transferring from local to cloud, it

  • Understanding the Linux Filesystem

    Understanding the filesystem is important if you want to know how data is stored and retrieved, but really, the term might be somewhat confusing in Linux, as it means two different things. A filesys