Lecture 2
Lecture 2
Lecture 2
(ECC 4209)
Lecture 2
(Introduction to Linux)
sjh@upm.edu.my
Contents
1. What makes Linux different?
2. Basic survival skills
3. Getting help
What makes Linux different
from other operating systems?
• Linux is free, easier than other OSs to install exactly
where and when needed for any use you can imagine.
• Linux makes it possible to do various really useful and
creative things.
• Linux was built with the same technology and comes with
most of the same tools as the deeply mature UNIX OS.
• Linux distributions also provide sophisticated software
package management systems that reliably install and
maintain any applications available through online
repositories.
What makes Linux different from
other operating systems? (cont)
• But beyond free, Linux is open source, which means
anyone can take the code base and reshape it into
anything they want
• A distribution (sometimes shortened to distro) is a
customized stack of software that’s packaged along with
the Linux kernel and distributed with tools for installing a
working version of Linux of user computers
Linux Distributions
(www.distrowatch.org)
Purpose Distro
Security/anti-hacking Kali Linux
Parrot
Consumer desktop Mint
Elementary OS
Lightweight Puppy Linux
(old hardware; diagnostics) LXLE
Internet of Things administration Snappy Ubuntu Core
Directory tree
representation
of data files
Root
Shared libraries
Third-party binaries
Linux Navigation Tools
• ls
– List directory contents
– l flag (l stands for long) lists not only the object names,
but their file permissions, owner, group, file size, and
time stamp.
– Adding a directory designation like /var displays the
contents of that directory
• ls -l /var
• ls -lh /var (display file size in human readable format)
– Note that short form equivalent for command i.e same
output (see online manual)
• ls -h
• ls --human-readable
– Can display directory and sub-directory
• ls -R
Linux Navigation Tools
(continue)
• pwd
– Print Working Directory (i.e path to current directory)
– Show current directory
• cd
– Change directory
– Change into other directory via absolute or relative path
– Relative path
• cd (take you back to the home user’s directory)
• cd .
• cd ..
– Absolute path
• cd /
• cd /home/yourname (same as cd)
Linux Navigation Tools
(continue)
• cat
– Output file contents into terminal
• cat /etc/fstab
• cat /etc/group
– Short from concatenate
• cat can join multiple strings or files into a single text stream
• less
– Output file contents into terminal, with scrolling feature
– Output file contents into terminal, with scrolling feature
– can scroll up and down through the file with the arrow,
PgUp, PgDn, and spacebar keys
– When done, press the q key to exit
– less /etc/services
Bash shell
Shell
(Interprets commands)
Kernel
Hardware
Linux File Management Tools
• need to know how to create, destroy, move, and
copy files
• touch updates its time stamp without making any
• changes
– $touch myfile
– $cat myfile
– $nano myfile
– $vim myfile
• Note that nano is modeless editor and vim is a modal editor
– Vi/Vim is one of the popular Unix/Linux editor in addition to Emacs
– enter Insert Mode by pressing the i key and then typing your text
– save your file by pressing Esc, then type :w, and then exit by:q
– type :wq for save and exit at the same time
Creating and Deleting Directories
• Every object within file system is represented by a
unique collection of metadata called an inode
– file system is built from the metadata associated with all
the many inodes
– object used by UNIX systems to identify the disk
location and attributes of files within a file system
– Usually one inode for each file or directory.
– $stat myfile (display more information including
inode information)
• Output includes data describing the file’s name,
attributes, time stamps and inode ID number
• When move, copy, or delete a file or directory, all is
really editing its inode attributes, not its ID
Creating and Deleting Directories
(continue)
• Create a new directory that you can use
– $ mkdir myplace
• Move to your new directory and create a file there by
cd myplace and touch newfile
• Move back up to parent directory by cd .. and
delete the new directory by rmdir myplace
– rmdir: failed to remove 'myplace':
Directory not empty
– This is a built-in check to prevent you from accidentally
deleting directories
– To get around this
• --ignore-fail-on-non-empty argument to the rmdir
command
• rm -r myplace (add the -r flag, meaning recursive)
Copying And Moving Files
• Create a few more files and a new directory:
– $ touch file1 file2 file3
– $ mkdir newdir
• You can make an identical copy of an object
using cp. This example creates a copy of
– file1 within the directory newdir:
– $ cp file1 newdir
• cp command recognize newdir as a directory
rather than a file.
– If no directory newdir in the current location, cp would
instead make a new copy of file1 named newdir
Copying And Moving Files
(continue)
• mv command will permanently move an object
from one place to another.
– $ mv file2 newdir
– original file would no longer be available
• Copy, move, or delete directories using the same
commands as for files, adding the -r flag where necessary
• Potential to move more than just the directory you see and
any existing layers of unseen nested levels will also be
dragged
– always check your output or results of command
File Globbing
• Globbing (derived from the word global)
– applying wildcard characters to the filenames
addressed by commands
• To move all the contents of the current directory
to some other location:
– $ mv * /some/other/directory/
• To move all files whose names begin with the
letters file, but leaves everything else untouched:
– $ mv file* /some/other/directory/
• To move files with names file1, file2, file3 …, file15 and
wanted to move only those between file1 and file9, you’d
use the question mark (?) instead of the asterisk:
– $ mv file? /some/other/directory/
Deleting Files
• Files and objects can be deleted using rm.
– Note that operations are irreversible
– $ rm file1 (delete file1 from the directory)
• File globbing can be applied to rm the same way
as to cp or mv, and with the same efficiency
– $ rm file* (deletes all files in the current directory
whose names begin with the letters file)
• Adding the -r argument to a delete operation will make
the action recursive and delete the contents of any
subdirectories in the specified path:
– $ rm -r *
– very dangerous combination, especially when working
with root authority
Keyboard Tricks
• Cutting And Pasting
– Ctrl-c (copy) and Ctrl-v (paste) key combinations won’t work
but Shift-Ctrl-c and Shift-Ctrl-v will.
– Cut and paste by right-clicking your mouse and selecting the
appropriate operation from the menu
• Tab Completion
– Press the Tab key tells Bash to display its best guess
on the command line
– $ touch my-very-long-filename_66-b.txt
– $ rm my-<tab>
Pseudo file systems
• A normal file is a collection of data that can be
accessed over even after a system reboot.
• Contents of a Linux pseudo (or virtual) file, do not
exist in the normal sense
– /sys and /proc directories
– pseudo file’s contents are dynamically generated by
the OS itself to represent specific values
• Use cat to display of bytes on the disk,
designated by the system as sda:
– $ cat /sys/block/sda/size
– 1937389568
– common source of information and data
all system processes rely including GUI
– Examples top, htop
Pseudo file systems
(continue)
• For example howto know your drive designation?
• Linux organizes attached storage as block
devices
– Check contents of /sys/block directory
– $ cd /sys/block
– $ ls
– loop0 loop1 loop2 sda sr0
Sudo
• For practical reasons, using an OS account that
enjoys full administration powers is risky
• But fully restricting to non-administration account
makes it impossible to get anything done
• Linux solve this problem by providing selected
accounts with admin authority using sudo
• Once you confirm your identity by providing your
password (not root password), your command will
be treated as though it was issued by the root user
– $ cat /etc/shadow
– cat: /etc/shadow: Permission denied
– $ sudo cat /etc/shadow
– [sudo] password for ubuntu:
• Please note that non-root user shell prompt is
Getting Help
(man files)
• man files (or manual files) are installed together
with any software being installed on Linux
– $man man
– $man ls
• man files contents include:
– NAME (of the installed software)
– SYNOPSIS (detailed syntax overview)
– DESCRIPTION (details explanation includes a list of
command-line arguments and flags)
– EXAMPLES (typical usages examples)
– SEE ALSO (related command and functions)
• man files is written in the vi/vim format for reading,
navigation, etc
Getting Help
(man files - continue)
Section Descriptions
1 General commands
2 System calls