Hazem Allbabidi

June 26, 2023 | 6 min read


Structure of a Command in Linux

Knowing how to effeciently use Linux and it’s commands are essential to anyone trying to improve and speed up their development. In this article, I will explain how a Linux command looks like, what are options and values, as well as how to get help when wanting to do a specific task using a tool.

Prerequisites

Since this article talks about Linux commands, you must have a Linux installation to be used, it can be on your own Host PC, a Virtual Machine, or a server. I am currently using Ubuntu 22.04, but these concepts should be true regarding all Linux distributions.

First Part: The Tool

The first part of a Linux command references the tool you wish to use. Linux has many tools that come by default with most distributions, one of the most used tools is cp which is a tool that copies files and directories from one place to another. We will be using the cp tool to work with throughout this article.

When running a command, you first run that command:

cp

If you run this command by itself, you will get an error since the command is not getting any values, which we will be going through in a later section. This is what you will be getting in the output:

cp: missing file operand
Try 'cp --help' for more information.

Now that know the first part of running a command in Linux, we can move onto the values that can used in a command.

Second Part: The Values

Some tools allow you to add values onto which the command will perform the action on. In the case of cp it takes two values, SOURCE and DEST. The first value, SOURCE, allows you to choose the path of the file or directory that you wish to copy. The second value, DEST, allows you to choose where the file or directory will be saved, on which path.

For example, let us say we have a file called apple.txt in the Downloads directory and we want to copy it to the Documents directory, we will assume that they both are in the same root directory (i.e. the paths are /home/Downloads and /home/Documents). To perform this action, we would run:

cp /home/Downloads/apple.txt /home/Documents

Let us go through the command above:

Each tool has it’s own set of values that can be ran along with command. We will see later how we can identify these values. We can now move on to the last part of the Linux command, which are options.

Third Part: The Options

When running the command, there might be specific options that you wish to add to the command when necessary. These values are optional, meaning they are only added when needed. Continuing with our cp usage, I will use the option verbose to explain this section.

There are two main ways in which an option can be added to the command. The first way being the “normal way”, such as --verbose. This method takes two dashes -- then the relevant keyword, verbose in this case. The second way is referred to as the short-hand way, such as -v. This method takes a single dash - and a single letter, such as v (short for verbose). The difference between these two is that the second method is made for options that are commonly used when running a command. In our case, --verbose might be used often when running cp commands, which is why they created a short-hand method, to simply make it easier to add this option and make the command shorter. Other than that, there are no differences.

Another thing to keep in mind is that options are also values. When adding an option to a command, it takes a value along with it, but the value can either be a Boolean or a String. Options that are ran without any values afterwards are Boolean values. An option might be False by default, but once it is added to the command, it is added to turn that value to a True.

Options that take Strings are ran with the value afterwards. An example of this can be --filepath=/Documents (this isn’t a real option, just an example). These types of options allow you to configure the process to fit your specific needs, like if you need to run a command in a specific path or on a specific value.

This concludes how you can use a Linux command effeciently. Now we will look at how we can get help regarding the available values and options in a tool.

Knowing How To Use a Tool

There are two common ways to know how to use a tool, and to view all of it’s available values and options. The first way to simply attach the --help to the command, for example:

cp --help

When ran, the above command will display the usage of the tool, the mandatory values, and the available options. Running the command above should display:

Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
...

Note: it does not display the full output in order to fit it here

As you can see above, it outputs the main usage of the tool with the mandatory values:

Usage: cp [OPTION]... [-T] SOURCE DEST

As well as the other options that can be used:

  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
...

From here, you can know what are the methods in which you can use this tool effeciently.

The second way to know how to use a tool, is it view it’s man page. man is a tool in Linux that displays “Manuals” of a tool. Running man cp should display:

CP(1)                      				User Commands								CP(1)

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --archive
              same as -dR --preserve=all

Similar to the --help, it displays the usage, the mandatory options, as well as the available options.

You can choose to use either of these two methods to display the usage information of a specific tool. Some tools might have only of these two methods, and some other tools might display more information on one of these methods than it displays in the other method.

Conclusion

Hopefully you learned how to use a Linux command effeciently. This is one of the main steps you need to go through in order to learn how to Linux, which will lead you to knowing how to use many other tools, from Development applications like npm to DevOps systems like docker.

Thank you for reading. Hope you learned a thing or two. See you in another article. Best of luck!


Previous

Building a CLI Tool Using Go and Cobra-CLI

Next

Hashing vs Encryption
Sign Up To Binance To Get 10% Off Commission Fees Sign Up To Kucoin