How do you use the Command line? PowerShell, cmd, bash? - Computer Stuff They Didn't Teach You #13

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends it's computer stuff they didn't teach you arbitrarily numbered number 13. just in case you feel that they did teach you this and you comment first in the comments that hey i learned that in school that's fine but we're going to talk about something in maybe a different way than is usually taught in school this is meant for computer people who just never quite understood how these things work consoles terminals shells command line hey you need to learn the command line do you understand the command line a lot of time you spend you know interface like this whether it be in windows or mac and then one day someone changes it and now you're looking at this and they expect you to know exactly what's going on and that can be really confusing and that's called the command line we're going to talk about the difference between this and this and what it means i'm going to do it in a way that maybe you haven't seen before what we're going to do is i've taken windows terminal here and i've chopped it up into four panes um down here where i am with this green area is not going to be used that's where i'll be we've got over here in the upper upper left corner powershell we'll talk about that down here we've got what used to be called dos but we'll call the windows command line the command prompt cmd and then above me right there in the maroon we've got bash the bash shell i've labeled it ubuntu because that's the operating system that you'll be most familiar with but that brings up our first point so what's the command line what's a terminal what's a console well a terminal or a console app is basically like a web browser or the command structure that is provided to you by a shell so you can use firefox or chrome or edge or whatever to get on the world wide web you can use any terminal application windows terminal x term item there's lots of different terminals out there to talk to a shell a shell could be powershell bash ash fish oh my zsh there's a ton of different shells and opportunities there there's command that's built into windows there's powershell there's powershell core these are just text based applications they call them text mode apps where you type some stuff and something happens you type some stuff and you press enter now in this case stuff isn't a thing so nothing happened you'll also notice that right above me as i typed stuff because i want you to understand how this works you're going to see my key strokes because people at the command line tend to move pretty fast i figured i'd put my keystrokes above us here using an application called carnac and karnak is going to be watching all right additionally you'll notice that powershell has just a little bit of a blue frame around it it's the frame that we're currently using if i click over here now ubuntu has a little blue frame here if i click down here on the windows command line it has a little blue frame i'll click between those and i can also go alt use the alt key i'm pressing alt on my keyboard and i go up down left right and i can move from place to place and you can see that those hotkeys are going to stack up and then they're going to fade away so that's going to show you what i'm typing okay you'll notice when i typed something here in powershell and i didn't like it i cleared the screen i said clear i'm going to do each command in each shell so that you understand what's going on so i type stuff and powershell says this isn't a commandlet i type it in what i'm going to call dos but the windows command line it says that's not a recognized command and then if i type it over here in ubuntu it says oh did you mean stiff and then it offers to try to install something so right off the bat you see that different shells in different environments give different experiences this one here in ubuntu and bash is trying to be helpful and tell me something this one here is suggesting maybe my spelling is wrong this one down here is just saying well it's not a command or a program or a batch file but the fact is i typed something and i pressed enter that is a common thing that all shells have now if i type in cls here in dos and i type in clear here in powershell and i type in oh i made a mistake there see i typed really fast i misspelled clear and you can see the keys that i pressed to backspace okay now i might want to run clear down here in dos and have that work but clear isn't a command in the windows file system so what i did is i made it a command shells like this have different commands that you can type and they have what are called aliases so i can type alias and get a list of all of the things that i could potentially type and the thing that it actually points to so if i type in alias over here in ubuntu here's a couple of places where i said el means something or ll mean something else i'll talk about those in a little bit there are no aliases in the windows command line again every shell is different and there's all kind of fun history that you can go and learn about if you choose to so what i did is i wrote my own clear you'll see here i'm saying where where is clear and i made a batch file it's a batch of commands and it's right here in this location i'm actually going to select that with my mouse okay and then i'm going to either hit ctrl c or copy or i'm going to right click with my mouse and i'm going to say type i'm going to paste that in and you'll see that clear.bat it's just a text file with cls in it why would i do such a thing well if i'm going to show you a bunch of different command lines a bunch of different shells i was confused that if typing clear here and clear here and cls over here might be confusing i figure i'll type clear which runs that batch file and then runs cls clear screen cls which is how you clear a screen on windows it also helped me introduce the concept of aliases there's lots of commands that you can learn i'm just going to teach you about 10 or 20 but there's hundreds this is a lot like learning any language you learn a couple of basics you figure out how to buy bread how to get food and then you can move your way around so we'll learn how to move our way around and that will hopefully set you up for success where you can go and experiment with the shell that makes you happy another quick note about shells is a whole religious argument about which shell you should use you should only use powershell oh my zsh is the shell for the people no use starship you should use bash or you should use whatever use the shell that makes you happy doesn't matter to me i don't care you're going to type some commands you're going to press enter and it's either going to seem intuitive to you or it's not so it doesn't really matter which shell that you use let's go back to aliases so first thing we notice here is that powershell has a ton of aliases and if i type alias here in bash on linux there's a couple of aliases but we'll notice these ones that are talking about ls and i'll notice over here that dir or directory and ls on powershell are both aliased to a command called get child item as is one that's called gci get child item so that's interesting let me clear my screen i'm going to type in ls i'm going to type in dir i'm going to type in gci those all do the same thing in this context it is a thing called get child item so that's how you get a directory listing we'll talk about directories in just a second over here i type in ls over here in windows i can't type ls i type dir so dir works here does it work over in linux look it does must be an alias for that somewhere or i can type ls here it doesn't really matter again which one you pick but every shell is different every shell is different and when something works and you wonder how it works like here in powershell i'm typing dir and ls that alias is set up to make people feel comfortable so that when they show up in a shell that is unfamiliar it'll mostly work so if you were a windows or dos person if you were an ubuntu person you showed up in powershell you'd go i'm confused uh ls oh cool and then you suddenly feel comfortable that's important i'm going to go ahead and clear these clear clear which is our batch file and clear and i'm going to notice this here this right there that little tilde tilde tld gilda that symbol is telling me it is in the home directory so if i type in pwd print working directory pwd interesting doesn't tell me that pwd over here i'll type cd so print working directory print working directory doesn't work in windows you can type cd it'll tell you your current directory you'll notice it's also in my what's called my prompt see how it's blinking right there it's prompting me or the next thing i'm going to type it's blinking it's prompting me notice how your prompts are all different windows and dos prompts usually include a drive letter the c drive or the a drive or the d drive i added some cool stuff to my prompt and powershell that's actually my blood sugar i'm diabetic and you can search for hanselman diabetes prompt to learn how i put my blood sugar in my prompt linux people are used to a prompt with a dollar sign by it but either way we have a current directory it's the place that we currently are on these i am in c drive in the users folder in the scott folder beneath that and i can type change directory and go into the desktop so now i am in the home directory desktop now powershell here this is using a thing called oh my posh you can go and google for hanselman d prompt and i'll teach you how to do this this is saying the home directory and desktop underneath it which means i'm in c user scott desktop down here in dos i can type in cd desktop as well and over here i don't think i have a desktop on this ubuntu so it says there's no such file no such directory that's because i don't have a desktop folder here if i type in ls i can see lots of different files and lots of different folders but i don't have desktop i could make a directory called desktop nothing happens but it made it type ls and see that it made it and then i can go in there and now i'm in there so now on each one of these things i am in a folder called desktop i got there with cd now in powershell i can cd all over the place i can go into my blog backup then i can say cd dot dot to go back up i just went back up to desktop i can say cd dot dot and now i'm back up again dd desktop cd log backup now watch this tab yeah that got me there fast cd dot dot dot dot cd desktop tab cd bl tab i can also type cd hyphen and just go back to where i was before cd hyphen let's see if that works in other places that's interesting see some places these things work some places they don't so you want to watch for that all right let me clear my screens each one of these is a shell now if i say dir eir ls to look at my folders i might want to make a new file on a ubuntu or a bash shell you would usually say touch you could say new file dot text and if i say ls there's my new file dot text i want to see more information about that i can add in some switches these are all different switches here yeah that's a very different ls than the one we got before this ls came out kind of wide and each one was in a column there here we've got a lot more information about what's going on nice coloring those switches can be described in what are called man pages so if i say man for manual like where's the manual type man ls i get a whole list of switches all those switches we were looking for remember when i said dash a for all give me everything and then i can just type in q right there q for quit to get out now when i've typed a bunch of commands in any shell you can press the up arrow and each time i press the up arrow i can see my history notice how i get a nice history up up up up up down down down in bash or in powershell over here and over here in dos or in the windows command line but the windows command line if i press f7 gives me this which is pretty cool it actually draws its history that's a feature that i would really like in one of my other shells and you're going to find that as you start exploring shells whether it be powershell windows or ubuntu you're going to say i really like that one and i really like that one and you're going to end up wanting to basically build the perfect shell it's very natural because what you're ending up with here are a series of small tools and you're using those tools to work together and you're going to say well i like the way this one works and i like this prompt but i really like this history thing but i like the way this does directories and i like the way i can move files around and you might switch prompts during your career in computers i can use all of these prompts fairly comfortably but i kind of find myself in powershell more often than not it's just honda toyota you know they're all the same car but i kind of like honda that's one of the things that you're going to have to discover as to discover as you move your way through tech so we said touch and we made a new file okay and i do the same thing over here well on powershell you can't go and do touch you can say new item and you can say make an item and give it a switch with the item type of file and you could say new file.text and there you go i just made a new file you might say gosh that is not nearly as cool as doing touch well remember aliases there's an alias or new item called ni so i could just say ni num file.txt and now i've made another empty file that i could then open up and do something with you're also going to find as you play around with different shells that there's a lot of different ways to do the exact same thing there's ac for add content and i could say add content some other file dot text and then i could give it null or nothing content so then i can say hey give me all the text files there's the text files that we made just now you see the date and the time and look at this i did a wild card okay right here we said give me everything with this extension star dot text i've got two files here that are called sum that's interesting i could say eir sum star or ls star those things usually work the same those wild cards use the same across almost all shells so if we try the same thing down here oops notice okay this is a great example here so in powershell i'm in c user scott and the windows command line look where i'm at see user scott desktop i said hey give me some all the files name sum and i i immediately you heard me go oops oh well i'm in the wrong folder so how do i go up i can go cd dot dot okay or i could type the entire thing that causes that does the exact same thing now remember that i typed this before i could go up up up up up enter on windows or i could hit f7 enter and do the exact same thing okay let's try that over here now we're in ubuntu let's say ls r.text it has a different file system because it's a different operating system i could say ll my alias remember that i have an alias for kalel that goes and puts in some other things for me so i said here ll star dot text all right and that works great if i'm in the desktop and i do ls it says i can't find that there's nothing here i say oh i'm in the wrong place okay how can i get back well i can go up like this or this is a little bit tricky i can go and search the history of all the things that i did before i could hit ctrl r reverse search okay reverse search and we could go and say look there's the time i typed desktop or i could say there's the time i typed this search just for that control r search for it enter so that allowed me to search through all of my history that's a little bit advanced but that's kind of a kind of a neat thing all right let's clear our screens with clear als als oops see can't do cls gotta be clear on ubuntu you're gonna find as you move around it's like going to italy and saying a spanish word maybe it'll work maybe it won't you gotta watch out for that so we had some files in each of these worlds and i went and touched them notice that they are empty they have nothing in them i can prove that by typing cat for you know concatenate and i could say hey new file p a t space n e tab tab enter see how i found another file there when i did that i could say some file tab enter i could type alias to make sure i know what at means on powershell it means get content down here in dos cat isn't a thing you have to say type type some file and over here it's cat so everything's different oh that's interesting look at that so food.txt actually has something in it we can probably prove that and see that its size is 5 which makes sense it's got 4 letters and then an enter or a line feed in this case there's your five bytes and that's why that's five so if i said cat new file there's nothing in there so how do i put something in there i could use a text editor and talk about those another day but what i could do is i could say echo make friends and then i could do this interesting thing look at this hey friends echo is a command and this shell every shell is different i could redirect it i'm pointing it it's almost like those are pointing in that direction i'm going to say put that in this file if i had just one it would make the file it would clobber the file if i have two it's going to add to whatever that file already has it's going to append to that file let's try that okay and then we'll go back and we'll look in our file look at that then let's look at our directory look at that see hey friends and then we see we've got 13 bytes we've manipulated that file okay do the same thing over here actually let's try it without those and we'll our clobber file all right now look we've got a file with 14 and a file with 14. i can say cat foo it's a cat new file and see they both have the same thing in there and that's because we clobbered it right here and we appended right there same thing applies down here in dos i'm going to go ahead and clear my screen i could say echo it's windows hey it's windows some other file oh can't use cat gotta remember your different languages in your different worlds and there we go look at that that's interesting see how it put quotes inside the file let's try something else that look at that what if i did no quotes would that work this is another interesting thing to think about every shell treats quotes differently something like this might seem very counter-intuitive to someone using bash they might expect to put a string between two quotes whether they be single quotes or double quotes but in this world it's okay which is a little bit weird over here you definitely want to have those these are the kinds of things that you'll bump into as you go and explore your shell worlds okay now um last time we had a show made a file let's see if it's here i think i put it on the desktop called stacks okay and this file here is my notes from episode 13 that i was using to go and figure out how to teach you all how stacks work these are my my notes i'm just scrolling with my scrolling wheel so how would i go and search that text file or a particular word okay again everyone is different let's clear our screens make sure that we're on the desktop on each shell or we have that stacks file available to us there we go so i've got stacks here stacks here stacks there okay so in um let's do ubuntu first okay we're gonna search for a word in ubud too we can do it a couple of different ways remember that we can say cat stacks and then spit it out let's say i want to find the word no okay so i'm going to say at ax okay and then i'm going to type it i'm not going to redirect it i'm going to type it i'm going to pipe it into a thing called grep grep is a word you may have heard before that is kind of ubiquitous for looking for stuff if you grip through a bunch of stuff you're looking for stuff there you go look at that so we just went through this whole file we took the output of that file which before went to the screen we put it into a pipe and fed it into the grep command it then went looking for the word no let's do the same thing in dos well dos doesn't have grep okay so it's got a different thing so it's got feinster so i could go and say type stacks and then pipe it through feinstein find string and look for the word no do the same thing didn't get the cool coloring there but not too bad that's cool now a little bit different inside of powershell in powershell there are aliases for things but virtually everything in powershell that has an alias has a big long big long command like select string is a thing and gc get content is a thing so technically you're supposed to say get content stacks and then pipe that through select string and look for no and then i get the same thing i would get over here or i could type gc that's an alias type it through select string sls that's an alias and see no as well which is kind of cool okay you'll see lots of commands whether you're doing kubernetes which we saw in a previous video or really anything where you take a little command that does something you take its output and you feed it into another command you beat it into another command you build up entire scripts bash scripting there's been books written about bash scripting or powershell scripting or shorter books written on batch file scripting that will teach you how to do stuff to manipulate your computer whether it be manipulating strings or manipulating a little bit more the last thing we're going to see as this has been just an intro to three different shells is not going to be manipulating strings but rather manipulating processes or processes depending on what country you're from i type in get process i can see the things that are running on my machine over here in powershell i'm here in bash i type ps for process and see just the ones that are available to me there's fewer because i'm not an administrator and now in dos or in the windows command line i can type ask list and get the same list that i saw up here or at least a pretty close one okay so then if i wanted to i could go and say ps and then i could go and grep for something right i could say zoom it running let's see we don't have grep on powershell so i've got to go and say sls select string and then ps is an alias forget process so here i went and i got those over here in dos i could say find string and i could say zoom interesting so not sure why that didn't work maybe it's because it's case sensitive there's a switch called ignore case slash i with any command you want to use in virtually any shell you can ask yourself questions about it with either dash dash help or forward slash question mark and it'll tell you about it on linux we said a man we said man i could learn about grep or manual on powershell i could say ps dash dash help oops look at that i can't say dash dash help notice how i'm trying to figure out what it is it's going to tell me about this there we go they have man pages too not interesting but there's always a way to figure it out but they're going to be different for each shell okay so just to be clear i'll back up here and i'll say find the process called zoom it come back down here find one called zoom it and i'm not running zoom it of course over here in ubuntu so i can't go and search for it all right very last thing that i want to show you as you're thinking about different shells is the idea of who is administrator now the concept of administrator between different shells really depends on the operating system underneath it so for example if i wanted to run a copy of windows terminal again i could hold down control and shift i'm holding down control and shift i'm going to click right here it's going to pop up a question and it's going to go and make a shell and it's going to make me administrator i've got my posh kit set to give me a little lightning bolt and this thing popped up and said administrator administrator you are someone who can delete everything and potentially hurt yourself that is a windows thing over here in the ubuntu world that's done with a thing called sudo super user do so if you want to go and do something like if i wanted to go and install a program it'll say hey you can't do that you need to be a boss so i'd have to say okay super user do that thing okay and then if you don't like sudo you think maybe that's a little bit mean you're ordering them around i could go and say alias please and i could say please is sudo now i made my own alias i could say apt install emacs i think it's hell right fine please app install emax giving away my password and now that's going and installing the program that i wanted there we go all right and there's my alias so what do we learn in a short half an hour there's different kinds of shells shells can be talked to with really any terminal application that makes you happy different operating systems have different shells powershell is a shell that's available to you on windows and mac and on linux you can install powershell go and google around for that dos or the windows command line is the one that you usually see c colon on and that's been around forever and ever you might find yourself there you can just go to the start menu and type in command to find those different command prompts then bash the one that i'm using over here in ubuntu is just one of the many many prompts available to you in a linux type environment and i have been accessing it using an instance of ubuntu using a thing called wsl2 the windows subsystem for linux that allows you to run linux on a windows machine you go learn about that in my other videos now i recognize that this is not the complete understanding of all things command line it's just a primer we talked about cd we talked about making directories moving a little bit around changing directories from place to place we talked about cat and type and looking at text files inside of those text files searching for stuff with feinsteir or crep we talked about looking at processes with ps or get process or task list and then from there you can go and explore your own machines and pick the shell that makes you happy and i would remind you also that i'm happen to be using the windows terminal which has allowed me to go and do all this kind of cool stuff in different panes and add graphics and colors and all kinds of fun stuff so be sure to check out the windows terminal and if you like videos like this please give me suggestions for other videos in the comments and the best thing that you can do for me as i try to get towards 100 000 subscribers is tell your friends and have them subscribe and then maybe i'll uh keep doing this thanks so much
Info
Channel: Scott Hanselman
Views: 28,993
Rating: undefined out of 5
Keywords:
Id: QKBcHuA3VJE
Channel Id: undefined
Length: 34min 52sec (2092 seconds)
Published: Sun Oct 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.