Linux Command line Basics – Executing commands from the command line

You can use system commands on the command line to instruct the system to perform specific tasks. The commands are received into a terminal window. This post is a very basic introduction to using commands with options and/or arguments and also how to run multiple commands on the command line.

Note: UNIX commands are case-sensitive.

Command-Line Syntax

– The command syntax is the structure and order of the command components: name, options, and arguments.
– Command-line commands can exist with or without options and arguments.
– You can change the behavior of commands by using a combination of options and arguments.
– The table shown below describes the components of a command.

Item Description
Command Specifies what the system does (an executable)
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)
Note: Most Linux commands also use a double hyphen (–) for their command-line switches.

Using UNIX Commands

Some examples of basic commands are uname, date, cal, and clear. The uname command provides information about the system. By default, when you use the uname command, the name of the current operating system appears.

1. To display the operating system information, enter:

# uname
Linux

2. To display the date and time, enter:

# date
Sat May 12 07:02:24 UTC 2018

3. To display the calendar, enter:

# cal
      May 2018
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

4. To clear the terminal window, enter:

# clear

Using Commands with Options

– Adding options to a command alters the information displayed.
– You can use more than one option with a command.
– You can also list multiple options separately or they can be combined after a dash (-).
– Use of a dash (-) preceding an option is command specific. Also, options are command specific.

Note: For additional information and proper usage of options, check the appropriate man page for the command.

The given example shows the uname command with two options:

  • The -i option displays the name of the hardware platform.
  • The -n option prints the host name of the local system.
# uname -i
x86_64
# uname -n
geeklab1.mylabserver.com

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

# uname -sr
Linux 3.10.0-693.21.1.el7.x86_64

The following example shows the uname command with the -a option, which displays the current system information.

# uname -a
Linux geeklab1.mylabserver.com 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Using Commands with Arguments

Arguments enable you to additionally define the output from a command. The following example shows the cal command with two arguments, 05 and 2018.

  • The first argument, 05, specifies the month.
  • The second argument, 2018, specifies the year.
# cal 05 2018
      May 2018
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 without an option, with an option, with an argument, and with an option and argument together.

# ls
file1  file2  file3  file4  file5  file6
$ ls -l
total 0
-rw-rw-r--. 1 user user 0 May 12 07:43 file1
-rw-rw-r--. 1 user user 0 May 12 07:43 file2
-rw-rw-r--. 1 user user 0 May 12 07:43 file3
-rw-rw-r--. 1 user user 0 May 12 07:43 file4
-rw-rw-r--. 1 user user 0 May 12 07:43 file5
-rw-rw-r--. 1 user user 0 May 12 07:43 file6
# ls file1
file1
# ls -l file1
-rw-rw-r--. 1 user user 0 May 12 07:43 file1

In the examples, 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.

Using Multiple Commands on the Command Line

You can enter multiple commands on a single command line by using a semicolon (;) to separate each command.

# command option argument;command option argument

The shell recognizes the semicolon (;) as a command separator. The following example shows three commands separated by semicolons. The cal command has two arguments, followed by the date command, and the uname command with two options.

# cal 05 2018; date; uname -rs
      May 2018
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

Sat May 12 07:48:02 UTC 2018
Linux 3.10.0-693.21.1.el7.x86_64

The shell executes each command from left to right when you press Enter/Return.

Related Post