- Using alias Command in Linux to Improve Your Efficiency
- Linux alias command
- How to create alias in Linux
- Make alias permanent
- Creating global aliases for all users
- How to see all the alias set on your Linux system for you
- How to remove alias
- My favorite alias in Linux I cannot live without
- 1. Making ‘rm’ safer
- 2. Making ssh to other systems easier and faster
- 3. Show time or date for day to day things
- 4. Easily manage your iptables information
- 5. Update Debian based servers in one single command
- 6. Add an alias to run alias with sudo
- How do I permanently remove an Alias from my Shell
- 3 Answers 3
- How do I remove an alias?
- 3 Answers 3
Using alias Command in Linux to Improve Your Efficiency
Alias command in Linux saves a lot of your time and improves your efficiency. Learn how to use this command properly and see some of the common aliases I use.
As it is known, Linux and Unix were created as command line based systems, which means having control of the commands typed is basically the most important thing to be a *nix SysOp.
This is where ‘alias’ command comes to the show and can be extremely useful, especially for security purposes and to simplify work for a system administrator.
Linux alias command
The shell alias is simply a way to reference another command. It can be used to avoid repetitive long typing of commands and shell lines and simplify work or to even make things safer or dumb-proof.
Take a simple example where you have to use ls command to show everything in reverse chronological order. You can type ls -lrta all the time or you create an alias (say) ‘ll’ that will be equivalent to ls -lrta. It will save you a few keystrokes.
In a similar fashion, you can substitute long and complicated commands with small and simple commands by using alias in Linux.
Let’s see how to make alias in Linux.
How to create alias in Linux
I’ll continue with the same example I used above.
You must note a few things:
- The substituted command is always used under the inverted commas (‘).
- There must be no spaces before or after the equals sign in the alias command. If you mistakenly put a space, you’ll see an error like alias not found.
- Be careful in choosing the name of the alias. There are no reserved keywords so you may replace an existing command with a totally irrelevant command.
You can check if a certain command is actually an alias with the type command. For example, in Ubuntu, ls is actually an alias to show you colorful output.
type ls ls is aliased to `ls --color=auto'
If you want to use the original command, without its aliased version, use single quotes around it.
Now, the alias you just is temporary. If you exit the shell, you’ll lose the alias. You need to make the alias permanent.
Make alias permanent
In order to add aliases that can work in a system all the time, you need to simply edit your user’s (or any other user’s) .bashrc file. Some distributions might suggest using a dedicated .bash_aliases file for storing aliases.
This file is located in your /home// folder. You can use vim command to edit the file in terminal.
This will immediately open your .bashrc file and you must start adding aliases by the end of the file, just after the last written line of it. You should then source the bashrc file.
I suggest creating either blocks or at least adding commentaries in the file for each of the aliases created so it’s easier in the future to know what is each thing for maintenance purposes.
Creating global aliases for all users
If you want the aliases to be available for all users on your Linux system, you should add the aliases in /etc/bash.bashrc file.
If you don’t have this file, create it.
How to see all the alias set on your Linux system for you
If you want to see all the alias set on the system for you, you can check the configuration file of your shell like ~/.bashrc, ~/.zshrc etc.
However, a much easier way to see all the alias would be to simply run the alias command without any arguments.
A typically Ubuntu system has the following alias set by default.
alias alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '''s/^s9+s//;s/[;&|]s*alert$//''')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -lrt' alias ls='ls --color=auto'
How to remove alias
If you want to remove an alias, you can use the unalias command in this manner:
You can remove all alias at once using the -a option:
As with the alias command, changes by unalias command are also temporary. If you wan to remove an alias permanently, you should delete it from the bashrc file.
My favorite alias in Linux I cannot live without
Let me share some aliases that I always use to work more effectively in Linux.
1. Making ‘rm’ safer
As it is well known, rm command is very commonly used day by day and can be also very destructive and disruptive of a system if not properly used.
This is why, I use the following alias:
#make rm command safer alias rm="rm -i"
This makes ‘rm’ command safer as whenever you use it, it will always ask if you are sure you want to perform the infamous removal operation before proceeding, giving you a second and last chance to not break things unintentionally.
2. Making ssh to other systems easier and faster
Sometimes, especially if you use private keys instead of credentials to log in to other Linux systems, it can be easier to set up aliases for each of them with mnemonic names.
As a SysOp, I use SSH for logging in to different Linux servers and I have several private keys, depending on my clients, therefore it helps me to have aliases such as:
#alias for servers SSH alias sshserver1='ssh -i ~/Documents/IT/SSH-keys/server1.pem [email protected]' alias sshserver2='ssh -i ~/Documents/SSH-keys/server2.pem [email protected]' alias sshplexserverhome='ssh [email protected]' alias sshclientserver='ssh -i ~/Documents/IT/SSH-keys/client.pem [email protected]'
As you can see, I have all my private keys in a folder named “SSH-keys” inside my own user’s folder and then I simply create aliases to connect each of them, following a standard of “ssh”+”name of the server”.
This way I only have to remember the name of the server I want to connect and not where the key is located, the name of each key (if they are different) or even the username that needs to be used for each server to connect.
3. Show time or date for day to day things
Some people either because they script things that use specific times or need to consult time and/or date and want to have in specific formats, can get some benefit of aliases by doing:
alias nowtime='date +"%T"' #this will show the current time in 24hrs format as HH:MM:SS alias nowdate='date +"%d-%m-%Y"' #this will show the current date in format dd-MM-YY
4. Easily manage your iptables information
Let’s face it: iptables or nftables are not the easiest nor friendly thing in the world and even though it’s not hard, the first time you deal with it is not that easy. This is why using aliases such these ones can make things a lot easier:
#Displaying iptables information the easy way :) alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers' #this will display all lines of your current iptables alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers' #this will display all your INCOMING rules in iptables alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers' #this will display all your OUTGOING rules in iptables
5. Update Debian based servers in one single command
I use Ubuntu myself, but you can adapt this for any Red Hat, CentOS servers and ‘yum’ command. Updating a Debian server in a single command could be done through an alias such as this one:
# update on one command alias update='sudo apt-get update && sudo apt-get upgrade'
6. Add an alias to run alias with sudo
You cannot run an alias with sudo by default. If you want to run an aliased command with root access, you’ll have to create an alias with sudo like this:
As you can see, it all depends on what you do and your creativity, but you can basically take the time to create as many aliases as you need for your day to day tasks and make them easier by using this useful ‘alias’ command in Linux.
How do I permanently remove an Alias from my Shell
When I was not near my computer some guy got it and set an Alias for ls in my root folder. He set it to ‘yes NeverGonnaGiveYouUp’ . So now when im in my root folder and type ls I get an infinite loop of NeverGonnaGiveYouUp. It’s driving me nuts and I don’t know how to get rid of it. I’ve already tried unalias and unalias -a but those just remove it temporarily. Once I close the shell and reopen it it comes back. How do I get rid of this crap?
Whats the output of grep -n ‘NeverGonnaGiveYouUp’ ~/.profile ~/.bashrc /etc/bash.bashrc /etc/profile ? There are some other files to look at too depending on your shell and distro..but this should give us a start..
3 Answers 3
If unalias removes the issue (even temporarily) we have confirmation it is an alias. It could be «brute forced» out by adding an unalias ls in ~/.bashrc.
That will get excuted every time bashrc is read and will remove the alias.
That will buy you some peace but will not resolve the actual issue that some file is still containing code to re-start the alias. You need to find which file contains the problem.
grep "NeverGonnaGiveYouUp" /etc/profile /etc/bash.bashrc \ ~/.bashrc ~/.bash_profile ~/.profile \ /root/.bashrc /root/.bash_profile /root/.profile
That’s a good list of possible files that got the definition. If nothing shows up in that search, or you use some other shell, let us know to further help.
How do I remove an alias?
I want to remove gs alias from my PC. When I type gs it will open GhostScript. But I checked everywhere in the home directory .alias .bash_aliases .bashrc I also overwrite the gs with my custom alias. I can’t remove it. And I also type alias in terminal, in the list I couldn’t find it. Please I want to remove it.
gs isn’t an alias, it’s the name of the GhostScript binary. If you want gs to do nothing then you would have to uninstall ghostscript from your system although this probably isn’t a good idea as a lot of other software depends on it.
You can use type gs to check if the command is an executable file, an alias, a bash function or a bash builtin.
3 Answers 3
The command to remove an alias is unalias so.
unalias — remove alias definitions
unalias alias-name. unalias -a
DESCRIPTION
The unalias utility shall remove the definition for each alias name specified. See Alias Substitution . The aliases shall be removed from the current shell execution environment; see Shell Execution Environment .
-a Removes All aliases
caution: unalias -a will remove all alias definitions from the current shell execution environment. , possibly including other innocent ones.
unalias removes an alias or aliases from the current shell, but if they are ‘stored’ in for example ~/.bashrc , they will come back when you create a new shell (or terminal window). So you should remove the definition of the aliases from where they are stored in order to get rid of them, unless they are temporary aliases.
TL;DR: The command to remove a shell alias is unalias . See the unalias manual. Run:
But if your gs command is not an alias (most likely), it will not help. The response will be something like:
To find the type of the command trouble you’re in, use type built-in shell command:
On my system it says it is an executable:
Compare that with a common alias ll :
See help type for detailed description of the type shell built-in command
You can also check if it is a symbolic link with:
-rwxr-xr-x 1 root root 14520 Aug 24 17:03 /usr/bin/gs
Otherwise there would be an arrow like this:
-rwxr-xr-x 1 root root 14520 Aug 24 17:03 /usr/bin/gs -> /some/other/file
As pointed out by others, removing the alias from the current shell session is temporary. For permanent removal, you will need to find where it is defined, such as alias gs=xyz , and remove it there, or add unalias gs to your ~/.profile or ~/.bashrc file.
If it is not an alias, you can locate the package that installed the command and uninstall it. On Debian/Ubuntu for instance:
sudo apt-get remove ghostscript