Understanding Command Structure

Ziyad Yehia
A free video tutorial from Ziyad Yehia
Energetic Udemy Instructor with a Project-based Approach
4.7 instructor rating • 2 courses • 146,293 students

Lecture description

In this video, you will learn the overall structure that every command follows. This will allow Linux commands to start looking like a language rather than random text on the screen ;)

Learn more from the full course

Linux Mastery: Master the Linux Command Line in 11.5 Hours

Learn the Linux Command Line from Scratch and Improve your Career with the World's Most Fun Project-Based Linux Course!

11:16:52 of on-demand video • Updated July 2020

  • Quickly Learn the Linux Command Line from Scratch!
  • Use Bash Scripts and Cron Scheduling Software to Automate Boring Tasks!
  • Become an Independent User of the Linux Operating System!
  • Learn how to Search for, Customise, Install and Manage Open Source Software with Package Managers!
  • Operate a Linux Computer Entirely from the Command Line!
  • Gain a Complete Understanding of Linux & fill in any existing knowledge gaps!
English [Auto] Hello you beautiful people so when the last video you learn that there is something called the shell that is interpreting the commands that you type into the terminal. Now one reason that Linux is amazing is that the commands that you type all follow a similar type of structure in the way that you type them. Now by understanding the structure you'll be able to transition from simply just memorizing commands and hoping for the best to actually understanding the common language that all Linux commands use and that will give you a massive boost in your abilities. So in this video you're going to learn what commands actually are and how they are structured. And by the end of this video you'll have a much deeper understanding of what commands are and the commands themselves will actually start to look like a language rather than just random jibberish on the screen. So this is going to be a very game changing lecture. If you understand fully what I show you here it will make your work with the terminal so much easier. So let's not waste any time and get right into it. OK. So the first thing you need to know is that all commands are effectively just little computer programs that are installed somewhere on your computer. So for example date is a program Cal is a program Ecca was another program and so on. Now the command or program name that you want to use is always the first part of the command. So we start a command with the word data or with the word count with the word echo like you saw before. Right now each command has its own unique behavior. You can actually look up using something called the manual pages which I'll show you how to do later in this section of the course. But the general command structure goes like this. First you type out the command name. So I would be like date cow echo or whatever it would be. Then you give the comments of options to customize its behavior. Then you give the command some inputs to actually operate on. Now the first thing you type is the command name. Now that lets the shell know what program you actually want to run. And once the Shell knows what program you want to run it knows the name of the program you want to run the show will then search for that program on something called your Shell's path which is just a list of folders that contain these programs. Now you can actually see your Shell's path by typing echo. Then a dollar sign then path. OK so Ecko space than dollar sign and path and you press enter and you see this terrifying output come out. But it's actually it's actually very easy to understand when you notice that these are just different folder paths each separated by a colon by these two dots here. So for example there's a there's a set there's a folder here that's called slushed use a slash local slash Espin. Then you've got another folder here Slusser uses less local slushed bin than another folder here and another folder here and another folder here and so on and so on. OK. So what the shell will do is it'll start at the very left of the path and it will look inside that folder for a command called Echo or called Kallo called data or whatever you called it. OK. So it will look in the left most folder and it will if it doesn't find it there it will move to the next one. So it would start at the very left and it would move to maybe to this folder. OK then if it didn't find it then it would move to this folder and if it didn't find it there would move to this folder and so on and so on and so on until it got right to the ends. So to slash not slash been OK. And if it didn't find a folder with that name in any of those folders. OK. Then the show would give you a command an error like command not found. If I tried to do an article that was a command it says it couldn't find this command. So what the shell did is it looked for a command called this in each of these folders. Didn't find it and said command not found. Now this is some this has an interesting and interesting impact. The fact that it the fact that it reads from the very left all the way to the very right. Let's say you had a program called blah OK and blah. It was actually in this folder here is a slash Espen but it was also in the very far left folder. Which one do you think will actually be run well actually what would happen is it would only run the version in here because that's where it found it first. And if you wanted it to run this version was in this folder that wouldn't be possible because it found the same name here first. OK. So that's just something to bear in mind it's not something you'll really run into but it's just something to bear in mind. OK. Now you can actually see which folder the command is stored in by using the which command. So it's like which folder is this command in. So I could say which and then count. OK. And if I press enter it will tell me that the command is actually in the slowest user slash bin folder. And if we look at the path we can see that slash use a slash bin is located here. So it is on our path and therefore Cao was able to be found. And let's try that again for Echo. If we do which echo you can say slash been slash echo. And if we look on here we'll find somewhere slash bin is somewhere slash bin. There we go. So slash bin is also on our path. You actually also look at the which come on. So the which of which which folder is that which commanded. And we say it's in slushed use a slush bin which is here. There we go slushed user slash Ben Gay and we see the which command also runs as well so that's how the Shell knows where to find your commands. Now you can customize the way that commands work by giving them different options and different inputs. Let's go ahead and start with the inputs. So it's important to note that not all commands actually require inputs some inputs are optional. For example the date command doesn't strictly require an input. You can just type date and be you know be done with it. OK but most commands do require some form of input which can actually have a fancy name called an operand. OK. Because commands operate on the input the input is sometimes called an operand. OK so to give the command an input or an operand you simply type a space after the command name then type the input. OK that's assuming you don't have any options. So for example if we take the cow command I could just type cow OK and press enter and I would get this month's calendar. But to customize the behavior I can give it an input. So for example if I want to get the calendar for the year 2017 I could type cow 20 17. And if I press Enter now I get a completely different input output sorry because I gave the command this input. OK. You can also give more than 1 inputs. Let me clear the screen using control and all the nice shortcut so that you can give more than one input. So for example I could type cow 12 20:17 and press enter. You can see now that I get I've given Come on the calc command two inputs one being 12 which means the twelfth month and the next one being the year which is 2017. So I said here can you give me the calendar for December 2017. All right so we gave it to inputs and that also customize output. So basically you can just give commands inputs like you and if I say echo when I say hello then echo then hello sorry was the input to the echo command. So it's quite straightforward. All we did was type the command name which the shell then found on the path and we gave that command that that program some input. OK so that we understand inputs Let's discuss options because this is where stuff gets really interesting. OK. You've seen you seen as use an option with the Kalkhoven as well so the cow command we gave it the y option before. OK so if I do it by default the dash Y will show the current year's calendar. OK. So we've managed we gave the command command an option which the y option which you can see that has been preceded by this dash and that's common for commands. They tend to be preceded by dashes and hyphens and things. So let's take a look at the date command to understand how options work even better. OK. Date by default will show today's date in our current time zone. So if I say date it says here's the here's the date and time in the current time zone which is apparently British Summer Time OK. So get the day in universal time such as UTC or Greenwich mean time we can type a date slash you and we can see now the day I was gone back one OK because now we're in UTC rather than British Summer Time which is currently in our forward. So we gave the date command the you option. Now if we had other options to give the commands we could list them right after one another. So for example we could do date slash dash a dash B C D E F D and this would give it these different options or the option B option to see option the d option the eruption the f option the g option. OK. Or more simply you could just do dash A B C D G. OK. That's a nice shortcut. You can just stick them all together like that. OK. Now the specific example doesn't work. So if I try that it will say Invalid option but it just illustrates the point that if you change these options together you can actually chain them together and the order doesn't really matter. So I could do date. G d g f and it would have exactly the same effect. OK. All right then. So now it can be sometimes be difficult to understand what these options mean. So sometimes options actually have long form names. So for example data slots you ok. Sorry. Which gives it the date in universal time. You can also do date dash dash universal now long and gives us the same output. No universal long names are preceded by two dashes Z's a dash dash universal whereas the short form options which are just one letter usually preceded by one dash. OK. Now the long form commands can make commands easier to read but they're not available for all options. It really depends upon the command that you're using. And again just note is the fact that I use two dashes for long forms on one dash for short form commands. Now if you wanted to use for example three long form options you need to do something like this. So it has to do date dash dash universal. That's one long form option then dash dash long form option to then dash dash long form option 3. You can't really change those together like you can with the short form options. OK. So that's not going to work. But the short form options you can do. Dave slash a b c d e f g but with long form options you can have to separate them just universal dash dash long form option 2 and dash dash long form option 3. OK. Now it's important to know that everything on Linux including commands is case sensitive. So for example the date command in lowercase is valid. So they combine and locate is valid but the date command like with a capital D would not be valid. The date command we'd like a capital A here that would not be valid either. So you need to make sure that you spell your stuff correctly and with the right case sensitivity and that goes for options and inputs too. So it's not just the name of the commands and the date code on for example has a new option but I don't think it has a capital U option c it doesn't. So per capital human it didn't give me the universal output because it doesn't know that it has a capital you. I knows that it has a lowercase you options. Make sure that you spell your commands with the right case sensitivity. Now the only other thing to know is that sometimes options can have their own inputs. So for example if we go to a cow I'm going to play the screen if we get to. We could see the of deficit for December 2017 by giving it to inputs right now 12 2017 not options to inputs. But there is an option that will allow us to see a certain amount of months after that too. So for example if I did cow gave it the capital a option then rather number 1 12 2017. Okay this will show December 2017 with one month after and the the option gives you after but we did it also need to know how many months after. So I said one. So this one here is actually an input to the option and then you've got the inputs for the command. Okay. You can also show a month before. So for example KOUDDOUS be 1 12 2017 that will show us December 27 set to in December 2017 with one month before it's actually also combined together so now one month after one month before December 2017. Okay. So now you've got December 2017 with one month after which is generally 2018 and one month before November 2017. So here you can see how you can actually provide options with their own inputs and how options are okay to use when you give commands it's own input. Okay so you can use those different ways to combine this stuff together. So here we have the A and B options with our own inputs and the cow come on has its own two inputs 12 and 20 so 12 in 2017. So this is starting to get a bit crazy right like it's on to get a bit advanced and a bit hairy but this is just should just begin to highlight just how powerful Linux commands can actually become. And the only other thing you might need to know is that some commands allow you to put an equal sign to match up those options with the appropriate parts more explicitly to make stuff a bit easier to read. This doesn't work for cow but if it did it would be something like a cow after equals one before equals 1 12 in 2017. Now that doesn't work for cow and because it's manual pages and let it happen. So we only get 12 to 17 that didn't quite work but that's how it would work if it was possible for the command. And it won't you know it might just make it a bit easier to read the options are linked up with the appropriate inputs. If there were long form options you would do something like cow dash dash before equals one dash dash after equals 1 and 12 20:17. So here you know you've got long form options also being linked with their inputs again that's not possible for the cow. Come on specifically you can see how features like that. You know joining up the inputs with the actual options can make commands easier to read. So in this admittedly rather in-depth video you've learned that commands are basically broken up into three main parts. First you've got the command name then you've got some options which are preceded by dashes and hyphens. And you've also then got some inputs for the command. Now the command name needs to match a program that is on the shelf search path. Now the path is just a piece of text that stored in the shell that lists a bunch of folders. The shell will start searching from the left to the right separating each folder with a colon which is the two dots on top of each other. And if the shell finds an appropriate program then that program will run and the shell will pass to that program. Any inputs that you have given in the command. OK so you can also customize a Command's behavior using options now. Options are usually just letters that are separated by hyphens so a dash like you see at the very top of the summary slide here you give each option a dash before it or you can change the little letters together in one go. The order does not matter. And sometimes they have long form versions which are need two dashes but allow you to use easier to read words so instead of having Dasch you for the universal option you can use dash dash universal. OK but that really depends on whether the command allows that to be possible. And each command is slightly different. And one thing that became very clear in this video is that each command behaves very differently to other commands. So for example some commands can take multiple inputs and others take none. Some can take long form options and others can't. Some can have an equal sign to match option inputs with the appropriate option and others can't. So now that you understand the overarching command name then options then input structure that commands follow and the different ways of implementing that and the different options that you've got. How can you know what is possible for a specific command and how you use each specific command. Well each command actually comes with their own manual page on man page for short. That shows you how to use them. And in the next video I'm going to show you how to access and read the man pages. So just imagine having all the deets on a big catalog of computer programs just sat there waiting to be explored using the man pages is going to be a complete game changer for you if you want to become independent in the way that you use the Linux operating system. And I really can't wait to show it to you. So for that goodness I'll see you in the next video.