The 'alias' function -- aka the greatest thing since sliced bread!


Recommended Posts

Here's another one for the "Cool Tools" bin, folks. :yes: I'm getting into some more advanced Linux/Unix/BSD usage, and I needed a way to simplify things I wanted to do on the commandline.

 

Enter 'alias'. This one is part of every Linux, Unix, Mac, and BSD installation but it's likely that 95% of us who use these operating systems aren't aware of it. Unless you happen to be a Sysadmin or possess Super Cow powers.

 

What it does is lets one make shortcuts for the commandline! Woohoo! Just what we need. (Well, it's just what *I* needed!)

 

So instead of something with a ton of commandline options that we need to type in every single time, we alias the command into something more speedy. Sounds great? Interesting? I thought so too, and set to researching its' powers.

 

'alias' is very simple.

alias: usage: alias [-p] [name[=value] ... ]

if a user were to type 'alias' by itself, they'll see a list of all commands that have been aliased already. I'll provide a list of mine that I've already added to demonstrate:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias clean='sudo apt-get autoremove && sudo apt-get autoclean'
alias df='dfc -TasowW'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --colour=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias update='sudo apt-get update && sudo apt-get dist-upgrade'

Most of these are default, that came standard with my distributions' installation. Three of these are not; namely 'update', 'clean' and 'df'.

 

Breaking down the aliases, we can see the alias name, and the command that we've aliased. As you can see, we can do anything with an alias that we can do with a command prompt.

 

I've installed a really spiffy program known as DFC. It provides the user with a readout of all mounted volumes on the system. In its' stock form (typing dfc at the commandline), it looks like this:

 

before-alias.thumb.png.8fd1672fa6116d4d9

 

It's ... ok. Not great, but ok. Let's change that by using 'dfc -TasowW':

 

after-alias.thumb.png.2bbf7048b813ec011e

 

Much better. :yes:

 

Now we want to have it look like that every time. Alias it!

 

alias df="dfc -TasowW"

Now, when we type 'df' into our Terminal, it'll look as we wish it.

 

Some commands that have been aliased don't always remain intact between Terminal windows. You'll need to add those manually, yourself. The "approved and suggested way" didn't work for me (adding new .bashrc and .bashrc_aliases files in your /home directory), so I did it the "not approved and definitely not-suggested way" by adding the commands into /etc/bash.bashrc. Since I'm the only user on my machine, it's fine.

 

Happy aliasing! :D

  • Like 2
Link to comment
Share on other sites

To add to that, sometimes it sucks to find some basic commands aliased (eg. the "rm -i" on redhat).

Rather than de-aliasing it, you can just run it with a backslash to call the original command instead of the alias:

 

\rm somefile.txt

  • Like 1
Link to comment
Share on other sites

2 hours ago, Mulrian said:

What color scheme are you using for your terminal? Started to dislike mine recently and looking for something new.

It's the default scheme that comes with Mint. I haven't changed it aside from the font, which is Liberation Mono 9pt. Window borders are Numix.

 

1 hour ago, ichi said:

To add to that, sometimes it sucks to find some basic commands aliased (eg. the "rm -i" on redhat).

Rather than de-aliasing it, you can just run it with a backslash to call the original command instead of the alias:

 

\rm somefile.txt

I thought rm was 'remove'. Heh. :p 

Link to comment
Share on other sites

It is, but on redhat when executing rm what you are actually executing is the rm alias, not the rm command, so what you get is an "rm -I" which prompts for confirmation for every file.

 

Using a backslash before a command which has the same name as an alias executes the command instead of the alias.

  • Like 3
Link to comment
Share on other sites

20 hours ago, Haggis said:

you can also add stuff into the .bashrc file ;)

Indeed!

alias is a cool thing to set up lots of things, like a quick backup for a folder:

alias backupdocuments="rsync -arv --delete --progress /home/yourname/Documents/ /media/nameofmedia/yourname/Documents/"

or to start a game:

alias doom="chocolate-doom -iwad /gamelocation/DOOM.WAD" (assuming you've got chocolate-doom installed and have the DOOM.WAD and want to play that game0

  • Like 1
Link to comment
Share on other sites

I've always been a fan of alias. It's a great way of simplifying complex and multi-part operations into a single command. Some great info here, thanks BetaGuyGZT. Perhaps this should be stickied and turned into a tips and tricks thread.

  • Like 1
Link to comment
Share on other sites

Thanks for the support, folks. I love alias. I'm going to echo (teehee!) @simplezz's vote for a dedicated tips and tricks sticky thread here in the Linux and Unix Support Forum. :yes:

 

It'll be a beautiful thing.

 

I'm happy to be of service! Please post any useful aliases here in the alias thread! I'm quickly becoming an alias junkie; and as such, I'm considering the development of a little application that will allow a user to create aliases, delete them, etc. I haven't really thought it through, though. (That's a lot of T's, isn't it?).

 

:D 

Link to comment
Share on other sites

As Haggis says, you can add things to your .bashrc file and, once you restart your session, they stay in memory. I quite like:

 

alias update='sudo apt-get update && sudo apt-get upgrade'

 

As it means that updating is a bit more straightforward.

  • Like 2
Link to comment
Share on other sites

8 hours ago, BetaguyGZT said:

Thanks for the support, folks. I love alias. I'm going to echo (teehee!) @simplezz's vote for a dedicated tips and tricks sticky thread here in the Linux and Unix Support Forum. :yes:

 

It'll be a beautiful thing.

 

I'm happy to be of service! Please post any useful aliases here in the alias thread! I'm quickly becoming an alias junkie; and as such, I'm considering the development of a little application that will allow a user to create aliases, delete them, etc. I haven't really thought it through, though. (That's a lot of T's, isn't it?).

 

:D 

Alias isn't a *nix only thing though.

Link to comment
Share on other sites

8 minutes ago, HawkMan said:

also BSD is BSD not unix or linux ;p

Ahh. I thought BSD was Unix. I stand corrected. :)

 

11 minutes ago, HawkMan said:

Windows Powershell uses it (I believe it's very bash based )

Interesting! I wonder how bash-based it is. I know people could at one time install SFU (Services for Unix) on Windows, but that was a while ago.

Link to comment
Share on other sites

17 hours ago, BetaguyGZT said:

Interesting! I wonder how bash-based it is. I know people could at one time install SFU (Services for Unix) on Windows, but that was a while ago.

You can use Bash under WIndows but PowerShell is its own thing that just happens to have aliases as well.  Which is good as it's stupidly easy to use English-like syntax can get pretty tedious fast, there's very large number set out of the box, including a bunch that are more or less identical to their *Nix equivalents.

Link to comment
Share on other sites

This topic is now closed to further replies.