Linux Essentials - Bash Aliases

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello again everyone and welcome back to learn linux tv in today's episode of linux essentials we're going to take a look at aliases aliases are awesome they essentially allow you to well create your own linux commands you could call an alias whatever you'd like and have that alias execute a specific command which means you can get a command that's like this big and just scrunch it down into a bite-size alias it's just an awesome thing to do also in addition to teaching you guys how aliases work i'm also going to give you some of my favorite aliases that i use personally so let's go ahead and dive right in so how exactly do you create an alias what i'm going to do right now is give you a simple example of creating an alias and then i'll show you my favorites the alias command as you see here is the command that we'll use to create an alias and the way that the syntax works out is we first give it a name basically what we want to call our alias i'm just going to call mine my command abbreviated we're going to set that equal to something there's no space here and inside the double quotes we type another command so as a quick example i'll type ls-lh so what exactly did that do for us well from now on if i type my cmd just like that that's what i call the alias it actually executed ls-lh now we could probably argue that typing my cmd isn't all that much shorter than simply typing ls-lh but this is just an example to create an alias you give it a name and then you give it the command that you want executed anytime you type that alias now one thing we could do is actually make this a lot shorter and actually somewhat useful by just lowering it down to one letter because again you can name your alias whatever you want even if you only name it one character so now that i have an alias that is just the letter l if i type l and then press enter then that will execute ls-lh and with this variation we have our first alias that's actually dare i say it somewhat useful and typing one character is a lot easier than typing out that entire command so we just saw a few examples of creating an alias so how exactly do we list the aliases that we have on the system for that we simply type alias but we won't include any options or anything like that we'll just type alias by itself and as you can see here i have well several aliases i've only created two i created this one right here and i also created this one now the reason why i have so many aliases here is actually because various distributions will give you some aliases by default so when you run the alias command on your end your output will probably look a lot different than mine the output will include all the aliases that the distribution has given you along with all the ones that you've created yourself okay so we know how to create an alias and you've also just seen how to list all the aliases that you have on the system how do you remove an alias well that's actually easy we have the on alias command for that now earlier we've created an alias that was down to just one character l that's the one that executes ls-lh if i press enter on this it's going to actually delete that alias so if i run alias again with no arguments we can see that the alias of just l is no longer on the list in the previous output the l alias was actually right above this one right here and now it's gone so now you know that the on alias command enables you to remove an alias now how about we check out some of my favorite uses for aliases now this isn't an exhaustive list or anything i have quite a few but what i've decided to do was just narrow this down to my favorites i do want to give credit to whoever out there originally created some of these aliases i'm not going to take credit for all of these myself some of these i found quite a few years ago when i was learning i don't remember where i got these from originally so i just want to call out the fact that these are not mine these are aliases that i've picked up over the course of my career over the course of years anyway let's check out the next alias and what we're going to do with the next alias is simplify the output of df-h this is what it looks like right now as you probably already know from previous videos in this series and other videos on this channel the df command or disk free command what that allows you to do is see how much storage you have available the dash h option gives you human readable output essentially that means it'll show the output in megabytes and gigabytes and on my end there's nothing really truly wrong with this output i can see the root file system right here i'm only using four percent of it so i don't have that many lines of output now some of you guys out there you might have pages of output when you run df-h so how do you narrow this down to just the output that you care about so let's set up the alias i'm going to name the alias df and as you know that's already a command on the system if i create an alias that has the same name as an actual command then the alias will override the original command so what i'm going to do is set that equal to df dash h dash x i want to exclude squash f s if it's present another dash x i want to exclude temp fs x again and this time i want to exclude dev temp fs i'll press enter and there we have the alias i execute it you can see that we have fewer lines of output here effectively the x option allows you to exclude and i've excluded some things that i didn't care about here so i have only the output that i do care about so now the output of the df command is a lot simpler in fact i don't even have to type df-h because dash h is part of the alias so i've simplified that down to just df but when i type df it's going to instead execute this as you'll see if i just copy and paste this command right here the output is going to be exactly the same and it would be the same even if i didn't create the alias so this actually served multiple purposes this entire command was simplified down to just df and it also excludes file systems that i don't want to see information for which allows me to get to the information that i care about a lot easier now for the next alias i'm going to show you what it looks like before and after but to do that i'm going to need to shrink the font size a bit and the reason for that is because the mount command actually produces quite a bit of output as you can see here it's just all over the place as i'm sure you remember from a previous episode in this series the mount command allows you to mount additional file systems to your linux system but if you execute the mount command by itself it just gives you a list of everything that's mounted but i want to clean this up a little bit because it's just all jumbled together and honestly kind of hard to read so let's set up another alias i'm going to call this one ls mount i'm going to set that equal to mount and i'm going to pipe that output into another command and the command that i'm going to pipe it into is column dash t just like that so if i enter ls mount which is the name of the alias you can see that everything is a lot cleaner basically what it did was it forced the output of the mount command to be separated into columns and for me that makes it a lot easier to read there's still a lot of information here but since it's in columns i think it's just easier to digest so let's go ahead and try another fun alias that i like a lot i'm going to call it extip and i'm going to set that equal to curl then i can has and it's misspelled on purpose that's the name of the website icanhazip.com just like that you will need curl installed in order for this to work so if you don't have curl installed you'll need to install it with apt dnf or whatever your package manager is so let's see what happens and there it is that's actually my external ip address and in my case it actually shows me the correct ip you are not able to see it on your end but just take my word for it this is actually the correct ip address so as you can see that's a very useful alias to have so at this point what i would like to do is simplify the process of installing packages and to do that i will yet again set up another alias i'm going to name this one install and this is going to vary from one distribution to another but i'm running on ubuntu in my case so what i'm going to do is set this alias equal to sudo apt install so normally what i would do to install a package is i would run sudo apt install and then the name of the package so maybe i want to install tmux or something like that but with this particular alias i can simplify it down to just install and then the package name so there we go it's installing tmux and in my opinion that's a lot easier to type than sudo apt install however i can make it even easier by simplifying this alias down to just i and that allows me to type i and then the package name so tmux for example it's already installed and that's a lot easier than typing out sudo apt install or even just install i have a simple alias of i for installing packages on your end if you're not running debian or ubuntu then maybe you would change that to dnf install yum install pacman dash capital s whatever your package manager is but you get the idea this allowed me to simplify the command all the way down to one character similarly i can set up another alias for upgrading packages as well and that's a longer command so first of all with debian and ubuntu we generally like to run sudo apt update first because we want to update the package sources and repository indexes and then we type a double ampersand which means that if this command is successful we want it to run the second command right after so by typing upgrade it's going to first run sudo apt update and then sudo app disk upgrade let's go ahead and see it in action i'll simply type upgrade i'll press enter and in my case i've already installed all the updates on this computer so i don't have any updates to install but if i did then i could run this one word command of upgrade to make sure that all available patches and upgrades are installed on this machine for this next alias you'll need to have python installed on your system so you can type which and then python to see if it's installed and if you don't get any output then it might actually be called python3 in your distribution it varies what each distribution calls their instance of python python 3 is generally the one you want some distros actually have python 3 under python in my case it's user bin python 3. so for this one you do have to have python installed like i mentioned i always recommend python 3 but it doesn't really matter you just want to make sure that you have it installed so i went ahead and pasted in the alias right here because it's just a longer one and i didn't want to bore you guys with watching me type all of this but effectively what i'm doing is i'm creating an alias called speed test and i'm setting it equal to this and what this is doing specifically is it's using curl to pull down a python script from github and it's going to redirect that right into python 3. now that could be just python in your case like i mentioned it differs from one distribution to another but i'll press enter to save the alias let's go ahead and run it and as you can see it went ahead and did a speed test now in my case this isn't actually an accurate speed test i've set up throttling on my network to make sure no one device can use too much bandwidth so i'll never have the full speed show up in the speed test and your results might be lower as well if you have some sort of bottleneck in your network but as long as you don't have a bottleneck you should be able to get a fairly accurate speed test with this alias how cool is that so now what i'm going to do is show you guys two aliases that are closely related so i pasted in the first one right here i'm calling it mem5 i'm not going to explain everything that it's doing right here you can check the man pages if you really want to get the finer details but what this should do if it works is give you the top five processes that are using the most memory and here i've pasted another alias this one is very similar to the previous one but the main difference is that it's going to show you the top five cpu hungry processes while the previous one shows you the top five memory hungry processes so now if i type mem5 i get the top five processes that are using the most memory and of course i could do the same thing when it comes to the cpu usage by typing cpu five and now we can see the top five processes that are using the most cpu and you could go ahead and play around with this for example you could change this to cpu 10 and then change the number here to 10 and that'll give you the top 10 processes that are using the most cpu in fact let's go ahead and try it and as you can see i get quite a bit more output now one downside is that all of these aliases are specific to this particular shell so for example if i open up a new tab here and i'll type cpu5 that's an alias that we've created the command is not found but it does work you've seen me use it before and i'll do it again right now and it works in this tab and the reason for that like i mentioned is that everything you set up when it comes to aliases is session specific so if i was to close this terminal window right here i would lose every alias that i set up so how can you make these permanent well what you would do is you would edit your bash rc i'll just use nano and the file name is dot bash rc the dot means it's a hidden file so i'll press enter and your output is probably going to look quite a bit different than mine each distro has their own default bashrc file but what you can do is scroll all the way down to the bottom you can add a comment although this is optional i just like to do this so i'm just adding one of the aliases here that we've used previously ctrl o and then enter to save the file and then ctrl x to exit out so now if i open up another tab here type i and then tmux even though it's already installed it's asking me for my password that's a good sign and the command worked and i didn't have to set up that alias because i put it in the bashrc file and because i did that that file was read when the shell was created which also means that that alias was created as soon as i launched this shell so what you can do is add all of your favorite aliases to your.bashrc file in your home directory and from that point on your user account will have access to all of those aliases every single time you open a terminal what can i say i love aliases i have a bunch of them in my bashrc file because it's just an awesome concept to take a specific command change the name shorten a command i mean you could really use your creativity with aliases and that's one of the things that i love the most about it and you know what let me know what some of your favorite aliases are i look forward to checking those out in the comments down below so leave me your favorite alias down there and also click that like button if you like this video that lets youtube know that you want to see more content just like this as always thanks for watching and subscribing and i'll see you again very soon [Music] you
Info
Channel: LearnLinuxTV
Views: 7,936
Rating: undefined out of 5
Keywords: Linux, Tutorial, Howto, Guide, Distribution, Learn Linux, open-source, open source, gnu/linux, LearnLinuxTV, linux tutorial, linux tutorial for beginners, bash alias, linux command line tutorial, linux commands, command line, linux bash, linux commands for beginners, linux command line, linux basic commands, shell aliases, how to, linux, gnu linux, shell, terminal, tty, command prompt, shell commands, bash, bash shell, learn the shell, learn linux, learn the command line, bash tutorial
Id: Ok_kD_sgNcs
Channel Id: undefined
Length: 17min 58sec (1078 seconds)
Published: Wed Oct 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.