Week 2. Tutorial: A session with Linux
2.1 Exploring your system environment
2.1.1 Viewing the system date and time
Users can display the system’s current date and time using the date command:
$ date Thu Feb 29 21:21:12 IST 2020
The date string above might look strange to you. It is the format originally chosen by the inventors of Unix and still the default format in most of the Unix-like systems. To print the date in a different format you need to give it an optional format string. Here are some examples:
$ date +%T 21:21:12 $ date +%Y 2021
Make a note of the timezone in the earlier example. You notice that in Unix the time is by default given with the time zone. IST stands for Indian Standard Time, which is 5 hours 30 minutes ahead of the UTC (Coordinated Universal Time). Typically a Unix system’s hardware clock is set to UTC. The system need to know its time zone setting from which it calculates the local time. If you carry a computer across time zones, you change the time zone, not the system time!
The designers of Unix have arbitrarily chosen the instant 00:00:00 UTC of 1 January 1970 as the origin of its system time, which they called the “epoch”. This has become a part of the Unix standard and is widely used. You can get this epoch time in seconds by running date +%s.
$ date +%s
1612160355
Unix systems rely much on accurate clock time and therefore almost always synchronized with a time server in the Internet. A normal user can not change the clock, only the system administrator can.
2.1.2 Monthly calenders
The standard calender program of Unix is called cal:
$ cal
January 2015
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
The program ncal offers you an alternative layout. Try cal -3 and ncal -3.
2.1.3 How busy is the machine
There is another time-related command called uptime which shows the duration for which the system has been running. In addition it tells you how many users are currently logged in and prints out three values for system load averages: the load average during the last 1, 5 and 15 minutes.
$ uptime
17:19:20 up 13:34, 6 users, load average: 0.22, 0.18, 0.15
The unit of measurement for load average is the number of CPUs (or hyper-threads) being utilized. If the machine is single CPU with only a single thread, 1.00 means the CPU is fully utilized. If it has 2 CPUs each with two hyper-threads each, a value of 4.00 means that all four CPU threads are being fully utilized.
There is another popular command called top which continuously monitors the system state:
$ top
17:52:46 up 1:42, 7 users, load average: 0.10, 0.09, 0.12
Tasks: 157 total, 2 running, 155 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.0 us, 1.1 sy, 0.0 ni, 97.8 id, 0.1 wa, 0.0 hi, ... KiB Mem: 7987388 total, 1825888 used, 6161500 free, 79584 buffers
KiB Swap: 0 total, 0 used, 0 free, 854744 cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2950 root 20 0 162m 22m 6884 S 5.0 0.3 2:49.21 Xorg 7829 foo 20 0 416m 34m 16m S 1.0 0.4 0:00.29 termin 6863 bar 20 0 468m 49m 14m S 0.7 0.6 1:06.15 evince [...]
Hint: There is an almost-graphical process viewer called htop. Try it!
2.1.4 System information
The system’s host name can be queried through the command hostname. The command uname prints system information like the CPU architecture and the operating system:
$ hostname
sece-codesys
$ uname −a
Linux sece-codesys 4.18.0-15-generic #16~18.04.1-Ubuntu SMP \
Thu Feb 7 14:06:04 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Another source of system information are the environment variables. This is an area in the memory space of the shell where a list of variables and their values are stored. One can query them with the echo command. The value of a variable is addressed by prepending its name by the $ sign:
$ echo $OSTYPE
linux−gnu
$ echo $SHELL
/bin/bash
Some other useful environment variables are: HOSTNAME, HOSTTYPE, TERM, USER. Try them!
Note that traditionally the environment variables are written in all capital.
2.2 Screen handling
2.2.1 Type-ahead
The terminal emulator reads the keys you type and immediately pass them to the computer at the other end. The keys are normally “echoed” back to the terminal unless they are suppressed as in the case of passwords. If the shell is busy with something else or the connection is very slow, you might not see the echo immediately. But you don’t need to worry. The key strokes are put to a waiting queue and the shell answers when it is back. Which means, you can type blindly ahead of the shell output.
You can think of the key strokes you type and the output of the shell as two independent (multiplexed) streams of characters. They do not get in to the way of the other!
2.2.2 Modifying the screen
If too much text is printed on terminal the clear command will clear it. Also watch the behaviour of the simple Enter key. In fact, it is a simple way of testing whether a shell is responding.
Programs may send special characters to the terminal to get various effects like colour or blinking text. Sometimes this leads to mishaps, where the display gets completely messed up. With the reset command you can recover a jumbled terminal. You may have to type the command blindly!
The original hardware terminals had fixed-sized fonts. Which meant that for a given resolution of the terminal the number of characters per line (called columns) and the number of lines displayed could not be changed: 80 columns times 24 lines was the standard. In today’s high resolution graphics one can display much more.
You can query the terminal size through the resize command.
$ resize
COLUMNS=92;
LINES=27;
export COLUMNS LINES;
2.2.3 Control keys
In dedicated terminals there was a BREAK key to stop whatever the command is running at that moment. Later this function was taken over by the Delete key. In today's keyboards the Delete key deletes the character under the cursor and Backspace the character to the left. Note that these things are system dependent. But a set of commands known as Control commands have a fairly consistent behaviour throughout many systems as they are part of the ASCII.
You can break a running program by typing Ctrl+C. To delete the character to the left, type Ctrl+H. Ctrl+U deletes the whole command line to the left of the cursor, Ctrl+K kills the part to the command line right of it. Ctrl+D signals the end of input, which will close the shell. Or, if you just want the output to pause, to keep something important disappearing off the screen, type Ctrl+S. To restart type Ctrl+Q.
The following table lists some of the more important Ctrl keys.
Key | ASCII control code | Effect |
Ctrl-A | SOH (start of header) | Go to the beginning of the line |
Ctrl-C | ETX (end of text) | Kill the current process |
Ctrl-D | EOT (end of transmission) | (on a line of its own) Exit the current shell (otherwise) delete the character under the cursor |
Ctrl-E | ENQ (enquiry) | Go to the end of the line |
Ctrl-H | BS (back space) | Delete the character to the left of the cursor (backspace) |
Ctrl-K | LF (line feed) | Delete from the cursor position to the end of line |
Ctrl-L | FF (form feed) | Clear the terminal |
Ctrl-R | DC2 (device control 2) |
Search command history |
Ctrl-U | ACK (acknowledge) | Delete from beginning of line to cursor position |
Ctrl-Z | CAN (cancel) |
Suspend a program |
2.2.3 Function of the extended keypad of the PC keyboard
Modern terminal emulators on PCs together with the Bash make use of the key extended keys like arrows, page up, etc. Here's is list of them. You are strongly suggested to make a habit out of using them, so as to get the most out of your Linux experience from the very beginning.
Key or key combination | Function |
---|---|
Left arrow / Right arrow | Move the cursor one place to the left or right on the command line |
Up arrow / Down arrow | Command history: Go to the previous line you entered, go to the line you typed after the line in the display |
Shift+PageUp / Shift+PageDown | Turn pages of the terminal output |
Tab / Tab Tab |
Command or filename completion, if unique / shows completion possibilities otherwise |
2.3 More about ending the session
In the assignment of the last week we said that the command logout is the right way to log out. Strictly speaking this is not right. You can log out only from the very first shell to which you logged in. You could have started more shells on top of it. You start new shells one over the other by typing their names. Try for example bash and dash, names of available shells. Before you log out, you need to close those shells. The proper way to do that is to type Ctrl+D or exit. The “exit” from the last shell will automatically logs you out!
Sources: