Instructor: Aliases, what are those aliases?
Well, in real life alias is like a short name,
or easier name, or something easier to remember.
So I'm sure everybody has like a nickname
when you were growing up.
And just like other people who are very famous,
just look at 50 Cents.
He has his real name is Curtis James.
And there's another one,
like another example would be Pitbull.
His real name is Armando Perez.
So those are the real names,
but their alias is shorter.
So the shorter version and the shorter name they have
is because it's easier for people to remember them.
So it's the same way aliases work in Linux too.
So there are very long commands
that you run almost every day,
but you need a shorter version of it.
And how do you create a shorter version
or a shorter word for it is by creating aliases.
So again, alias is a very popular command
that is used to cut down on lengthy and repetitive commands.
And some of those examples that I have listed here,
we will cover those example one by one in our Linux machine,
so this way you'll exactly know how aliases work.
And we'll go into
our Oracle virtual machine console terminal
and we will try these aliases command one by one.
So the first command I have in aliases is ls.
So oftentimes if you want to know
all the files with -l and a option to listing
or a for to also show the hidden files,
we will run the command ls -la or al,
and you'll see all the listing of the files
and directories we have in our home directory.
And this is our home directory.
Simon will run that command,
oftentimes we'll forget the option
so what we'll do, we'll create an alias,
so we'll create alias,
and instead of just ls,
I'll create l=
"ls -al"
and just hit Enter.
Once you hit Enter
and you don't get an error message back,
it means the alias is created.
Now how do you verify if it's created?
By simply running an alias.
Like l, l was our alias,
and let's run it.
And there you go,
we got the output as if you run the whole command.
Let's create another alias.
The other alias, what it will do,
it will do the pwd for you
and also do ls for you.
So two commands in one shot
that you don't have to run twice.
So we'll do pl.
pl, I picked the word
just for print working directory and ls
="pwd,
and you separate each command with semicolon
and ls".
You hit Enter.
And now when I'm gonna type pl,
I should get pwd and ls command running.
So first let's clear the screen
and I'm gonna write pl.
And there you go.
So the first command it ran was pwd,
next command, ran ls.
So every time I log into a system,
I'll just do pl
and it will run those two commands for me.
Let's keep going on
and let's try our third one, which has three commands:
whoami, hostname, and pwd.
So I want you to create that alias on your own.
I'm gonna go onto the next one.
Next one is, it will list all the directories
inside of my home directory.
I run the command ls -l
and I grab it.
And the cap right here that you see,
which is on top of number six,
that's it's telling the grep everything
that starts with d.
So I'll run that
and you'll see it grep everything in the output
that starts with d.
So, of course, anything that starts with d is what?
Is a directory,
so that's what it's getting.
So I'm gonna create an alias for it
and I'm gonna call it dir=
"ls -l,
and I'm gonna grep it, the d,
Enter, clear the screen,
and let's see if it's working by typing dir.
And yes, it is working.
Then I'm gonna try the last command in my list.
So what last command is doing...
Sorry, I just have to move this a little more
and resize it so you'll see the last command.
So what last command is doing
is, of course, it's doing df -h.
Let's run df -h,
which will output your petition's information,
the size, where is it mounted, and how much is used.
And the next command it's doing,
df -h is printing.
It's awking the last column I believe.
Yes, $6, close.
And you'll see it is showing you,
let me make this smaller too.
So it's listing everything that is in the last column,
which is a column 6.
Now I want to get in that output,
I want to get only the first four characters of this output.
So I'm gonna run the command again
and I'm gonna do cut -c1-4.
Hit Enter, and there we go.
This is what it's telling me that the last column,
these are the petitions
that I have in my Linux distribution.
Sometimes you do want to run this command
just to find out what petitions you have.
You don't wanna look at the other information
that df -h gives you,
so I'm gonna create an alias for that.
So I'll just hit up arrow key of course
to bring the command back.
I'll go back all the way to the beginning of the line
by hitting the left arrow key.
I'll type alias,
and I'm gonna name this alias as just d=.
Of course, I have to put quotation here,
double quote.
And there is one little thing you have to change here
is you see here, $ sign,
in Linux $ sign has what meaning
if you guys remember?
$ sign actually outputs a variable,
whatever that you have variable inside
that you attach in the scripting,
it actually puts it in there.
So it will think that it's a variable too.
So we don't wanna tell it its variable,
so how we could change that
or how we could tell Linux
not to think it is as a alias,
sorry, as a variable,
so I'm put \.
After putting the \,
move all the way to the end
and hit quotation close,
hit Enter, and it created my alias.
Now if I do d again, let's clear the screen.
I'm gonna do a d again,
and there you go,
it's doing exactly the same thing we wanted to do,
except with all that long tedious command,
you don't have to run it all the time.
So it's much easier every time you log into the system,
you type d and you'll get your information.
Isn't that cool?
It is very cool
because that's why most of the time people use aliases
so it will be easier to remember
and do the tedious work in a very short amount of time.
Now, where are those aliases created?
Once you type aliases without any running,
creating any new commands,
just type aliases, hit Enter.
You'll see right here,
it will give you all the aliases that I have created.
You know, it's right here.
This is one, these are the two,
another one right here.
And then there are another one right here.
And the last one, no, this was.
Other ones that are already created,
it's already there by the system.
By default, it's already there for us,
so I don't have to touch it.
Now what if I wanted to remove that alias
from the list of alias that I've created?
Simply there's a command to do it,
it's unalias
and you type the alias name.
So if you notice the first alias we have,
we have alias name just d,
so I'm gonna just type d,
it unaliased.
Let's see if it still exists.
You just type alias
and see if the alias exists there or not.
It does not, it's gone.
There's another alias, dir,
I'm gonna unalias as well, dir.
And then I'm gonna try to check if it's there.
Nope, it's gone.
So it's just that simple.
It's masking something,
it's telling, it's creating a shortcut key
for you to run in Linux environment.