Unit 2: System Utilities and Basic Commands
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 thePATHvariable; the shell searches these directories left to right. - Exit status: Every command returns an integer,
0for 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;
Dateis notdate.
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.
TEXTcommand [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 -laequalsls -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 filewhere5belongs 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 2026prints September 2026; order is month then year. - Whole year:
cal 2026prints all twelve months. - Common option:
cal -3shows previous, current and next month together.
B. date
Displays or sets the system date and time.
- Default:
dateprints e.g.Fri Sep 25 21:33:13 UTC 2026. - Format strings: a leading
+introduces%codes:
TEXTdate "+%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 WorldoutputsHello World. - Variable expansion:
echo $HOMEprints the value the shell substitutes, e.g./home/user. - Escape sequences:
echo -e "a\tb"inserts a tab when-eis given;-nsuppresses 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.
TEXTprintf "%-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\nyourself. - 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.
- bc — an arbitrary-precision calculator language reading from stdin.
- Piped input:
echo "3.5 * 2" | bcyields7.0. - Decimal scale: floating-point needs
scale:echo "scale=4; 10/3" | bcgives3.3333. - Interactive mode:
bc -lloads the math library (sine, log) and defaults scale to 20.
- Piped input:
- expr — an integer-only expression evaluator that prints its result.
- Whitespace mandatory:
expr 4 + 5prints9;4+5is treated as one string. - Metacharacter escaping: multiplication must be quoted:
expr 4 \* 5. - String functions:
expr length "hello"returns5.
- Whitespace mandatory:
- Contrast:
bchandles fractions and large numbers;expris limited to integers and returns exit status1when a result is0or 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.logbegins recording intosession.log; with no name it usestypescript. - Stop: typing
exitor Ctrl-D ends the session and writes the file. - Timing:
script -tcan record timing data to replay the session at original speed.
B. passwd
Changes a user's login password.
- Self-service:
passwdprompts for the current password, then the new one twice. - Other accounts:
passwd usernamechanges 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,whoamireflects the elevated identity,root.
C. uname
Prints selected system information, by default the kernel name.
- Default:
unameprintsLinux. - 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 ttyand exits non-zero.
C. stty
Displays or changes terminal line settings.
- Show settings:
stty -alists every current setting (baud rate, control characters). - Change a key:
stty erase ^Hreassigns the backspace/erase character. - Toggle echo:
stty -echohides typed characters (for password entry);stty echorestores 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 dateopens the documentation fordate. - Numbered sections: manuals are grouped — 1=user commands, 2=system calls, 5=file formats, 8=admin commands.
man 5 passwdshows the file format, not the command. - Keyword search:
man -k copy(equivalent toapropos) 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:
/termsearches forward,?termbackward;nrepeats the search,Nreverses it. - Help and exit:
hopens the pager's help,qquits and returns to the shell prompt.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →