Unit 2: System Utilities and Basic Commands

CSE105 — Creative Engineering Workshop 7 min read

The Unix/Linux command line (originating with Unix at Bell Labs, 1969) exposes the operating system through small, single-purpose programs invoked by name at a shell prompt. This unit covers the anatomy of a command and a family of utilities for reporting time, printing text, doing arithmetic, identifying the user and machine, controlling the terminal, and reading documentation.

  • Shell: The program (commonly bash) that reads a typed line, splits it into words, expands wildcards and variables, then locates and runs the named program.
  • Utility: A standalone executable (e.g. /bin/date) found via the PATH variable; the shell searches these directories left to right.
  • Exit status: Every command returns an integer, 0 for success and non-zero for failure, held in $?.
  • Streams: Each command has standard input (fd 0), standard output (fd 1) and standard error (fd 2), redirectable with <, >, 2>.
  • Case sensitivity: Command and option names are case-sensitive; Date is not date.

II. Command Structure

The grammar of a command line

A command line is an ordered sequence: the command name, its options, and its arguments, separated by whitespace.

  • General form: the canonical layout the shell parses.
    TEXT
    command [options] [arguments]
  • Command name: the first word; the executable to run, e.g. ls.
  • Options (flags): modify behaviour, prefixed by - (short) or -- (long): -l, --all. Short options combine: ls -la equals ls -l -a.
  • Arguments (operands): the objects acted on, such as filenames or text: in cp file1 file2, both are arguments.
  • Option arguments: some options take their own value: head -n 5 file where 5 belongs to -n.
  • Terminator: -- signals "no more options", so filenames beginning with - are read as arguments.

III. cal and date

Reporting the calendar and clock

These utilities read the system's notion of date and time and format it for display.

A. cal

Prints a formatted calendar for a month or year.

  • No argument: shows the current month with today's date highlighted.
  • Month and year: cal 9 2026 prints September 2026; order is month then year.
  • Whole year: cal 2026 prints all twelve months.
  • Common option: cal -3 shows previous, current and next month together.

B. date

Displays or sets the system date and time.

  • Default: date prints e.g. Fri Sep 25 21:33:13 UTC 2026.
  • Format strings: a leading + introduces % codes:
    TEXT
    date "+%Y-%m-%d %H:%M:%S"

    where %Y=4-digit year, %m=month, %d=day, %H=hour (24h), %M=minute, %S=second.
  • Setting the clock: date -s "2026-09-25 21:00" requires superuser privilege.

IV. echo and printf

Sending text to standard output

Both write text to stdout; they differ in formatting control.

A. echo

Prints its arguments followed by a newline.

  • Basic use: echo Hello World outputs Hello World.
  • Variable expansion: echo $HOME prints the value the shell substitutes, e.g. /home/user.
  • Escape sequences: echo -e "a\tb" inserts a tab when -e is given; -n suppresses the trailing newline.

B. printf

Formats output using a C-style format string, giving precise control echo lacks.

  • Format and arguments: the first operand is a template with conversion specifiers.
    TEXT
    printf "%-10s %05.2f\n" "Item" 3.5

    %-10s=left-justified string in 10 columns, %05.2f=float, 2 decimals, zero-padded to width 5, \n=newline.
  • No automatic newline: unlike echo, you must supply \n yourself.
  • Reuse of format: if more arguments than specifiers exist, the format string repeats.

V. bc and expr

Command-line arithmetic

Both evaluate arithmetic, but at different scales of precision.

  1. bc — an arbitrary-precision calculator language reading from stdin.
    • Piped input: echo "3.5 * 2" | bc yields 7.0.
    • Decimal scale: floating-point needs scale: echo "scale=4; 10/3" | bc gives 3.3333.
    • Interactive mode: bc -l loads the math library (sine, log) and defaults scale to 20.
  2. expr — an integer-only expression evaluator that prints its result.
    • Whitespace mandatory: expr 4 + 5 prints 9; 4+5 is treated as one string.
    • Metacharacter escaping: multiplication must be quoted: expr 4 \* 5.
    • String functions: expr length "hello" returns 5.
  • Contrast: bc handles fractions and large numbers; expr is limited to integers and returns exit status 1 when a result is 0 or empty.

VI. script and passwd

Session recording and credential management

A. script

Records everything shown in a terminal session to a file for later review.

  • Purpose: captures both input and output verbatim, useful for demonstrations or logs.
  • Start: script session.log begins recording into session.log; with no name it uses typescript.
  • Stop: typing exit or Ctrl-D ends the session and writes the file.
  • Timing: script -t can record timing data to replay the session at original speed.

B. passwd

Changes a user's login password.

  • Self-service: passwd prompts for the current password, then the new one twice.
  • Other accounts: passwd username changes another user's password, permitted only to the superuser.
  • Storage: hashes live in /etc/shadow, not the world-readable /etc/passwd.
  • Policy: enforces minimum length and complexity rules set by the administrator.

VII. who, whoami, uname and uname -a

Reporting user and system identity

These commands answer "who is logged in" and "what machine is this".

A. who

Lists users currently logged into the system.

  • Output columns: username, terminal line (tty1, pts/0), login time, and remote host.
  • Current session: who am i (two words) prints only the invoking session's line.

B. whoami

Prints the effective username of the current user.

  • Single value: outputs just the name, e.g. user.
  • After privilege change: following sudo, whoami reflects the elevated identity, root.

C. uname

Prints selected system information, by default the kernel name.

  • Default: uname prints Linux.
  • Selective flags: -s=kernel name, -n=network hostname, -r=kernel release, -m=machine hardware (e.g. x86_64).

D. uname -a

Prints all available system information in one line.

  • Combined output: kernel name, hostname, kernel release, kernel version, machine architecture and operating system together.
  • Use: the quickest single command to characterise an unknown host for support or scripting.

VIII. uptime, tty and stty

System load and terminal control

A. uptime

Reports how long the system has been running and its current load.

  • Fields: current time, elapsed time since boot, number of logged-in users, and load averages.
  • Load averages: three numbers for the last 1, 5 and 15 minutes, e.g. 0.15, 0.10, 0.05; a value near the CPU count means a fully busy machine.

B. tty

Prints the filename of the terminal connected to standard input.

  • Interactive shell: returns a path such as /dev/pts/0.
  • Non-terminal input: when input is piped or redirected it prints not a tty and exits non-zero.

C. stty

Displays or changes terminal line settings.

  • Show settings: stty -a lists every current setting (baud rate, control characters).
  • Change a key: stty erase ^H reassigns the backspace/erase character.
  • Toggle echo: stty -echo hides typed characters (for password entry); stty echo restores them.

IX. man command and manual page navigation

Reading the built-in documentation

The manual pages are the authoritative reference bundled with each utility.

A. man command

Displays the manual page for a given command.

  • Basic use: man date opens the documentation for date.
  • Numbered sections: manuals are grouped — 1=user commands, 2=system calls, 5=file formats, 8=admin commands. man 5 passwd shows the file format, not the command.
  • Keyword search: man -k copy (equivalent to apropos) lists pages whose descriptions mention "copy".
  • Standard layout: each page follows NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXAMPLES, SEE ALSO.

B. manual page navigation

Manual pages are viewed through the less pager, controlled by single keystrokes.

  • Movement: Space/f=forward one screen, b=back one screen, arrow keys=line by line, g=top, G=end.
  • Searching: /term searches forward, ?term backward; n repeats the search, N reverses it.
  • Help and exit: h opens the pager's help, q quits and returns to the shell prompt.