Beginners Guide to using the command-line syntax in UNIX/Linux

Constructing and Executing Commands From a Command Line

You can use the UNIX system commands to instruct the system to perform specific tasks. You can perform commands on the command line in a terminal window. Command-line commands can exist with or without options and arguments. The command syntax is the structure and order of the components: command name, options, and arguments.

NOTE: Commands in the UNIX environment are case sensitive.

Command-Line Syntax

You can change the behavior of command functions by using options and arguments. Table below describes these components of a command.

Item Description
command Specifies what the system does (executes).
option Specifies how the command runs (a modifier). Options start with a dash (-) character.
argument Specifies what is affected (a file, a directory, or text).
# command options arguments

Using Commands

Some examples of basic commands are uname, date, cal, and clear.

The uname command provides information about the system. By default, when you perform the uname command, the name of the current operating system appears. To display the operating system information, perform the command:

$ uname
Linux

The date command displays the system’s current date and time. To display the date and time, perform the command:

$ date
Tue Dec 15 14:31:55 GMT 2019

The cal command displays a calendar for the current month and year. To display the calendar, perform the command

$ cal
   December 2019
S  M Tu  W Th  F  S
         1  2  3  4
5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

To clear the terminal window, perform the command:

$ clear

Using Commands With Options

Adding options to a command alters the type of information displayed. Command options are case sensitive. You can use more than one option with a command. You can list multiple options separately or combined after one dash (-).

The following examples show the uname command with two options. The -i option shows the name of the hardware platform, and the -n option prints the hostname of the local system. The following example shows the use of the uname command with separate options:

$ uname -i
x86_64
$ uname -n
host1

The -s option shows the name of the operating system. The -r option shows the operating system release level. The following example shows the uname command with two separate options:

$ uname -s -r
Linux 4.18.0-147.0.3.el8_1.x86_64

The following example shows the uname command with two combined options:

$ uname -rs
Linux 4.18.0-147.0.3.el8_1.x86_64

The following example of the uname command with the -a option shows information currently available from the system:

$ uname -a
Linux geek.mylabserver.com 4.18.0-147.0.3.el8_1.x86_64 #1 SMP Mon Nov 11 12:58:36 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Using Commands With Arguments

Arguments enable you to define exactly what you want to do with a command. The following example shows the cal command with two arguments. The first argument, 12, specifies the month to be viewed. The second argument, 2004, specifies the year to be viewed. Arguments enable you to define exactly wh

$ cal 12 2020
    December 2020
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Using Commands With Options and Arguments

The following examples show the ls command, the ls command with an option, the ls command with an argument, and the ls command with an option and an argument. The ls command lists the files in a directory. The -l option provides additional information about the files. The filename argument specifies the file to be viewed.

$ ls
dante dir2 dir5 file.3 file3 fruit2
dante_1 dir3 file.1 file1 file4 practice
dir1 dir4 file.2 file2 fruit tutor.vi
$ ls -l
total 90
-rw-rw----   1 user1    staff       1319 Mar 14 10:12 dante
-rw-rw----   1 user1    staff        368 Mar 14 10:12 dante_1
drwxr-xr-x   5 user1    staff        512 Mar 14 10:12 dir1
drwxr-xr-x   4 user1    staff        512 Mar 14 10:12 dir2
drwxr-xr-x   3 user1    staff        512 Mar 14 10:12 dir3
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 dir4
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 dir5
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.1
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.2
-rw-rw----   1 user1    staff          0 Mar 14 10:12 file.3
-rw-rw----   1 user1    staff       1610 Mar 14 10:12 file1
-rw-rw----   1 user1    staff        105 Mar 14 10:12 file2
-rw-rw----   1 user1    staff        218 Mar 14 10:12 file3
-rw-rw----   1 user1    staff        137 Mar 14 10:12 file4
-rw-rw----   1 user1    staff         57 Mar 14 10:12 fruit
-rw-rw----   1 user1    staff         57 Mar 14 10:12 fruit2
drwxr-xr-x   2 user1    staff        512 Mar 14 10:12 practice
-rwx--x--x   1 user1    staff      28738 Mar 14 10:12 tutor.vi
$ ls dante
dante
$ ls -l dante
-rw-rw----   1 user1    staff       1319 Mar 14 10:12 dante

Entering Multiple Commands on a Single Command Line

You can enter multiple commands on a single command line by using a semicolon (;) to separate each command. The shell recognizes the semicolon as a command separator. The shell executes each command from left to right when you press Return. The command format for multiple commands is:

command option argument;command option argument

The following example shows two commands separated by a semicolon:

$ date; uname
Mon May 25 17:45:36 UTC 2020
Linux

The following example shows three commands separated by a semicolon. The cal command has two arguments, followed by the date command, and the uname command with two options:

$ cal 12 2020; date; uname -rs
    December 2020
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Mon May 25 17:46:13 UTC 2020
Linux 4.18.0-147.0.3.el8_1.x86_64