ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Linux: Aliases

    IT Discussion
    linux sam linux administration alias
    7
    17
    4.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by scottalanmiller

      In Linux it is common to use the alias function in order to modify system behaviour in handy ways. Most Linux distros today ship with a stock list of alias modifications to make their systems easier, safer or just unique. The alias command is used to both display existing aliases and to create or modify our own aliases.

      An alias in Linux is just what it sounds like, an alternative way to refer to something. Most commonly an alias might be used to reduce a common, complex command into something very short and simple or to make a common typo correct.

      For example, it is not uncommon for a full time Windows Admin who only needs Linux sometimes to alias dir to ls so that they can accidentally type dir and get what they expected.

      If we just use the alias command with no options, it will display the aliases currently set on our system. Let's see what are the default aliases on a stock CentOS 7 installation:

      # alias
      alias egrep='egrep --color=auto'
      alias fgrep='fgrep --color=auto'
      alias grep='grep --color=auto'
      alias l.='ls -d .* --color=auto'
      alias ll='ls -l --color=auto'
      alias ls='ls --color=auto'
      alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
      

      As you can see, there are seven default aliases included in CentOS 7. Most of these are pretty basic, just modifying the default egrep, fgrep, grep and ls commands to have full colour by default. Nothing too exciting.

      We can make our own aliases for whatever we want. Typically this is not recommended, but it is a facility that is available and should be understood by system administrators as it would be easy to hide or override functionality using aliases and if they are not understood it can lead to confusion.

      Why We Do Not Recommend Aliases for Most System Administration Functions This same reasoning will come up many times as a good general practice for SAs. By and large we do not want to customize our systems. System customizations are important for end uses who need to be efficient at repetitive tasks and daily duties. End users can often take time to set up their environments, tweak settings and such to make their environments work as they want them to work. As System Admins, we rarely have this luxury. What is most important is that we cannot guarantee that we will have these tweaks and settings when things matter most - when something is broken. Whether it is being used to having a "safety" feature to catch us from mistakes or a quick reference to a file we use, it is generally better that we learn to do things the standard way because when it is most important, that may be the only option that we have.

      We will make and remove a simple alias here to learn out it works. Our alias will take us directly to the /tmp directory.

      # alias go2t="cd /tmp"
      

      Not very practical, but it shows how an alias works. Now we can test it.

      # go2t
      # pwd
      /tmp
      

      That was easy. But we do not want to keep that hanging around. We can remove an alias by setting it to null like so...

      # unalias go2t
      

      And the alias is gone. Run the alias command again and you will see that our go2t alias is not in the list, it has been erased.

      alex.olynykA 1 Reply Last reply Reply Quote 8
      • alex.olynykA
        alex.olynyk @scottalanmiller
        last edited by

        @scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.

        scottalanmillerS stacksofplatesS 2 Replies Last reply Reply Quote 0
        • scottalanmillerS
          scottalanmiller @alex.olynyk
          last edited by scottalanmiller

          @alex.olynyk said:

          @scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.

          # alias cls="clear"
          

          Then add it into your .bashrc file for just you or into the /etc/profile for everyone on a system.

          1 Reply Last reply Reply Quote 0
          • scottalanmillerS
            scottalanmiller
            last edited by

            Or your .profile if you want it across all of your shells.

            1 Reply Last reply Reply Quote 0
            • aaron-closed accountA
              aaron-closed account Banned
              last edited by

              This post is deleted!
              scottalanmillerS 1 Reply Last reply Reply Quote 1
              • scottalanmillerS
                scottalanmiller @aaron-closed account
                last edited by

                @aaron said:

                @scottalanmiller I am enjoying these write-ups and would like to share them with some of my junior staff. Is there an index page of sorts ?

                There are ways to show them as an index. One is the official "table of contents" which presents the entire series "in order":

                http://mangolassi.it/topic/7825/sam-learning-linux-system-administration

                And then there is the "SAM Linux Administration" tag which shows all of the articles tagged as part of the series which will show them more in "order they were written":

                http://mangolassi.it/tags/sam linux administration

                1 Reply Last reply Reply Quote 1
                • scottalanmillerS
                  scottalanmiller
                  last edited by

                  My plan is to get a small "banner" that goes across the top of each and points back to the main page to make navigation much easier and clearer. Just need to get a banner made, though 🙂

                  1 Reply Last reply Reply Quote 3
                  • stacksofplatesS
                    stacksofplates @alex.olynyk
                    last edited by

                    @alex.olynyk said:

                    @scottalanmiller How do make a permanent alias? For example I want to shorten clear to cls.

                    You can just use ctrl+l then you don't even need the alias.

                    1 Reply Last reply Reply Quote 0
                    • DustinB3403D
                      DustinB3403
                      last edited by

                      In order for me to properly remove the sample "go2t" alias I had to use

                      unalias go2t
                      alias
                      alias ls='ls --color=auto'
                      

                      Prior to unalias I had

                       alias 
                       alias go2t=''
                       alias ls='ls --color=auto'
                      
                      scottalanmillerS 1 Reply Last reply Reply Quote 0
                      • scottalanmillerS
                        scottalanmiller @DustinB3403
                        last edited by

                        @DustinB3403 Thanks. OP has been fixed.

                        1 Reply Last reply Reply Quote 0
                        • tonyshowoffT
                          tonyshowoff
                          last edited by

                          Instead of just using aliases, set your home directory in your PATH before /usr/bin/local and create replacement programs to irritate anyone using your user account... that's what I do for fun on my personal machine.

                          travisdh1T 1 Reply Last reply Reply Quote 2
                          • travisdh1T
                            travisdh1 @tonyshowoff
                            last edited by

                            @tonyshowoff said in Linux: Aliases:

                            Instead of just using aliases, set your home directory in your PATH before /usr/bin/local and create replacement programs to irritate anyone using your user account... that's what I do for fun on my personal machine.

                            If it's just your local machine..... why not do that system wide? (Apparently I'm feeling especially evil tonight.)

                            1 Reply Last reply Reply Quote 2
                            • DustinB3403D
                              DustinB3403
                              last edited by

                              You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!

                              stacksofplatesS 1 Reply Last reply Reply Quote 0
                              • stacksofplatesS
                                stacksofplates @DustinB3403
                                last edited by

                                @DustinB3403 said in Linux: Aliases:

                                You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!

                                You can just do ctrl+l

                                DustinB3403D 1 Reply Last reply Reply Quote 0
                                • DustinB3403D
                                  DustinB3403 @stacksofplates
                                  last edited by

                                  @johnhooks said in Linux: Aliases:

                                  @DustinB3403 said in Linux: Aliases:

                                  You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!

                                  You can just do ctrl+l

                                  And have to remember yet another keyboard shortcut?!

                                  travisdh1T tonyshowoffT 2 Replies Last reply Reply Quote 0
                                  • travisdh1T
                                    travisdh1 @DustinB3403
                                    last edited by

                                    @DustinB3403 said in Linux: Aliases:

                                    @johnhooks said in Linux: Aliases:

                                    @DustinB3403 said in Linux: Aliases:

                                    You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!

                                    You can just do ctrl+l

                                    And have to remember yet another keyboard shortcut?!

                                    SOME of us had nothing BUT keyboard shortcuts to work with at one point 😉 Seriously, real, physical RS-232 terminals with 64 keys were no fun! I can't even find pictures of the one I was stuck with, it was numbers, letters, and a ctrl key.

                                    1 Reply Last reply Reply Quote 0
                                    • tonyshowoffT
                                      tonyshowoff @DustinB3403
                                      last edited by

                                      @DustinB3403 said in Linux: Aliases:

                                      @johnhooks said in Linux: Aliases:

                                      @DustinB3403 said in Linux: Aliases:

                                      You know just because I hate having to type "clear" every time... every linux system I have is getting the "cls" alias!

                                      You can just do ctrl+l

                                      And have to remember yet another keyboard shortcut?!

                                      ctrl + l (L) is pretty universal for clear screen in the POSIX-compliant world, it's really about learning standard short cuts, like ctrl + k is delete line, ctrl + c is SIGINT, hell the latter even works in MSDOS and PowerShell.

                                      1 Reply Last reply Reply Quote 1
                                      • 1 / 1
                                      • First post
                                        Last post