Understanding the Structure of a Command
A handy thing to know for both real-life and any certification exams that you may take, is the structure of a command. Commands can consist of up to three parts, and there’s a certain order for the parts. Here are the parts and the order in which you’ll normally place them:
- The command itself
- Command options
- Command arguments
If you plan to take a Linux certification exam, you’ll definitely want to remember this ordering rule. Later on though, we’ll see that some commands don’t always follow this rule.
Using Command Options
There are two general types of option switches:
- Single-letter options: For most commands, a single-letter option is preceded by a single dash. Most of the time, two or more single-letter options can be combined with a single dash.
- Whole-word options: For most commands, a whole word option is preceded by two dashes. Two or more whole-word options must be listed separately, because they can’t be combined with a single pair of dashes.
To show you what we mean, check out this hands-on lab.
Hands-on Lab – Practice With Command Options
In this lab, we’ll be working with the humble ls
utility. Options and arguments are optional for this utility, so we’ll get to see the different configurations for the command in this hands-on practice.
- Let’s issue the naked
ls
command in order to see the files and directories that are in our current directory.[donnie@fedora ~]$ ls 4-2_Building_an_Alpine_Container.bak Public 4-2_Building_an_Alpine_Container.pptx pwsafe.key addresses.txt python_container alma9_default.txt rad-bfgminer alma9_future.txt ramfetch alma_link.txt read.me.first . . . . . . pCloudDrive yad-form.sh Pictures [donnie@fedora ~]$
- Now, let’s add a single-letter option. We’ll use the
-l
option to show the files and directories with some of their characteristics.[donnie@fedora ~]$ ls -l total 40257473 -rw-r--r--. 1 donnie donnie 754207 Apr 5 16:13 4-2_Building_an_Alpine_Container.bak -rw-r--r--. 1 donnie donnie 761796 Apr 8 14:49 4-2_Building_an_Alpine_Container.pptx -rw-r--r--. 1 donnie donnie 137 Apr 2 15:05 addresses.txt -rw-r--r--. 1 donnie donnie 1438 Nov 2 2022 alma9_default.txt . . . . . . -rwxr--r--. 1 donnie donnie 263 May 16 15:42 yad-form.sh [donnie@fedora ~]$
- Use the
ls
command with the-a
option to see any hidden files or directories. (Hidden files or directories have names that begin with a period.)[donnie@fedora ~]$ ls -a . .pcloud .. pCloudDrive 4-2_Building_an_Alpine_Container.bak Pictures 4-2_Building_an_Alpine_Container.pptx .pki addresses.txt .podman-desktop alma9_default.txt .profile . . . . . . .mozilla .Xauthority Music .xscreensaver NetRexx .xsession-errors nikto yad-form.sh [donnie@fedora ~]$
- Next, let’s combine the two options, so that we can see the characteristics of both the hidden and unhidden files and directories:
[donnie@fedora ~]$ ls -la total 40257561 drwx------. 1 donnie donnie 2820 Jul 25 13:53 . drwxr-xr-x. 1 root root 12 Aug 9 2022 .. -rw-r--r--. 1 donnie donnie 137 Apr 2 15:05 addresses.txt -rw-------. 1 donnie donnie 15804 Jul 24 17:53 .bash_history -rw-r--r--. 1 donnie donnie 18 Jan 19 2022 .bash_logout -rw-r--r--. 1 donnie donnie 194 Apr 3 12:11 .bash_profile -rw-r--r--. 1 donnie donnie 513 Apr 3 12:11 .bashrc . . . . . . -rw-r--r--. 1 donnie donnie 9041 Feb 4 12:57 .xscreensaver -rw-------. 1 donnie donnie 0 Jul 25 13:53 .xsession-errors -rwxr--r--. 1 donnie donnie 263 May 16 15:42 yad-form.sh [donnie@fedora ~]$
In the preceding examples, the donnie donnie
part indicates that the files and directories belong to user donnie
and are associated with the donnie
group. In this example, we’re using a whole-word option, --author
, preceded by two dashes, to view some extra information. Let’s use this --author
switch and the -l
switch together to see who authored these files:
[donnie@fedora ~]$ ls -l --author
total 40257473
-rw-r--r--. 1 donnie donnie donnie 137 Apr 2 15:05 addresses.txt
-rw-r--r--. 1 donnie donnie donnie 1438 Nov 2 2022 alma9_default.txt
-rw-r--r--. 1 donnie donnie donnie 1297 Nov 2 2022 alma9_future.txt
. . .
. . .
rwxr--r--. 1 donnie donnie donnie 263 May 16 15:42 yad-form.sh
[donnie@fedora ~]$
So, it appears that that Donnie character also created the files in the first place. (Oh, that’s me, isn’t it?)
Using Command Arguments
An argument is an object upon which a command will operate. For the ls
command, an argument would be the name of a file or directory. For example, let’s say that we want to see the details of just a certain file. We can do something like this:
[donnie@fedora ~]$ ls -l yad-form.sh
-rwxr--r--. 1 donnie donnie 263 May 16 15:42 yad-form.sh
[donnie@fedora ~]$
We can use the *
wildcard to see details of all files of a certain type, like so:
[donnie@fedora ~]$ ls -l *.sh
-rwxr--r--. 1 donnie donnie 116 May 16 15:04 root.sh
-rwxr--r--. 1 donnie donnie 263 May 16 15:42 yad-form.sh
[donnie@fedora ~]$
If you’re not familiar with the concept of wildcards, think of them as a way to perform pattern-matching. In the above example, the *
wildcard is used to match one or more characters. For this reason, the ls -l *.sh
command allows us to see all files with the .sh filename extension. You can also use this wildcard in other ways. For example, to see all filenames and directory names that begin with the letter w, just do this
donnie@opensuse:~> ls -ld w*
drwxrwxr-x 1 donnie users 22 Mar 5 2022 windows
-rw-r--r-- 1 donnie users 82180 Dec 7 2019 wingding.ttf
drwxr-xr-x 1 donnie users 138 Mar 11 2023 wownero-x86_64-linux-gnu-v0.11
donnie@opensuse:~>
F
or more information about wildcards, check out the reference in the Further Reading section.
In this case, we’re looking at all files whose names end in .sh
.
You’re not always limited to specifying just one argument. In this example, we’re looking at three different files:
[donnie@fedora ~]$ ls -l missing_stuff.txt yad-form.sh Dylan-My_Back_Pages-tab.odt
-rw-r--r--. 1 donnie donnie 29502 Mar 7 18:30 Dylan-My_Back_Pages-tab.odt
-rw-r--r--. 1 donnie donnie 394 Dec 7 2022 missing_stuff.txt
-rwxr--r--. 1 donnie donnie 263 May 16 15:42 yad-form.sh
[donnie@fedora ~]$
Use the -ld
option to view the characteristics of a directory without viewing the contents of the directory, like so:
[donnie@fedora ~]$ ls -ld Downloads/
drwxr-xr-x. 1 donnie donnie 8100 Aug 4 12:37 Downloads/
[donnie@fedora ~]$
Although you can actually change the order in which options and arguments appear in many commands, it’s bad practice to do so. To avoid confusion and to prepare yourself for any Linux certifications exams that you might take, just follow the ordering rule that I’ve presented here. That is, the command itself, command options, and lastly, the command arguments.
That about does it for the command structure part. Let’s move on to see how to execute multiple commands at once.