2020-08-20
2
The Shell
COMP500
Spring 2019
Review from last week
● Types of Operating Systems
● Popularity of Linux
● Kernel Responsibilities and Versioning
● Common Uses of Linux
● Types of Licensing
Learning Objectives
● Describe the Linux Shell
● Identify Shell Commands
● Use Linux Help System
● Identify components of File System
● Use File Management Commands
1
2020-08-20
Using the Shell
The Shell
Program that allows a user to interact with the system using typed text
commands displayed in the screen
The most popular implementation is the Bash Shell (Bourne Again Shell). Other
examples are Dash, Fish and ZShell
When used properly, allows for a faster interaction with the machine so it is the
System Administrators’ tool of choice
Accessing the Shell
Provided by default on a basic Linux installation with no GUI installed (you will see
this shell during the lab sessions)
In a system with a GUI installed, run a terminal emulator program such as
Gnome-Terminal, X-Terminal, Terminator, etc.
2
2020-08-20
Dennis Ritchie
PDP-11
Ken Thompson
Teleprinter
(also called
Teletype or TTY)
https://en.wikipedia.org/wiki/Ken_Thompson#/media/File:Ken_Thompson_(sitting)_and_Dennis_Ritchie_at_PDP-11_(2876612463).jpg (CC BY-SA 2.0)
Terminal
https://i.stack.imgur.com/mTxcb.jpg
Shell Commands
A command is a program that is run by inputting its name in the shell
The command can be built-in, an alias or user-created
Examples of built-in are ls, top, mkdir
Alias are shorter versions of long commands: ll in place of ls -al
Users can create their own programs and define a text command for execution
3
2020-08-20
Shell Command Syntax
command [options...] [arguments...]
Example: rm -r /user
Command is the name of the program: “rm” is the command for remove
Options modify the behavior of the program: “-r” indicates recursive operation
Arguments are variables passed from the user to the program: “/user” is the
folder to be removed
Shell Command Syntax
IMPORTANT OBSERVATIONS
Commands are case sensitive:
Rm is not the same as rm
Commands, options and arguments are separated by spaces:
rm -r /user is not the same as rm-r/user
Shell Command Options
Short form uses a single dash and a character: ls -l
Short form options can be combined: ls -a -l becomes ls -al
Long form uses a double dash and a word: ls --long
Long and form options can be used in the same command
Options are all case-sensitive
BSD(UNIX) style options do not use hyphens: ps axu
4
2020-08-20
Shell Command Arguments
Arguments are commonly optional
Used to pass variables from the user to the command
Arguments are often file or directory names: rm -r /home
Arguments can also be values: sleep 120
Require single quotes if text contains special non-alphanumeric values
Command Autocompletion
If a user types cle and then hits <TAB> the shell autocompletes clear
Useful for saving time while typing long inputs
The text is auto completed only if characters typed so far are unique to the target
A second <TAB> gives a list of possible matches
Works with commands, file names and directory names
Command History
Can use ↑ and ↓ to cycle through previously used commands
Press the ↑ key to retrieve the last command from the command history
Press the ↓ key to go forward though the command history after using ↑ to go
back through the history
Command retrieved from history can be edited before being executed
5
2020-08-20
The Help System
The man Command
man is short for manual
Displays shell commands documentation, syntax, options, arguments and
examples
To display documentation type man and then the command (e.g. man ls)
● Move per line with Up/Down Arrows
● Scroll one screen at a time with Space (down) and b (up)
● Search the file for specific “term” with /term
● Exit with q key
Help page layout
NAME. A brief description of the command
SYNOPSIS. Syntax definition
DESCRIPTION. List and describes the command options
AUTHOR. The command developer
COPYRIGHT. License information
SEE ALSO. Other resources for additional information
6
2020-08-20
Man Page Sections
Man pages are classified in Sections (numbers in parentheses after command
name when viewing a man page)
1. Executable programs or shell commands
2. System calls
3. Library calls
4. Special files
5. File formats and conventions
6. Games
7. Miscellaneous
8. System administration
9. Kernel routines
Other Sources of Help
Command info displays help information about a command: info ls
Option --help displays help information about a command: ls --help
Not all commands have --help option
7
2020-08-20
Utility of the Help System
Provides a quick source of information about shell commands
Provides description about usage, syntax and examples
Packages often feature extra help content that can be found in /usr/doc or
/usr/share/doc
No reason for user for stopping workflow
Find an Actual Program File
The command whereis displays the location in disk of an executable program. It
also shows the source code and documentation if available
Also possible to use locate command which can find any file provided the correct
permission
The command updatedb updates information for locate command. It is scheduled
for execution daily but the root user can run anytime using the command
updatedb
Basic Shell Commands
8
2020-08-20
uname
Prints system information
Linux Kernel name uname -s
Linux Kernel release uname -r
Processor type uname -p
Operating System uname -o
All the information uname -a
sudo
Executes a command as the root user (or another user)
Short for "super user do"
One of the most used commands for its implications
The user must have the proper permissions in order to use sudo
Install applications: sudo apt install php
Access files outside of user’s home directory: sudo vim /etc/passwd
https://linuxacademy.com/blog/linux/linux-commands-for-beginners-sudo/
exec
Executes another command that replaces the current shell
Receives another command as argument: exec zsh
It does not return upon successful completion
Cannot be used inside a pipeline
https://www.computerhope.com/unix/bash/exec.htm
9
2020-08-20
history
Search, delete and manipulate the command history
Display a list of commands entered since the start of the session: history
Re-run the 3rd command on the history list: !3
Search commands containing ‘foo’: history | grep foo
Clear all history items: history -c
https://opensource.com/article/18/6/history-command
echo
Prints a line of text that is passed as an argument
Mostly used in shell scripts and batch files to output status text to STDOUT
Output text can be redirected to a file
For example, “echo hello”
Managing Inputs and Outputs
10
2020-08-20
STDIN, STDOUT and STDERR
STDIN. Standard input, by default originated in the keyboard
STDOUT. Standard output, by default sent to the screen
STDERR. Standard error, by default sent to the screen
All have a system-defined variable used in the shell. STDIN is $0, STDOUT is $1
and STDERR is $2
Piping
A command expects some input in order to produce an output
The pipeline symbol ‘|’ allows to apply the output of a command
as the input of another. Thus, joining two or more commands
processing an initial input to produce a final output
The example below applies the output of cat command (text) as
the input of grep command which will look for occurences of the
word “hello”
cat myfile | grep “hello”
Redirection
Output redirection “>” STDOUT or STDERR are redirected to another output or file
cat myfile > newfile saves the contents of myfile to newfile instead of
displaying it
cat myfile >> newfile appends instead of overwrite
Input redirection “<” STDIN is redirected from another input
11
2020-08-20
The Filesystem
Files and Directories in Linux
Everything is a file in Linux (text files, programs, binary files, devices, etc)
Allowed characters are letters, digits, hyphens, underscores and dots
All file names are case sensitive
Directories are entities that group files
Paths to Directories and Files
Any file or directory has a path
Paths referred to root directory ‘/’ are ‘absolute’ paths
/home/user1/file.txt
Paths referred to the current directory are ‘relative’ paths
file.txt (current location is already /home/user1/)
12
2020-08-20
The home Directory
Each user of a Linux system is assigned a folder inside /home/
The user has all permissions (read, write and execute) inside his/hers home folder
Denoted by the “~” character (i.e. ~/testfile is actually /home/user/testfile)
Only root user has access to all files and directories
Use sudo to access other directories outside of your home directory
Present Working Directory
The command ‘pwd’ means present working directory
Provides the absolute path to current location of a user
Sometimes is displayed in the prompt:
The Root Directory ‘/’
Top level directory with no parent directory
Denoted with the symbol ‘/’
All other directories fork from it
https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
https://www.howtogeek.com/117435/htg-explains-the-linux-directory-structure-explained/
13
2020-08-20
/bin
Essential binaries (programs) for default commands are stored in this directory
Commands used by all the users of the system are located here
Some common binaries are: bash, cat, date, echo, kill, mv, cp
/boot
Contains files needed to boot the system
Kernel initrd, vmlinux, grub files are located under /boot
i.e. initrd.img-4.15.0-24-generic, vmlinuz-4.15.0-24-generic
/dev
Contains device files (everything is a file in Linux, even hardware)
These include a file for each terminal, usb, or any device attached to the system
For example: /dev/tty1, /dev/usbmon0
14
2020-08-20
/etc
Contains configuration files required by all programs
This also contains startup and shutdown shell scripts used to start/stop individual
programs
Examples: /etc/hostname, /etc/resolv.conf
/home
Stores home directories for all non-root users
user1 will have a default home directory located at /home/user1
Home directory is denoted by ‘~’ symbol for the current user
/lib
Contains essential library files that support the binaries located under /bin and
/sbin
Library file names are either ld* or lib*.so.*
For example: ld-2.11.1.so, libncurses.so.5.7
15
2020-08-20
/media and /mnt
/media is a temporary directory for removable devices automatically assigned
In contrast, /mnt is a temporary directory for manual mount by sysadmins
For example, /media/cdrom for CD-ROM, /media/floppy for floppy drives,
/mnt/boot for a new Linux installation
/opt
opt stands for optional
Contains add-on applications from third party vendors
Add-on applications should be installed under either /opt/ or /opt/ sub-directory
/proc
Contains special files that represent system and process information
The directory /proc/{pid} contains information about the process with that
particular pid
Also contains information about system resources (i.e. /proc/uptime)
16
2020-08-20
/root
home directory of the root user
Instead of being located at /home/root, it’s located at /root
/sbin
Similar to /bin, /sbin contains binary executables
Binaries stored here are used for system maintenance purpose
Commands such as: iptables, reboot, fdisk, ifconfig, swapon
/srv
Contains data for services provided by the system
For example:
● Website files could be stored in /srv/www
● CVS files could be stored in /srv/cvs
17
2020-08-20
/usr
Contains binaries, libraries, documentation, and source-code for second level
programs
/usr/bin contains binary files for user programs. For example: at, awk, cc, less, scp
/usr/sbin contains binary files for system administrators. For example: atd, cron,
sshd, useradd, userdel
/usr/lib contains libraries for /usr/bin and /usr/sbin
/usr/local contains users programs that you install from source. For example,
when you install apache from source, it goes under /usr/local/apache2
/var
Content of files that are expected to change can be found under this directory
● system log files (/var/log)
● packages and database files (/var/lib)
● emails (/var/mail)
● print queues (/var/spool)
● lock files (/var/lock)
● temp files needed across reboots (/var/tmp)
File Management
18
2020-08-20
Change Directory
To change directories use the cd command: cd ~/Downloads
ls command with no arguments takes the user to home folder (~)
To stay in the current directory: cd .
To go to the parent directory: cd ..
Listing Files
List files and directories in a directory with ls command
Receives the location to list as argument: ls /home
Without arguments, lists the contents in the current directory: ls
Common options are:
● -a for all files including hidden files
● -l for long list format
● -R for recursive listing
Creating Files
Using the ‘touch’ command to create an empty file: touch myfile
Using the ‘echo’ command to redirect STDOUT to a file: echo “Hello” > myfile
Using the ‘cat’ command cat > myfile any text entered after this line will be sent to
the file ‘myfile’. End with Ctrl-D
19
2020-08-20
Displaying File Contents
One of the simplest methods is using ‘cat’ command: cat myfile
The text inside the file ‘myfile’ is displayed on the screen
Summary
● Describe the Linux Shell
● Identify Shell Commands
● Use the Linux Help System
● Identify Structure of the File System
● Use File Management Commands
Lab this Week – Lab 2
● Installing a LAMP Server using CLI
● Set of open-source software that can be used to build web applications
○ Linux operating system
○ Apache HTTP Server
○ MySQL relational database
○ PHP programming language
20
2020-08-20
Lecture Next Week
● Linux Kernel
● Kernel Provisioning
References
Nemeth, E., & Snyder, G., Hein, T., Whaley, B., Mackin, D. (2018). UNIX and
Linux System Administration Handbook. Toronto: Addison-Wesley.
21