Commands are the backbone of the Linux operating system. There are thousands of commands in different categories and remembering all the commands in impossible. What we really need is to understand the syntax of the command well and the components that make up a command string.In this chapter, we will look into the command syntax, the components that make up various commands that we use on a daily basis. Understanding the structure of the command makes learning and using new commands easier.
The Unix Philosophy is all about simplicity and reusability. The commands are there to do only one thing and do it really well. There are features like redirection that helps us use multiple commands together to solve complex problems however the commands themselves focus on providing solution for a single problem.
Some commands appear before we cover them in detail. These commands serve as supplement to demonstrate other commands. For example the
lscommand that is used to list files and directories. We will see these commands in detail at the later sections
Let us look at couple of examples first
The echo command can be used to display text on the screen. The command accepts sequence of characters as argument and display it on the screen.
$ echo "Hello World"
Hello Word
The date command displays current date and time on the screen. We can modify the default format by passing date format string as argument
# date: default format
$ date
Fri Jul 2 15:05:19 IST 2021
# date: display date in YYYY-MM-DD format
$ date +"%Y-%m-%d"
2021-07-02
A command contains one or more words that are separated by spaces, which will be executed when we press the enter/return key . The first word is the verb that refers the action followed by zero or more words that are called options or argumets. The basic syntax of a command is as follows
$ command [option(s)] [argument(s)]
We usually code options after the command followed by the arguments.
Arguments are entities (usually files, text or numbers) that the command will act on. Lot of commands have default argument. For example, the ls command that is used to list files and directories accepts a directory path as argument. If we don’t provide one, it will take the current working directory as the default argument.
Option, also called as flag can modify the behavior of the command. For example, most of the commands will not display any message when they get executed successfully. We use option to turn on the verbosity of the command.
Options can have their own arguments too. For example: the awk command’s option -F, uses , as an argument (field delimiter) and grep command’s --color=always uses always to highlight searched text.
Options are coded with a leading - or --. The ones with - are usually followed by a single character; -v for example and the ones with -- are followed by multiple characters; --verbose for example.
The
findcommand is an exception to the way-and--are used. Its options use-followed by a word;-type,-user,-atimefor example
We can merge multiple single character options and code with one leading -; for example mkdir -p -v data/input or mkdir -pv data/input.
Each command supports set of options and usually there can be both - and -- versions. We can use the - version for our day-to-day use on the shell whereas the -- can be used in the shell scripts that works as a self-documentation. For example, the mkdir command that creates directories can use -v or --verbose to, you guessed it right, turn on the verbosity of the command.
verbosity: the quality of using more words than needed; ..just like this article.
Since several people contributed to the development of UNIX, there are cases where an option has different meaning for different commands. For example -v means verbose mostly, it also means version for some commands and inverse match in the grep command. On the other hand, same functionality would use different option across commands. For example, -d, -t, -s and -F are used to provide field separators to the cut, sort, column and awk commands respectively
Finally, since - has a special meaning, if we want to use - as a part of argument, we need to simply code -- to mark the end of the options.
To summarize, Options
- or -- prefix- prefix is followed by a single character and we can code multiple options with one leading - or individually; -pv or -p and -v-- prefix is followed by a multi-character option name; --verbose, --all, …-- signals end of options and whatever is passed after the -- will be treated as argumentThis section covers the basic commands that we will encounter when we start learning the command line interface. These commands are there to get information about the system such as current date, current working directory, clear screen etc..
These commands gives us a good start to get ourselves comfortable using the shell / CLI
| # | Name | Description |
|---|---|---|
| 1 | echo <STR> |
print the string on the screen |
| 2 | pwd |
print current working directory |
| 3 | date |
print current date and time |
| 4 | clear |
clear the screen, use the CTRL-L key as alternate |
| 5 | sleep <N> |
sleeps for N seconds |
| 6 | cal |
displays current month’s calendar in tabular format. |
| 7 | man CMD |
displays help text for the CMD passed as argument. |
| 8 | history |
display the list of commands ran so far |
| 9 | passwd |
change password |
| 10 | seq |
create number sequence |
| 11 | exit |
terminate session |
echo STR: displays text on screenBy default, the echo command simply displays the argument on the screen. If we provide a shell variable, it will substitute the value instead.
| Option | Description |
|---|---|
-n |
suppresses newline |
-e |
interprets escape sequences like \n, \t` etc |
# displays text and adds newline
$ echo "Hello World
Hello World
$
# displays a new line if there are no arguments
$ echo
$
# displays text; '-n' suppresses new line
$ echo -n "Hello World
Hello World$
# interprets tab and newline
$ echo -n -e "Hello\tWorld\n"
Hello World
$
# display value of $USER: shell variable
$ echo "Hello, $USER"
Hello, mk
pwd : print working directorypwd is one of the simplest commands we will encounter in Linux. It simply displays current directory you are in. When you are in the shell, you will be in your home directory
$ pwd
/home/mk
date : Set / Display Current Date and TimeBy default, it prints the date in ddd, dd mmm yyyy HH:MM:SS. AM|PM TZ format. If we have admin access, we can set the date and time. Here is the basic syntax.
$ date +"format string"
We can use format strings to print date in several formats
| Format String | Meaning | Value(s) |
|---|---|---|
| %d | Current Date | 01-31 |
| %m | Current Month | 01-12 |
| %y | Current Year 2 digits | 00-99 |
| %Y | Current Year 4 digits | 1999, 2020 etc.. |
| %H | Hour | 00-23 |
| %M | Minutes | 00-59 |
| %S | Seconds | 00-59 |
| %s | Seconds since 1-Jan-1979 | seconds as integer |
For complete reference, please refer the manual pages for the date command; man date
$ date
Sat 03 Jul 2021 03:59:27 PM UTC
$ date +"%Y%m%d"
20210703
$ date +"%Y%m%d%H%M%S"
20210703155949
$ date +"%Y-%m-%d"
2021-07-03
$ date +"%F"
2021-07-03
clear : Clears the Screen !!The clear command cleans out the display and put the command prompt $ on the first line of the terminal. Based on the terminal settings, you may or may not scroll back. In some emulators, clear may not work. We can use Control key + l CTRL-L to clear out the screen
sleep N : sleep for N secondsThe sleep command blocks the terminal for N seconds and return the command prompt back to the user. It can be used in wait and watch scenarios. For example, wait for 5 seconds and lookup a file in a directory. While learning, we can use sleep to simulate the effect of a long running command.
We can run commands one by one or submit the commands at the same time using semicolon
;as delimiter
$ date;echo "Start Sleep";sleep 5; date
Sat 03 Jul 2021 04:09:44 PM UTC
Start Sleep
Sat 03 Jul 2021 04:09:49 PM UTC
cal : displays calendarThe cal command displays days of current month in tabular format with current date highlighted. We can pass month and year or year as argument.
$ cal
July 2021
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
$ cal dec 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
man CMD : display manual pages for a commandThe man command is used to access the manual pages for commands and system calls. It displays one screenful of documentation at a time and we can use the following sub-commands to navigate the man pages.
f or spacebar : go to the next pageb : go to previous pageenter key or down arrow : scroll next lineup arrow : scroll previous lineq : quit man pagehistory : displays command historyLinux keeps track of the commands executed across various login sessions. By default, it will store the last 500 commands. We can use the history command to list the previously executed commands along with the line number. There are some shell expansions to rerun or just print any of these commands.
$ history
1 echo "Hello World"
2 date
3 pwd
4 clear
5 cal
...
We can rerun the last command by using !! characters. If we use !!:p, shell just prints the last command instead of running it
$ date
Sat 03 Jul 2021 04:37:37 PM UTC
$ !!
date
Sat 03 Jul 2021 04:37:39 PM UTC
The !N can be used to execute the Nth command in the history and !N:p prints the Nth command instead of running it
$ !10
date
Sat 03 Jul 2021 04:40:10 PM UTC
$ !10:p
date
there is more to history expansion, we shall look into that later
passwd : change passwordThe passwd command is used to change the existing password. This command is useful to proactively change the password in a multi-user server that has password expiry policy. The command will prompt for the current password. If correct password is provided, it will ask for new password and once reenter the new password again, it will change the password
$ passwd
Changing password for mk.
Current password:
New Password:
Retype Password:
passwd: password updated successfully.
seq : print number sequenceThe seq command can be used to generate number sequence. It requires one numeric argument as stop value and it generates numbers between 1 and stop value by increment of one. We can also provide start and step values. The number can be integers or decimals
The -w option pads smaller numbers with leading zeros and -f "FORMAT STR" can be used format numbers like printf
# start at 1 (default), increment by 1 (default), stop at 5
$ seq 4
1
2
3
4
# start at 2, increment by 1 (default), stop at 5
$ seq 2 4
2
3
4
# start at 2, increment by 2, stop at 5
$ seq 2 2 5
2
4
exit : exit current processThe exit command terminates the current process. If it is entered in the command prompt, it will terminate the session. We can also use the logout command
Extras: Why use - and -- as option prefix
As far as I understood, the earlier commands had - with single-character options first and since multiple - options can be combined together (-p, -v vs -pv), the developers might have needed a different syntax for multi-character options and used -- for that. For example, the ls command has --all option to include hidden files in the output, if we code -all then ls will take it as -a, -l and -l. Confusing ?? Try ls -all and ls --all