Helpful Aliases

Discussion in 'Linux Tips' started by DevynCJohnson, Oct 13, 2013.

  1. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,214
    Likes Received:
    1,019
    Trophy Points:
    113
    The alias command is a very helpful command to newbies, programmers, and professional administrators alike. The alias command creates a shortcut or alternate name for a command or set of commands. These new commands are saved in ~/.bash_aliases. Aliases could be viewed in the mind of Linux users as executable variables if one wished. Typing "alias" in a terminal will print the current aliases for the current user. To create an alias, type

    Code:
    alias today="date '+%m/%d/%y'"
    Now, the user can type "today" to see the current date in the format Month/Day/Year.

    However, this alias will no longer exist when the user logs out or closes the terminal. To make an alias permanent, open ~/.bash_aliases in a text editor and paste the whole command (alias today="date '+%m/%d/%y'") on its own line in this file. Alternately, a user can perform this action in a command line by typing


    Code:
    echo "alias today=\"date '+%m/%d/%y'\"" >> ~/.bash_aliases
    NOTE: Remember to escape special characters.

    This command will append the alias line to the end of the alias file.

    To remove a temporary alias that was created in a terminal, type "unalias ALIASNAME", where ALIASNAME is the alias to remove. Unalias also suppresses a permanent alias temporarily. One helpful fact to take note of is aliases (whether temporary or permanent) are not used in scripts. For instance, if a user had "today" in a script, the alias command will not execute unless the user happened to have an installed program or script function called "today".

    To permanently remove a permanent alias, remove that line via a text editor.

    The uses for aliases are limited by the user's imagination and needs. Users could make a large alias file full of many aliases that can make the terminal very easy to use.

    Some helpful alias examples are included below. A description follows each one after the pound sign. (Do not paste the comments with the aliases into the alias file.)


    Code:
    alias bye="exit" #Users can type "bye" to exit a terminal.
    
    alias BYE="exit" #BASH is case-sensitive, so "bye" is different from "BYE"
    
    alias Exit="exit" #If a user has a bad habit of capitalizing the first letter of commands, this alias will handle that issue
    
    alias EXIT="exit" #This alias helps users that tend to leave caps-lock on.
    
    alias clr="clear" #This is a shortcut for "clear"
    
    alias clsh="clear; history -c; echo \"\" > ~/.bash_history; exit" #This alias command will clear the terminal, delete the history, and exit. The backslashes protect the quotes that are within the aliases coding.
    
    alias del="rm" #Users that are accustomed to using DOS will benefit from an alias that mimics DOS.
    
    alias delete="rm" #This alias allows the user to use a easier-to-remember command to remove files.
    
    alias copy="cp" #This alias also allows a user to use a command that is not as abstract.
    
    alias firefix="firefox" #Users that misspell commands often will benefit from this idea.
    
    alias kthxbye="exit" #This command mimics LOLCODE's exit command. Some users may find it helpful to use commands from other languages.
    
    alias zed="sed" #This command helps with those spelling errors.
    
    alias ...="cd ../.." #This shortcut can save users time. The alias is three key-presses while the original requires eight.
    
    alias neobzr="bzr push lp:neobot" #Because I am a regular user of Bazaar and Launchpad, I use an alias to push my changes. Other Bazaar/Launchpad users can use this alias as a template for their projects. GIT, CVS, and Mercurial users can use this idea as inspiration for their version control systems.
    
    alias sendbeta="bzr push lp:novabot-xaiml; bzr push lp:novabot-xaiml" #To push many projects at once, add each push command separated with a semicolon.
    
    alias addppa="sudo add-apt-repository" #This alias can help save time and users that cannot remember the original command
    
    alias bitcoin="bitcoin-qt" #Have problems remembering that last part of the command or find it pointless? Well, make another alias.
    
    alias coffee="coffeescript" #Coffeescript users can save time by only typing "coffee". If you are still too lazy for that, try the next alias.
    
    alias cs="coffeescript"
    
    alias makepro="qmake *.pro" #Developers have many commonly typed commands. These types of commands could use some aliases.
    
    alias editalias="(gedit ~/.bash_aliases &)" #Opening a file manager and changing the view settings to show hidden files and then finding the alias file can all be time-consuming. Guess what? Make another time saving alias. Now, users can quickly have the file open for modifications.
    
    alias install="sudo apt-get install" #The command need to install software is long, so make an alias for this as well to save time.
    
    alias back="cd -" #To go back to the previous folder location, typing "back" will be quick to use and easy for users to remember.
    Hopefully, this article has given you aliases you can use or ideas you can latter develop.

    Attached Files:

    Last edited: Oct 16, 2013
    Haider92 and Rob like this.
  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,213
    Likes Received:
    407
    Trophy Points:
    83
    Very nice. Some of my favorites:
    Code:
    echo " Battery Status"
    alias bat_status='upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep  -E "state|to\ full|percentage|time to empty"'
    Code:
    alias nvidia-settings-optirun='optirun -vvv -b none nvidia-settings -c :8'
    DevynCJohnson and Rob like this.
  3. grim76

    grim76 Active Member Staff Writer

    Messages:
    177
    Likes Received:
    48
    Trophy Points:
    28
    When using aliases keep in mind that not all machines will have the aliases you have setup. When moving from system to system you still may need to either create the alias or know the full command that you need to run.
  4. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,214
    Likes Received:
    1,019
    Trophy Points:
    113
    Thanks for sharing some aliases with us. If anyone else has some favorites of their own, feel free to share.
    Haider92 likes this.
  5. seven

    seven New Member

    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    1
    alias mkfl='echo "" >'
    DevynCJohnson likes this.
  6. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,214
    Likes Received:
    1,019
    Trophy Points:
    113
    Thanks for the useful alias. I never thought of making an alias that makes an empty file so easily.
    Haider92 likes this.
  7. Jatsui

    Jatsui New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Another one I use very often (line in ".cshrc")

    alias c "clear; hostname; whoami; pwd"
  8. lobo

    lobo Active Member

    Messages:
    130
    Likes Received:
    74
    Trophy Points:
    28
    Code:
    $ touch easynewfile
  9. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,214
    Likes Received:
    1,019
    Trophy Points:
    113
    I am too lazy to type that much in a command-line ;). I would rather type four letters "mkfl" than type five.:p
    Haider92 likes this.
  10. lobo

    lobo Active Member

    Messages:
    130
    Likes Received:
    74
    Trophy Points:
    28
    Happy to oblige
    Code:
    alias touc="touch"
    Also, I'm with grim76 on aliases, I'd go as far as to say that aliases can be a bad thing, as you can train yourself into using different commands to everyone else, which are then useless when you're working on a different machine or helping someone else.

    There are valid uses for aliases, but I would say that shell scripts are better, more portable and more useful for achieving the same end. Using as a kind of "auto-correct" for the terminal is a sloppy practice.

    To save on repetitive typing: command history, bash completion and shell scripting.
    Last edited: Jan 7, 2014
    DevynCJohnson likes this.
  11. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,214
    Likes Received:
    1,019
    Trophy Points:
    113
    :Dhahahahaha:D

    I laughed when I saw this. That is perfect.

    ;)I need to make a list of the 26 most commonly used commands and make an alias for each one using one letter on the keyboard.:p

    I would then use the command-line like this -

    Code:
    a | b > ./file
    I would also do uppercase letters for an additional 26, but that would require the "shift" key. ;)

    In reality, I am not lazy. I just like to pretend to be lazy... or am I:rolleyes:
    Haider92 likes this.
  12. mek42

    mek42 Member

    Messages:
    31
    Likes Received:
    15
    Trophy Points:
    8
    I think I may have setup a shell script that let me use md for mkdir at one point before md was a command of it's own.
  13. JohnSerrano

    JohnSerrano New Member

    Messages:
    10
    Likes Received:
    3
    Trophy Points:
    3
    i am getting the help as i was not aware with many of the commands have been mentioned in the sorts of posts here .. so, appreciate the share

Share This Page