The Alias command in GNU/Linux and helpful tips with it
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.
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.
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!
alias rm=”rm -i”
Makes a better example for the uninitiated, perhaps.
Nice article, more like this would be appreciated.
macros i guess–
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…. ;)
@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?
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”
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
“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.
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
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.
Thanks to all the patreons!!!