Sitemap
TechWithTwin

TechWithTwin

I'm a Fullstack Developer (Next.js, Django, and Express.js ) and an accomplished technical writer.

What are Flags In Linux

4 min readAug 11, 2024

--

Press enter or click to view image in full size
how to use linux flags thumbnail by techwithtwin

Have you ever wondered what are those commands or tags added at the end of a linux command? e.g what is -a or -all here?

ls -a
# or like this
ls -all

So what you have just seen there on top are called flags in linux, and we will be exploring them in this article dimisfying what they are and how to use them correctly.

Linux parrot falling from the sky in a tutorial on how to use linux flags by techwithtwin.com

What are flags?

Flags in linux are also known as options and they play a very crucial role of modifying the behaviour of commands and they enable additional functionalities. Flags normally begin with a single “-” or multiple “ — “ Example usage as shown below:

ls -l
ls -la
ls --help
ls --all

Types of Flags.

You might have seen that some flags start with a single “-” whereas others start with double “ — “. Okay the reason behind this is that we have two types of flags.

  1. Short flags — begins with a single hyphen “-” e.g ls -l
  2. Long flags — begins with double hypens “ — “ e.g ls — list
  3. Combined Flags — has multiple short flags e.g ls -la

Short Flags:

Short flags are flags that begin with a single hyphen. Point to note is that when passing a single hyphen the interpreter will take all the preceding characters e.g if you write ls -help it would think that you are passing different flags for -h -e -l -p so hence the use of double hyphens. Check the following example to see how short flags are used.

# These are examples of short flags
ls -l # the -l is a shortform of the long --list
ls -a # the short flag -a is a short form of the long --all

Get TechWithTwin’s stories in your inbox

Join Medium for free to get updates from this writer.

Long Flags:

Long flags are flags that begin with double hyphens e.g

# These are examples of long flags
ls --list #long form -> short form is -l
ls --all #long form -> short form is -a

Combined Flags:

You can combine multiple short flags together to form a chain of flags. You can also combine multiple long flags together, but this is not always recommended. e.g

# These are examples of combined flags.
ls -la # this combines the listing and all flags.
ls -l -a # you can also write them like this but this is not recommended.
ls -lash # combines listing,all,size,human-readable flags together.
ls --list --all --size # combines multiple long form flags.
ls --all -a # This also works so take note of it!!

Isn’t that cool? You might be wondering how the hell will I memorize all those flags? Okay the answer is you don’t there is a special flag that helps you incase you can’t remember the exact command you want.

Help flag:

I’t also common to forget the flags, so, there is a special type off a flag called the — help. The help command as shown below prints the infomation of the particular command and also lists all the available flags. Check the example below:

ls --help #prints information about ls and all the available flags

Man command:

To print the documentation of a specific command that you might be unsure of or want more information you can use the man command. Check the example below:

man ls  # prints all information of the ls command

How to use the flags:

To use flags is as simple as just adding the flag at the end of the command. By now you have already seen the flags in usage. Key point to note is that there should be a space between the command and the flag. Check the example below:

ls -a #just append the flag after the command and make sure it is separated with a  space

Summary:

We have just learnt how to use flags and the different types of flags. If you’d like to receive such articles in your inbox kindly consider subscribing to my newsletter at https://techwithtwin.com/newsletter and also follow me around. See you later ! Happy coding.

Happy coding gif from techwithtwin.com summary

--

TechWithTwin
TechWithTwin

Written by TechWithTwin

I'm a Fullstack Developer (Next.js, Django, and Express.js ) and an accomplished technical writer.