Results 1 to 4 of 4

Thread: Get grip with aliases in Linux

  1. #1
    Administrator M.A.A's Avatar
    Join Date
    Oct 2011
    Posts
    345
    ΑΡ Credit
    3450

    Get grip with aliases in Linux

    Most Linux users are well versed in the typing out of lengthy commands in the Terminal. They're especially hardened and generally insured against such sudden lapses in concentration, where they have to go back mid-command to correct an error.
    Name:  An alias can also cut down on more complex commands and introduce a more friendly approach.jpg
Views: 462
Size:  49.5 KB


    The rest of us, however, have to deal with the fact that an error is inevitable and that we more often than not have to enter the command again to get it working correctly. There is another option available for Linux users, though. Instead of constantly or frequently having to tap out the complex line of instructions, we can instead use an Alias.

    The Alias Command
    The Alias command isn't just for Linux; there are examples of it being used via the DOS Key command in the past with DOS and earlier versions of Windows, and more recently appearing in the Windows PowerShell. However, it was introduced back in the old UNIX days and made life significantly simpler for the users of the time.

    In essence, the Alias command can be issued to replace a common command with a shorter word. So where in Linux the command 'sudo apt-get install' needs to be inputted, an alias can be set up so all the user has to do is type in 'install', for example. This will then last for as long as the user is in the Terminal for, but they can be set as permanent, which we'll get into later on.

    Using An Alias

    Alias, as we use with Bash can be viewed by entering the following into the Terminal:

    Code:
    alias
    Once Enter is pressed, you'll be presented with a list of currently used aliases. In the current version of Linux Mint, 16, we have:
    Code:
    alias grep='grep --colour=auto'
    alias ll='ls-al'
    alias Is='ls --colour=auto'
    Let's start with something simple. The pwd command, when entered into a Terminal, displays the current location of the user in the system directory structure; it stands for Present Working Directory. If we wanted to, instead of typing in 'pwd', to create an alias for this, then we could opt for simply entering p.

    If you were to enter the letter p into the Terminal, the chances are you'd get the 'command not found' response back. So if you now enter the following:
    Code:
    alias p="pwd"
    Now press Enter. The next time you enter just p you'll get the same command as if you entered pwd. Moving on a little, if we were to use the example mentioned earlier, the sudo apt get install command, we can change it to the following:
    Code:
    alias install= "sudo apt-get install"
    After pressing Enter the alias should now be set, and if we wanted to install a specific package, such as Gimp for example, then all we'd need to enter (after setting the alias) from then on is install Gimp. You will still need to enter your password, but the command will be issued as it would if you typed it in normally.
    Now try:
    Code:
    alias poweroff="sudo shutdown -h now"
    alias update="sudo apt-get update"
    alias upgrade=" sudo apt-get upgrade"
    As you imagine these three examples can be tweaked to your particular liking. And you can also include a more complex command in the alias, so where update and upgrade do different commands, you can combine it to one. For example:

    Code:
    alias sysupgrade="sudo apt-get update && sudo apt-get upgrade"
    It can get a little messy like this, though. Thankfully, there is a way to chain the aliases together. Chaining Aliases Let's say we set the update and upgrade alias to both sudo apt-get update and sudo apt-get upgrade individually. Rather than creating another messy alias we could simply chain the aliases together so you can enter:
    Code:
    update && upgrade
    The result is exactly the same as if you entered the commands in their original format hence the need for the double ampersand (&&) to combine two separate commands.
    Name:  And chain them together for commands and extensions.jpg
Views: 411
Size:  33.0 KB


    There's also a way to alias both the command and any command line extensions that you may want after it. There are a number of ways this can be useful the best being the obvious fact that you often forget the extension (in our case anyway) so where the command ls -laxo is used we can alias it down somewhat. For example we can friendly the LS command to:
    Code:
    alias list=" ls " (notice the space after the Is command)
    Then add:
    Code:
    alias files=" -laxo"
    The result can then be used as:
    Code:
    list file ./Downloads
    To list the files and folders within the Downloads directory.

    This example also displays the fact that an alias doesn't always have to be a shortened version of the original command but instead it can be customized to be more friendly to the user.

    No More Aliases

    If you've made more aliases than you care to remember or you want to use the friendly name for something else then you can remove an alias by entering the following:

    Code:
    unalias sysupgrade
    The unalias command will now remove the alias defined after.

    Making a Permanent Alias
    If you've now set up a collection of aliases and you're very happy with their outcomes then having them permanently accessible across the system whenever you reboot the PC is certainly going to be advantageous.

    After all you don't want to have to re-enter them every time. There are a couple of ways of doing this. The first is to create a Bash script that will create the aliases every time you run it which is a little messy. The second method is to alter the bash.bashrc file as found in /etc/.
    To do this enter the following:


    Code:
    sudo gedit /etc/bash.bashrc
    We used the text editor Gedit in this case but the file bash.bashrc should now be open and ready for you to add any aliases you want for your system. If you scroll down slightly you should be able to enter the new alias commands after he first if and fi commands as shown in our screenshot. When you're done save the file, exit and reboot the system. Once it's rebooted you can drop in to the Terminal again and enter the alias command on its own to see if they've worked.

  2. #2
    Member Malcolm's Avatar
    Join Date
    Sep 2013
    Posts
    48
    ΑΡ Credit
    480
    could you use an alias to store the name of a string (aka a -TARGET for some certain command)? For example, I do my web development locally on my desktop and when I'm ready to look at the product, I upload to my web server and view it from there. So everytime I'm ready to view, I scp the files to my server and it's often a lengthy command depending on what directory I'm in
    ("scp /home/tom/Programming/Web/website_template/* pi@SERVER-IP:/var/www/"). Is there a way to store the "pi@SERVER-IP:/var/www/" as an alias, so that I could simplify the command to something like "scp /home/tom/Programming/Web/website_template/* piserv", where piserv is the entire web server path?

    also, I know that if I cd in the Web directory that would shorten up the command alot, sometimes my terminal location is elsewhere in the filesystem though so I have to use the full path

  3. #3
    Administrator M.A.A's Avatar
    Join Date
    Oct 2011
    Posts
    345
    ΑΡ Credit
    3450
    I think in your case you would be better off using a function and adding it to your bashrc. This would enable you to implement more complex structures and have it be done in a clean fashion.
    e.g.

    Code:
    function foo() { /path/to/command "$@" ;}
    and then finally, call your foo using a syntax like
    Code:
    foo arg1 arg2 argN

  4. #4
    Administrator M.A.A's Avatar
    Join Date
    Oct 2011
    Posts
    345
    ΑΡ Credit
    3450
    In effect, you could do something like this for what you want. Granted I haven't test this but I don't see why it wouldn't work.
    Code:
    function piserv() {"$@" pi@SERVER-IP:/var/www/;}
    And then call it via

    Code:
    piserv scp /home/tom/Programming/Web/website_template/*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •