Skip to content

Flags

Earlier, I said that commands in Bash/Zsh are like functions in JavaScript. The analogy breaks down a bit when it comes to flags.

Flags are modifiers that tweak the behaviour of commands in predefined ways.

For example, let's look at the rm command. This command allows us to delete individual files:

Running 'rm theme-song.mp3', and then running 'ls' to show that the file was deleted.

We don't get any sort of confirmation, but if we check, the theme-song.mp3 file has indeed been deleted.

If you try and use the rm command on a directory, you'll get an error:

Running the 'rm' command on a directory, and getting an error

By default, rm can only remove individual files, but we can change this rule with the r flag:

Running the 'rm' command with the 'r' flag, and successfully deleting the directory

The r flag stands for “recursive”. It will delete everything inside the stuff directory, anything inside the directories inside the stuff directory, anything inside directories inside the directories inside the stuff directory, and so on.

You might also run into some file permission issues. For that reason, the f flag (Force) is commonly used as well. We can group multiple flags with a single dash, like this:

Running the 'rm' command with the 'r' and 'f' flags

Flags take many shapes and sizes. By convention, it's common for flags to have a short form (eg. -f) and a long form (--force). The long form typically uses two dashes, and uses whole words instead of individual letters.

Let's look at one more example. the ls command we saw earlier is commonly called with two flags:

  • The l flag, “long”, which prints the directory contents in a detailed list with metadata.
  • The a flag, "all", which'll include hidden files and directories.

This changes the output considerably:

Running 'ls -la'. A detailed list is shown, along with hidden files and folders.

There's a lot of noise here, including the ridiculously-obfuscated permission glyphs. But some of the metadata, like the dates that show when a file was last updated, can be useful!