Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Instructor: Arif Butt.

TA: Dilawer Hussain.

Lecture # 3,4
Basic Shell Commands

Course: Introduction to Computing


Instructor: Arif Butt
TA: Dilawer Hussain

Punjab University College of Information Technology (PUCIT)


University of the Punjab
Punjab University College of Information Technology (PUCIT) 1
Instructor: Arif Butt.
TA: Dilawer Hussain.

Today's Agenda
● Shell & Commands Basics
● Basic Commands
● Getting Help (Using Man Pages)
● Absolute and Relative Path
● More Commands
● Practising Commands

Punjab University College of Information Technology (PUCIT) 2


Instructor: Arif Butt.
TA: Dilawer Hussain.

Shell & Commands Basics

Punjab University College of Information Technology (PUCIT) 3


Instructor: Arif Butt.
TA: Dilawer Hussain.

Bash Shell Prompt

Username Machine Name

Present Working Directory. Type of User. '>'


'~' shows user's home Dir shows normal user,
'#' shows root user

Punjab University College of Information Technology (PUCIT) 4


Instructor: Arif Butt.
TA: Dilawer Hussain.

General Command Format


One or more optional modifiers (w/o spaces in between)that change the behavior of the command.
Usually one character preceded by -

Command Options Option argument(s) Command argument(s)

One or more optional modifiers


The name of the that can change the behavior
command of option

One or more objects


Command must be followed that are affected by the command
by a space

Punjab University College of Information Technology (PUCIT) 5


Instructor: Arif Butt.
TA: Dilawer Hussain.

How Command Executes (Flow Chart)

Punjab University College of Information Technology (PUCIT) 6


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands

Punjab University College of Information Technology (PUCIT) 7


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands
ls - list directory contents
SYNOPSIS
ls [OPTIONS]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by
default). Sort entries alphabetically by default.
Options:
-l display in long listing format (8 x columns)
-a do not ignore entries starting with a dot (i.e., hidden files)
-i show inode number of each file (used with long listing only)
-h show file size in human readable, i.e. In KBs (used with long listing only)
-t sort by modification time
-S, sort by size
-F adds a / in front of directory, an * in front of executable

Punjab University College of Information Technology (PUCIT) 8


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Examples of ls

Punjab University College of Information Technology (PUCIT) 9


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


pwd - print name of current/working directory
SYNOPSIS
pwd [OPTION]

DESCRIPTION
Print the full filename of the current working directory.
Example of pwd

Punjab University College of Information Technology (PUCIT) 10


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


passwd – change user password
SYNOPSIS
passwd [OPTION]

DESCRIPTION
A regular user can change his own password only. However the
administrator can change the password of any user by preceding the
name of the user after the command passwd.

whoami - print name of current logged user


SYNOPSIS
whoami

Punjab University College of Information Technology (PUCIT) 11


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


mkdir - make directories
SYNOPSIS
mkdir [OPTIONS] DIRECTORY....

DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.

Examples of mkdir

Punjab University College of Information Technology (PUCIT) 12


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


cd – is used to change the directory
SYNOPSIS
cd DIRECTORY-NAME

cd[space].. - is used to go one step back in directory hiererchy.

Cd – by simply typing (cd) the prompt will go to the home directory of


user, no matter where you are.

Examples

As a root user, home


directory is ~

Punjab University College of Information Technology (PUCIT) 13


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


rmdir - remove empty directories
SYNOPSIS
rmdir [OPTIONS] DIRECTORY(S)

DESCRIPTION
Remove the DIRECTORY(ies), if they are empty.
Options:
-p, Create/Remove DIRECTORY and its ancestors. E.g., rmdir -p
a/b/c is similar to rmdir a/b/c a/b a

Example of rmdir

Using rmdir a/b/c will


only remove directory c not
the parent directories.
Punjab University College of Information Technology (PUCIT) 14
Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


touch - change file timestamps
SYNOPSIS
touch [OPTIONS]... FILE...

DESCRIPTION
Update the access and modification times of each FILE to the
current time.

Example of touch

Touch command also makes a


new file by simply typing
touch Filename
Punjab University College of Information Technology (PUCIT) 15
Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


cp - copy files and directories
SYNOPSIS
cp [OPTIONS]... SOURCE DEST

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to
DIRECTORY.
Options:
-r, Recursively copies directory including files and subdirectories to
destination
-p, Preserves file access modes and time stamps on copied files

Punjab University College of Information Technology (PUCIT) 16


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Example of cp

dir1 is not empty


Simple cp command will not work

Use -r option for this case

Punjab University College of Information Technology (PUCIT) 17


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


mv - move (rename) files
SYNOPSIS
mv [OPTIONS] FILE/DIR-LIST DIRECTORY

DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to
DIRECTORY. Do not require -r flag for directories. More so, this
command is also used to rename files and directores

Options:
-i Prompt user before ovwrwriting destination

Punjab University College of Information Technology (PUCIT) 18


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Example of mv

Punjab University College of Information Technology (PUCIT) 19


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


rm - remove files or directories
SYNOPSIS
rm [OPTIONS] FILE...

DESCRIPTION
rm removes each specified file. By default, it does not remove
directories.
Options:
-r, -R, --recursive remove directories and their contents recursively

Punjab University College of Information Technology (PUCIT) 20


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Example of rm

To avoid prompts
use -f along with
other options

Punjab University College of Information Technology (PUCIT) 21


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTIONS] [filename]

DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
Options:
-n, Print line # with each line of the file (for display only)
-ev, Print $ sign, at the end of each line

Punjab University College of Information Technology (PUCIT) 22


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Example of cat

Punjab University College of Information Technology (PUCIT) 23


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


more – displays file contents page by page
SYNOPSIS
more [OPTIONS] [filename]

DESCRIPTION
More is a filter for paging through text one screenful at a time.

Example of more

Punjab University College of Information Technology (PUCIT) 24


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


less – display file contents page by page
SYNOPSIS
less [OPTIONS] [filename]
DESCRIPTION
Less is a program similar to more (1), but which allows backward
movement in the file as well as forward movement.
Commands:
Movement is done using up/down arrows and pgup and pgdown buttons
G go to the end of the file
g go to the start of the file
/arif search forward for string 'arif '
?arif search backward for string 'arif '
n repeat last search
N repeat last search in opposite direction
v open file in vim
q quit the program
Punjab University College of Information Technology (PUCIT) 25
Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


Example of less

Punjab University College of Information Technology (PUCIT) 26


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


head – output the first part of the file
SYNOPSIS
head [OPTIONS] filename(s)
DESCRIPTION
Print the first ten lines of each file to standard output. With more
than one file precede each with a header giving the file name. With no
file read standard input.
Options:
-n instead of default 10 lines display n lines on console

Punjab University College of Information Technology (PUCIT) 27


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


tail – output the last part of the file
SYNOPSIS
tail [OPTIONS] filename(s)
DESCRIPTION
Print the last ten lines of each file to standard output. With more
than one file precede each with a header giving the file name. With no
file read standard input.
Options:
-n instead of default 10 lines display n lines on console
-f follow growth (used to view log files)

Punjab University College of Information Technology (PUCIT) 28


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


alias – define or display aliases
SYNOPSIS
alias [aliasname=string]
DESCRIPTION
Allows you to create pseudonyms for commands. To remove an
existing alias use unalias command
To make a permanent change in the aliases list, a user need to change
the file ~/.bashrc
In order to make a global alias, the root must add it in
/etc/bash.bashrc.local in opensuse and /etc/bashrc file in RedHat
systems

Punjab University College of Information Technology (PUCIT) 29


Instructor: Arif Butt.
TA: Dilawer Hussain.

Basic Commands (cont..):


echo - display a line of text
SYNOPSIS
echo [OPTIONS] string

DESCRIPTION
Echo the STRING(s) to standard output.
Options:
-e enable interpretation of backslash escapes

If -e is in effect, the following sequences are recognized:


\\ backslash \b backspace
\n new line \t horizontal tab
Example of echo

Punjab University College of Information Technology (PUCIT) 30


Instructor: Arif Butt.
TA: Dilawer Hussain.

Getting Help

Punjab University College of Information Technology (PUCIT) 31


Instructor: Arif Butt.
TA: Dilawer Hussain.

Ways to Get Help


● GUI Help
● Press Alt+ F2, type help
● CLI Help (Open Konqueror Terminal, use)
● $info Command
● $help Command
● $man Command

Punjab University College of Information Technology (PUCIT) 32


Instructor: Arif Butt.
TA: Dilawer Hussain.

Using Man pages


The standard way to get help, which works on any UNIX system, is a
command called man (think "manual", as in user handbook).
There are mainly 9 sections of Man pages usually located in /usr/share/man/:
● Section 1 User Commands (cat.1.gz, clear.1.gz, cp.1.gz, ...)
● Section 2 System Calls (open.2.gz, close.2.gz, fork.2.gz, ...)
● Section 3 Language Library Calls (fopen.3.gz, fclose.3.gz, ...)
● Section 3p Perl Modules (fork.3p.gz, fputc.3p.gz, fopen.3p.gz,..)
● Section 4 Devices (mouse.4.gz, fifo.4.gz, ...)
● Section 5 File Formats (passwd.5.gz, fstab.5.gz, elf.5.gz, ...)
● Section 6 Games (fortune.6.gz, glchess.6.gz, ...)
● Section 7 Miscellaneous (ip.7.gz, tcp.7.gz, intro.7.gz, ...)
● Section 8 Administrative / Previleged Commands (fdisk.8.gz, ...)
● Section 9 Kernel

Punjab University College of Information Technology (PUCIT) 33


Instructor: Arif Butt.
TA: Dilawer Hussain.

Using Man pages (cont..)


Parts of man page:
1. Information at the top
2. Name of a command
3. Synopsis of a command
4. Description of a command
5. Author
6. Reporting Bugs
7. Copy Rights
8. See Also
9. Information at the Bottom

Punjab University College of Information Technology (PUCIT) 34


Instructor: Arif Butt.
TA: Dilawer Hussain.

Using Man pages (cont..)


● Example: Type 'man pwd' in konqueror Terminal & select the
option

● Navigating, Searching and Exiting from man pages:


● Man command use the less program to display the contents

of the help pages, so you can use all techniques that you have
learnt for navigation and searching in the less program.
patterns of 'pwd'

Punjab University College of Information Technology (PUCIT) 35


Instructor: Arif Butt.
TA: Dilawer Hussain.

Absolute Path / Relative Path


Absolute Path:
Absolute path specify the full path from root to the desired directory
or file. Every pathname that starts with a slash is an absolute
pathname.
Reference to the figure on next slide:
/home/staff/arif/file2 is an absolute path for file2
Relative Path:
A relative path is a path from the working directory to the file.
Reference to the figure on next slide:
● If working directory is staff then, rauf/file3 is relative path for file3

● If working directory is arif then, ../rauf/file3 is relative path for file3

Punjab University College of Information Technology (PUCIT) 36


Instructor: Arif Butt.
TA: Dilawer Hussain.

Directory Tree:
/

home dev usr

dilawer abdul staff

reports file1 arif rauf


.........
.........
......... file2 file3
......... ......... .........
......... .........
......... .........
......... .........

Punjab University College of Information Technology (PUCIT) 37


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands

Punjab University College of Information Technology (PUCIT) 38


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


which - shows the full path of (shell) commands.
SYNOPSIS
which [OPTIONS] programname

DESCRIPTION
Which takes one or more arguments. It searches an executable or
script in the directories listed in the environment variable PATH
Option:
-a Print all matching executables in PATH, not just the first
Example of which

Punjab University College of Information Technology (PUCIT) 39


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


whereis - locate the binary, source, and manual page files for a
command
SYNOPSIS
whereis [OPTIONS] filename

DESCRIPTION
whereis locates source/binary and manuals sections for specified
files. Whereis then attempts to locate the desired program in a list of
standard Linux places.
Options:
-b Search only for binaries
-m Search only for manual sections
-s Search only for sources

Punjab University College of Information Technology (PUCIT) 40


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


file - determine file type
SYNOPSIS
file filename .....
DESCRIPTION
File command displays the type of the file, which can be on of the
following:
● Text

● Executable

● Data

stat – display file or file system status


SYNOPSIS
stat filename .....
DESCRIPTION
Displays statistics about the file like, its size, IO blocks,
ownership, time stamps, e.t.c.
Punjab University College of Information Technology (PUCIT) 41
Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


wc – prints number of newlines, words, and bytes in a file

clear – clear the terminal screen


shutdown – When used with -h option, halts the system

shutdown – When used with -r option, reboots the system


bc – An interactive calculator program

Punjab University College of Information Technology (PUCIT) 42


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands:
find - search for files in a directory hierarchy
SYNOPSIS
find [OPTIONS] [path...] [expression]

DESCRIPTION
GNU find searches the directory tree rooted at each given file
name by evaluating the given expression from left to right, according
to the rules of precedence.

Example of find
1.Search for a file named file1 from /home/arif
find /home/arif/ -name file1
2.Search for a file named file1 in your entire file system
find / -name file1

Punjab University College of Information Technology (PUCIT) 43


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]

DESCRIPTION
Display the current time in the given FORMAT, or set the system
date.

Examples of date
To display the current date and time
date
To change the date and time
date MMDDhhmmYY

To set date you must be a root user

Punjab University College of Information Technology (PUCIT) 44


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


cal - displays a calendar
SYNOPSIS
cal [OPTIONS] [[month] year]

DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the current
month is displayed. The options are as follows:
Options:
-s Display Sunday as the first day of the week. (This is the default.)
-1 Display single month output. (This is the default.)
-3 Display prev/current/next month output.
-y Display a calendar for the current year.

Punjab University College of Information Technology (PUCIT) 45


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


Example of cal

Punjab University College of Information Technology (PUCIT) 46


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


diff - compare files line by line
SYNOPSIS
diff [OPTIONS] files

DESCRIPTION
Compare files line by line.
Options:
-i Ignore case differences in file contents
-E Ignore changes due to tab expansion
-b Ignore changes in the amount of white space
-B Ignore changes whose lines are all blank

Punjab University College of Information Technology (PUCIT) 47


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


Example of diff

Punjab University College of Information Technology (PUCIT) 48


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


grep - print lines matching a pattern
SYNOPSIS
grep [OPTIONS] PATTERN [FILE]

DESCRIPTION
grep searches the named input FILEs (or standard input if no
files are named, or if a single hyphen-minus (-) is given as file name)
for lines containing a match to the given PATTERN. By default,
grep prints the matching lines.
Options:
-f FILE, Obtain patterns from FILE, one per line. The empty file
contains zero patterns, and therefore matches nothing.
-i, Ignore case distinctions in both the PATTERN and the input
files. (-i is specified by POSIX.)

Punjab University College of Information Technology (PUCIT) 49


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


Example of grep

Punjab University College of Information Technology (PUCIT) 50


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


sort - sort lines of text files
SYNOPSIS
sort [OPTION]... [FILE]

DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.
Options:
-r, reverse the result of comparisons
Examples of sort

Punjab University College of Information Technology (PUCIT) 51


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


tar - The GNU version of the tar archiving utility
SYNOPSIS
tar [OPTIONS] [FILES]

DESCRIPTION
GNU `tar' (tape archive) saves many files/directories together
into a single tape or disk archive, and can restore individual files
from the archive.
Options:
-c, create a new archive
-r, append files to the end of an archive
-t, list the contents of an archive
-x, extract files from an archive
-f, use archive file or device ARCHIVE
-v, verbosely list files processed
-z, filter the archive through gzip
Punjab University College of Information Technology (PUCIT) 52
Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


Following tar command will create an archive mywork.tar. The original
files/dirs remain intact
$tar cvf mywork.tar space-separated-files/dirs-names

Following tar command is used to inspect the contents of an archive


$tar tvf mywork.tar

Following tar command will extract the original files/dirs from the
archive mywork.tar. Before extraction, your pwd must be the target
directory, because tar always extracts to current directory
$tar xvf mywork.tar

Punjab University College of Information Technology (PUCIT) 53


Instructor: Arif Butt.
TA: Dilawer Hussain.

More Commands (cont..):


Compressing Files
● Reduction in size of a file is known as file compression

● Advantages

● Less disk space

● Less time to transmit over network

● Less time to copy

● Not readable

● Following command will compress file1 to file1.gz and file2 to


file2.gz. Original files are replaced with the compressed files
$gzip file1 file2
● Following command will uncompress file1.gz and file2.gz.

$gzip file1.gz file2.gz


● bzip2 and bzip2 are newer utilities for compression, used in the

same way as gzip

Punjab University College of Information Technology (PUCIT) 54


Instructor: Arif Butt.
TA: Dilawer Hussain.

Wild Cards

Punjab University College of Information Technology (PUCIT) 55


Instructor: Arif Butt.
TA: Dilawer Hussain.

Wild Cards
When you give the shell abbreviated filenames that contain special
characters, also called metacharacters, the shell can generate
filenames that match the names of existing files. These special
characters are also referred to as wildcards because they act as the
jokers do in a deck of cards.

Special Characters:
● ?
● *
● []

Punjab University College of Information Technology (PUCIT) 56


Instructor: Arif Butt.
TA: Dilawer Hussain.

Wild Cards(cont..)
The ? Special Character
The question mark (?) causes the shell to generate filenames.
It matches any single character in the name of an existing file.
Example:

The * Special Character


The asterisk (*) performs a function similar to that of the
question mark but matches any number of characters, including
zero characters, in a filename.
Example:

Punjab University College of Information Technology (PUCIT) 57


Instructor: Arif Butt.
TA: Dilawer Hussain.

Wild Cards(cont..)
The [] Special Character
Using [], pair of brackets the shell matches all the characters
with the file name that is included in the pair of brackets.
Examples:
From page1-page6, we just
want to list page2, page3, page6

We want to list page1 - page4

Punjab University College of Information Technology (PUCIT) 58


Instructor: Arif Butt.
TA: Dilawer Hussain.

Things to do!

If you have problems visit me in counseling hours. . . .


Punjab University College of Information Technology (PUCIT) 59

You might also like