The Alias command in GNU/Linux and helpful tips with it

Mike Turcotte-McCusker
Mar 6, 2018
Linux
|
11

There is a terminal command called alias, that many users may not know exists, but likely will be very happy to find out about it.

The alias command, allows you to redefine what you type, to make things happen. It gives you the option to map large commands to an alias that you may run then instead of the large command to execute it.

A very straightforward (although risky) example of this would be:

alias dla="rm -rf *"

The reason this is both handy, but risky, is now if I enter any directory and simply type dla, I will delete everything in that directory. I did not add sudo to this, purposely, so at least system files can’t be deleted through this method, but caution must still be used. However, once accustomed to it, this alias combined with others like it, can speed up workflow exponentially.

Tip: Type alias to display the list of aliases set on the Linux machine.

commandline aliases linux

Another that I use frequently, since I use nginx as my webserver of choice on all my remote servers, and I have to restart or reload nginx often when mucking around with configurations, so I use the alias:

alias n="sudo service nginx restart"

Now, I simply need to type the letter n, and the command will be executed, saving me insane amounts of time and keystrokes.

Here are some other aliases that you might find useful!

  • alias l="ls -al" -- now, simply type a lowercase L, and receive a more detailed list of files/folders in the current directory
  • alias dla="rm -ri *"-- a much safer but more annoying shortcut. This command will delete everything (files and folders) in a directory, but will confirm each and every deletion one by one as it happens. While tedious, this can save you huge headaches from accidentally deleting a key file.
  • alias update="sudo apt update && sudo apt upgrade"-- one word will start the upgrade process, if need be, when using Debian based systems.
  • alias psmem="ps auxf | sort -nr -k 4"-- This will show you your processes, ordered by highest RAM usage.
  • alias pscpu="ps auxf | sort -nr -k 3"-- This will show you your processes ordered by highest CPU usage.
  • alias home='cd /home/martin/' -- Switch to the /home/martin/ directory when you type the alias.

Last thoughts

Aliases can make things so much faster when working with the command line, but always be careful not to set aliases up that could destroy your system with a simple keystroke, like a="sudo rm -rf *"as this runs far too many risks.

Now you: Do you use any aliases? What? Show us in the comments!

Summary
The Alias command in GNU/Linux and helpful tips with it
Article Name
The Alias command in GNU/Linux and helpful tips with it
Description
There is a terminal command called alias, that many users may not know exists, but likely will be very happy to find out about it. The alias command, allows you to redefine what you type, to make things happen. It gives you the option to map large commands to an alias that you may run then instead of the large command to execute it.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Previous Post: «
Next Post: «

Comments

  1. VOCFM said on March 11, 2018 at 9:27 pm
    Reply

    Thanks to all the patreons!!!

  2. Karan Rajpal said on March 10, 2018 at 7:03 am
    Reply

    You don’t need a cd alias to go to your home directory. Just type `cd` without any args and it should do just that. At least it’s been doing that for years on my Ubuntu system.

  3. John Wiersba said on March 8, 2018 at 4:14 pm
    Reply

    I have to second John Fenderson’s comment above. I use aliases only when there’s no other way (e.g. an alias for cd). Everything else is so much easier and more flexible with scripts. Additionally, by my own convention, I start my script names with “.” when those scripts need to be dotted to run (usefully). Usually I will create an alias to automatically dot those scripts which need to be dotted, so I don’t have to remember it or type it. When dotting a script, if you want to pass arguments to it, you will need to add a dummy argument first, since the shell’s behavior makes the case of no arguments indistinguishable from other cases when there are positional parameters set. The dummy argument makes it possible to distinguish no arguments to the dotted script vs. 1+ arguments. For example:

    alias cd=’. .mycd dmy’ # dot .mycd script with one (dummy) argument

  4. John Fenderson said on March 7, 2018 at 5:02 pm
    Reply

    “Do you use any aliases? What?”

    In my younger days, I used aliases quite a lot. I’ve stopped, though, for three reasons.

    First, aliasing common commands to include commonly used switches is great (that’s mostly what I used to do) — but you come across the problem of eventually forgetting that you’ve aliased the command and getting confused about why it doesn’t work right when you sit at a different machine.

    Second, I’ve come to prefer using shell scripts instead of aliases because then I have all of these things in one directory (I make a “bin” directory and add it to my path to hold my custom scripts). It just makes maintenance easier to only have to look in one place instead of two.

    Third, portability — by implementing this stuff as shell scripts, it makes it much easier to move everything to another machine. All I have to do is copy my “bin” directory and add it to the path on the new machine, and I have everything.

  5. cinikal said on March 7, 2018 at 4:44 pm
    Reply

    Now you: Do you use any aliases? No never knew this existed. Could use a bookmark speed dial type of gui (add, edit, remove) with a small terminal window built in. q:D

  6. Klaas Vaak said on March 7, 2018 at 12:43 pm
    Reply

    @Mike: if I understand you well, it means in the command panel one has to type in the alias command 1st, e.g. alias n=”sudo service nginx restart”, then after Enter, type the alias shortcut, n in this case. Is that right?

    1. Mike Turcotte said on March 8, 2018 at 12:30 am
      Reply

      Correct.

      alias n=”sudo service nginx restart”

      After setting this alias, simply typing the letter ‘n’ alone in the future as a command, will initiate “sudo service nginx restart”

  7. C said on March 7, 2018 at 2:16 am
    Reply

    We use aliases across hundreds of servers. Its all pushed out using Ansible.
    It means a consistent experience across all servers.
    We deploy very complex middleware software.
    Without aliases life would be very very hard.

    Good shout Martin… but we know that’s par for the course…. ;)

  8. jasray said on March 6, 2018 at 11:50 pm
    Reply
  9. naveed said on March 6, 2018 at 9:12 pm
    Reply

    Nice article, more like this would be appreciated.

  10. Geoff said on March 6, 2018 at 7:19 pm
    Reply

    alias rm=”rm -i”

    Makes a better example for the uninitiated, perhaps.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.