Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Flags" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to use flags on the command line, and explains that similarly to arguments, flags are information that can are used in CLI commands.


Transcript from the "Flags" Lesson

>> So let's get into something called flags. So I've already shown you one flag, kind of unintentionally and that's help. So if I say ls, let's do pwd again, -- help. This -- help is actually what's called a flag. It's saying turn this thing on or off, right?

So in this particular case, I'm saying I want to run pwd but I want to run pwd in help mode. And help mode just spew out all of your help information, that's all helped mode does. Let's talk about one that's a little bit more interesting than that. One thing I'm gonna say, people are probably going to try an echo -- help right now and it's just going to echo out help.

That's to be expected because echo is just trying to output literally everything in there and I think, there is a way to do it. Anyway, if you're trying to run echo help, you're going to see that and there is a way to run the echo in help mode but we won't talk about it right now.

So, going back to flags, you have ls, right? And ls just shows you everything in the directory but you can actually say I want to see more information than what I'm seeing right now. So if I say ls-l, it'll actually give me more information. So this -l is another kind of flag.

And this is telling me here is the long form output of this. So you can see here, this isn't super interesting because I only have one file in here. But it tells me this is from the ubuntu user from the ubuntu group. You can tell see that this is 4096 bytes big, aka four kilobytes, and this was created on June 30 at 10:45.

It's a directory called Ubuntu. Okay, and then we'll get into these are the permissions right here but we'll get into that later. So -l is another flag that just tells you here is the long form output. Okay, so now what happens if we wanna have two different flags?

Well, we can do -l and then we can do an ls-a. And what -a does is it says show me hidden files and directories. So in this particular case, it'll show you the relative path of the [INAUDIBLE] is actually represents this directory. So this directory is 4 kilobytes big, which is the minimum size in this particular case.

And dot dot is shows you to go up but that's the -a does is show me all the hidden things. So just for fun, let's create an extra hidden file. So I'm gonna use a file program called Touch. And this is gonna be called .hidden-file. No, sorry, I need to do that inside of Ubuntu.

So now I'm inside of the Ubuntu directory. Touch.hidden-file and the dot means it's a hidden file, and touch just creates a file, we'll go over that later as well. And you can see now there's actually a bunch of stuff in here. One of which is hidden file that I just created right there.

But there's a bunch of hidden files in here. But now if I run this again, without the -a, you'll see all of that stuff is hidden and all I can see is the home directory and the snap directory inside of my Ubuntu directory. So, that is what flags are.

We've talked about two different kinds. One is the long flags and that's gonna be something like help, right? So the two dashes means that I'm doing one flag and it's a long way of writing it out. So another way of doing that, I think, would be -h maybe.

Nope, so let's do ls--help. Help, and that's what all of these things are is telling you, what all of its flags are, so let's see if we can find help here. No, doesn't even exist, but Let's take a look at -a. So -a is that one that we're looking at, it says show me the hidden files, but you can also write -a as --all.

So --all so if I do ls-all and ls-a, you can notice those are the same output. That's because -a is just the shorter way of writing --all. So, the one dash is the shorthand, the two dashes is the long way of doing it. Now, why do they have different dashes?

Well, if I have ls-a-l, right, where I have two different flags here. The way I can actually rewrite that is just -al or -la. So the one dash means I'm gonna give you a bunch of flags all at once just right in a row, right? So I don't have to write -l -a, I can just write -la, so that's why it's just one dash.

It's letting you know a bunch of flags are coming in here. If I did -help like this, it would think that I'm passing in four flags of -h -e -l -p. That's all you have to do --help. And you can mix and match too, right, I could do -a or ls--all -l, it would be the same thing.

Questions about flags so far? I know I'm going really into depth on flags, but they're so important. They're so core to running stuff in the command line that you kinda need to know how they work. And I'll be honest, I didn't really know why there was one dash and why there's two dashes until halfway through my career.

That's probably shameful to admit out loud but I know it now, all right. So, one more thing about flags as you can also pass things to the flags directly as well. So if I come in here and I have a snap directory, right, let's say for a second I wanted to ignore the snap directory.

Well, there is a flag that says --ignore, right, which is what I would want to do. And I can just say --ignore=snap. And you can see here that it just ignores that but that's what that dash dash equals, this tells me I want to ignore this. And I think you can probably do this as well.

Oops, so often, that equals is optional as well and not always, which is kind of annoying as well and that's kind of what you're going to find with the command line. Every program works a little bit differently because they were written by different people in different decades frequently.

So some of them will respect this equal signs and some of them won't. Some of them require it, right? It's wild which ones work and which ones don't. Sometimes you just kinda have to remember it. I think with this one as well, you can also give it multiple times.

So let's say I wanted to ignore home as well. And you can see there now I can't see anything. That makes sense, right? If you want to ignore multiple things you can pass in the same flag multiple times. But again, the way that flags are handled is actually not native to [INAUDIBLE], it's actually native to how programs are written.

So it depends on how each one of these is written. Now luckily, most of these computer scientists have been working in concert for decades. So they work for the most part in ways that you would expect. But if you'll find that if this one works one way, sometimes another one won't work the same way and you kind of just have to know sometimes.

So last thing I'll leave you here is every time that I run ls, I always run it with -lsah and I actually don't even remember most of the time, what those stand for. But it actually gives you this full output and that's why I use most of the time.

It gives you the size in kilobytes, megabytes, which is useful. It's a little bit more human readable. It shows you all the hidden files. What is s? I can't even remember. See, I mean this is exactly I'm talking about. It's just it's second nature. -S is for, so that's the size one.

So that's what shows you kilobytes instead of bytes, and then H is human readable. I guess that's what that one is so S is it's just show the size in general like how big files are. I've actually seen some people will actually, yeah, modify the their [INAUDIBLE] so that when they run ls, it actually automatically runs it with lsah.

Cool, I think that's it. Yeah, that's the last thing I was going to show you as well. We were doing ignore, right, ls-ignore snap, right? You can also do ls-capital I snap as well, right? Most of the long form ones also have short hands and vice versa. So and it's always case sensitive.

So if I do lowercase i that's going to be a different flag than dash capital I. And again, you just have to know or you have to look it up every single time. And in particular, something like ls where it's such a flexible tool. You can see it has 30 flags that you can get it.

All right, how are we feeling about command lines so far is it going okay? Awesome. Thumbs up is exactly what I wanna see. All right, let's keep going. So now you should be feeling a bit more comfortable with how to run commands with the command lines, right? You have to run the command, you have to give it its parameters, you have to give it its flags.

Something else I will tell you about flags. Some programs care where you put them and some of them don't, and that's also kind of annoying. Let's just see with ls here for just a second if I say ls, where am I? Okay, so if I say ls snap, right, which will have some stuff in there, right?

It has one folder in there. If I say ls-lsah snap. Okay, that works. What happens if I say ls snap-la. It works, right? So, this one is a good one that it doesn't care what order I give it, right? It just knows that, okay. If I see the dash and it's a flag and it can handle that some programs profoundly care where you put the flags.

So I will say the standard place to put the flags is immediately after the command so it would be here. But there's a bunch of programs that you can put it afterwards. Just be aware that sometimes that that order matters and is just depends on how the program was written.

That's a good one to know, that trips me up all the time. So I would just say get used to putting them first because you'll just save yourself some heartache.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now