UNIX Commands
1 3rd Januaryl, 2025
Contents
Shell Introduction
Command Format
Shell I/O
Command I/O
Command Overview
2 3rd Januaryl, 2025
Shell Introduction
After logging in, Linux/Unix starts another program
called the shell.
Users communicate with operating system
through the shell
A shell is a system program that allows a user to
execute
i. shell functions (internal commands)
ii. other programs (external commands)
iii. shell scripts
3 3rd Januaryl, 2025
.
The shell interprets commands that the
user types in and manages their execution.
The shell communicates with the internal part of the
operating system called the kernel
The most popular shells are: tcsh, csh, korn, and bash
Shell commands are case sensitive(normally lower
case)
4 3rd Januaryl, 2025
Command Format
Format: command name and zero or more
arguments:
% commandname [arg1] ... [argN]
By % sign I mean prompt here and hereafter.
Arguments can be
options (switches to the command to indicate a
mode of operation) ; usually prefixed with a
hyphen (-).
non-options, or operands, basically the data to
work with (actual data, or a file name)
Exercise
5 3rd Januaryl, 2025
Shell I/O
The Shell is an interface, so the user interacts
with the shell by typing in the commands.
The shell interprets the commands, that may
produce some results, and the control is given
back to the user when a command completes (in
general).
In the case of external commands, shell executes
actual programs that may call functions of the OS
kernel.
These system commands are often wrapped
around a so-called system calls, to ask the kernel
to perform an operation (usually privileged) on
your behalf.
6 3rd Januaryl, 2025
Command I/O
Input to shell:
Command name and arguments typed by the user
Input to a command can be obtained from any of the following:
Keyboard, file, or other commands
Standard input: keyboard.
Standard output: screen.
These STDIN and STDOUT are often together referred to as a
terminal.
Both standard input and standard output can be redirected
from a file, to a file or other command.
File redirection:
< input
> output
>> output append
7 3rd Januaryl, 2025
Commands
8 3rd Januaryl, 2025
man
Manual Pages
Whenever you need help with a command
type “man” with the command name, e.g
man ls.
man is the first command to remember
Contains information about almost
everything
other commands
system calls
c/library functions
other utilities, applications, configuration files
To read about man itself type:
% man man
9 3rd Januaryl, 2025
which
Displays a path name of a command.
Searches a path environmental variable
for the command and displays the
absolute path.
To find which tcsh and bash are
actually in use, type:
% which tcsh
% which bash
% man which for more details
10 3rd Januaryl, 2025
Commonly used shell in most
variant of UNIX are:
• Bourne Shell (sh), first shell developed for UNIX
• Bourne Again Shell (bash), written by
programmers of Free Software Foundation, open
source shell from GNU
• Korn Shell (ksh), written by David Korn, superset
of Bourne shell, not widely distributed.
• C Shell (csh), written by Bill Joy, the author of vi,
shared much of the C language structure.
• Terminal Based C Shell (tcsh), enhanced
version of the Berkeley UNIX C shell csh
11 3rd Januaryl, 2025
chsh
Change Login Shell
Login shell is the shell that interprets commands
after you logged in by default.
You can change it with chsh (provided that your
system admin allowed you to do so).
To list all possible shells, depending on
implementation:
% chsh -l
% cat /etc/shells
% chsh with no arguments will prompt you for
the shell.
12 3rd Januaryl, 2025
whereis
Display all locations of a command (or
some other binary, man page, or a source
file).
Searchers all directories to find commands
that match whereis’ argument
% whereis tcsh
13 3rd Januaryl, 2025
General Commands
14 3rd Januaryl, 2025
passwd
Change your login password.
A very good idea after getting a new one.
It’s advisable to have your password having at
least 6 chars in the password, at least two
alphabetical and one numerical characters.
Some other restrictions (e.g. dictionary words
or previous password similarity) may apply.
Depending on a privilege, one can change
user’s and group passwords as well as real
name, login shell, etc.
% man passwd
15 3rd Januaryl, 2025
date
Displays dates in various formats
% date
% date -u
in GMT
% man date
16 3rd Januaryl, 2025
cal
Calendar % cal current month
% cal 2 2000Feb 2000, leap year
for month
% cal 2 2100not a leap year
entire year
% cal 2 2400leap year
Years range: 1 - % cal 9 175211 days skipped
9999 % cal 0 error
No year 0 % cal 2002 whole year
Calendar was
corrected in 1752 -
removed 11 days
17 3rd Januaryl, 2025
clear
Clears the screen
There’s an alias for it: Ctrl+L
Example sequence:
% cal
% clear
% cal
Ctrl+L
18 3rd Januaryl, 2025
sleep
“Sleeping” is doing nothing for some time.
Usually used for delays in shell scripts.
% sleep 2 2 seconds pause
19 3rd Januaryl, 2025
Command Grouping
Semicolon: “;”
Often grouping acts as if it were a single
command, so an output of different
commands can be redirected to a file:
% (date; cal; date) > out.txt
20 3rd Januaryl, 2025
alias
Defined a new name for a command
% alias
with no arguments lists currently active
aliases
% alias newcommand oldcommand
defines a newcommand
% alias cl cal 2003
% cl
21 3rd Januaryl, 2025
unalias
Removes alias
Requires an argument.
% unalias cl
22 3rd Januaryl, 2025
history
Display a history of % !n
recently used repeat command n in
commands the history
% history % !-1
all commands in the repeat last command =
history !!
% history 10 % !-2
last 10 repeat second last
command
% history -r 10
% !ca
reverse order
repeat last command
% !!
that begins with ‘ca’
repeat last command
23 3rd Januaryl, 2025
exit / logout
Exit from your login session.
% exit
% logout
24 3rd Januaryl, 2025
shutdown
Causes system to shutdown or reboot
cleanly.
May require superuser privileges
% shutdown -h now - stop
% shutdown -r now - reboot
25 3rd Januaryl, 2025
Files
26 3rd Januaryl, 2025
ls
List directory % ls -F
contents append “/” to dirs and
Has whole bunch of “*” to executables
% ls -l
options, see man ls
long format
for details.
% ls % ls -al
all files except those % ls -lt
starting with a “.” sort by modification
% ls -a time (latest - earliest)
all % ls -ltr
% ls -A reverse
all without “.” and “..”
27 3rd Januaryl, 2025
cat
Display and concatenate files.
% cat
Will read from STDIN and print to STDOUT every line
you enter.
% cat file1 [file2] ...
Will concatenate all files in one and print them to
STDOUT
% cat > filename
Will take whatever you type from STDIN and will put it
into the file filename
To exit cat or cat > filename type Ctrl+D to
indicate EOF (End of File).
28 3rd Januaryl, 2025
more / less
Pagers to display contents of large files
page by page or scroll line by line up and
down.
Have a lot of viewing options and search
capability.
Interactive. To exit: ‘q’
29 3rd Januaryl, 2025
less
less ("less is more") a bit more smart than the
more command
to display contents of a file:
% less filename
To display line numbers:
% less -N filename
To display a prompt:
% less -P"Press 'q' to quit" filename
Combine the two:
% less -NP“ Justin press enter to move down or q"
filename
For more information:
% man less
30 3rd Januaryl, 2025
touch
By touching a file you either create it if it
did not exists (with 0 length).
Or you update it’s last modification and
access times.
There are options to override the default
behavior.
% touch file
% man touch
31 3rd Januaryl, 2025
cp
Copies files / directories.
% cp [options] <source> <destination>
% cp file1 file2
% cp file1 [file2] … /directory
Useful option: -i to prevent overwriting
existing files and prompt the user to confirm.
32 3rd Januaryl, 2025
mv
Moves or renames files/directories.
% mv <source> <destination>
The <source> gets removed
% mv file1 dir/
% mv file1 file2
rename
% mv file1 file2 dir/
% mv dir1 dir2
33 3rd Januaryl, 2025
rm
Removes file(s) and/or directories.
% rm file1 [file2] ...
% rm -r dir1 [dir2] ...
% rm -r file1 dir1 dir2 file4 ...
34 3rd Januaryl, 2025
script
Writes a log (a typescript) of whatever
happened in the terminal to a file.
% script [file]
% script
all log is saved into a file named typescript
% script file
all log is saved into a file named file
To exit logging, type:
% exit
35 3rd Januaryl, 2025
find
Looks up a file in a directory tree.
% find . -name name
% find . \(-name ‘w*’ -or -name ‘W*’ \)
36 3rd Januaryl, 2025
mkdir
Creates a directory.
% mkdir newdir
Often people make an alias of md for it.
37 3rd Januaryl, 2025
cd
Changes your current directory to a new
one.
% cd /some/other/dir
Absolute path
% cd subdir
Assuming subdir is in the current directory.
% cd
Returns you to your home directory.
38 3rd Januaryl, 2025
pwd
Displays personal working directory, i.e.
your current directory.
% pwd
39 3rd Januaryl, 2025
rmdir
Removes a directory.
% rmdir dirname
Equivalent:
% rm -r dirname
40 3rd Januaryl, 2025
ln
Symbolic link or a “shortcut” in M$
terminology.
% ln –s <real-name> <fake-name>
41 3rd Januaryl, 2025
chmod
Changes file permissions
Possible invocations
% chmod 600 filename
-rw------- 1 user group 2785 Feb 8 14:18
filename
(a bit not intuitive where 600 comes from)
% chmod u+rw filename
(the same thing, more readable)
For the assignment:
% chmod u+x myshellscript
(mysshellscript is now executable)
-rwx------ 1 user group 2785 Feb 8 14:18
myshellscript
42 3rd Januaryl, 2025
grep
Searches its input for a pattern.
The pattern can be a simple substring or a
complex regular expression.
If a line matches, it’s directed to STDOUT;
otherwise, it’s discarded.
% echo “blah-comp203” | grep blah
Will print the matching line
% echo “blah-comp203” | grep zee
Will not.
43 3rd Januaryl, 2025
Pipes
What's a pipe?
is a method of interprocess communication
(IPC)
in shells a '|' symbol used
it means that the output of one program (on
one side of a pipe) serves as an input for the
program on another end.
a set of "piped" commands is often called a
pipeline
Why it's useful?
Because by combining simple OS utilities one
can easily solve more complex tasks
44 3rd Januaryl, 2025