You are on page 1of 37

Module 1

Introduction to Linux and Shell


Programming
General purpose utilities
1. Cal current month
2. Cal 03 2010- specified month and year
3. Date system date
4. Date +%m only month
5. Date +%h month name
6. Date +%h %m
7. Date +%d- day of the month
8. Date +%y last 2 digits of year
9. Date +%D date in mm/dd//yy format
10.Date +% - time in HH:MM:SS format
General purpose utilities
1. Echo display msg
Eg: $echo enter name \c prompt and cursor in
same line
o/p: enter name $ _
2. bc
3. Script to record current users login record
4. Password to change password
5. Who
6. Who am i
General purpose utilities
1. Tty knowing user terminal
2. Stty displaying and setting terminal
characteristics
3. Ssty a display with all options
The file system
The file is a container for storing information
Ordinary file also, regular file, contains data as
stream of characters
Director file contains files and other directories,
strictly, it contains their name and number
associated with each name
Device file all device and peripheral are
represented by files. To read or write a device, we
have to perform operations on associated files
The file systems
1. Pwd
2. Cd
3. Mkdir
4. Rmdir
5. Ls
6. Ls filename
7. Ls x : output in multiple columns
8. Ls Fx : list with directory and excutables
Filename* - exe file
Filename\ - directory
The file systems
1. Ls axF list including hidden files
2. Ls xR list file and sub directories in directory
tree structure
Options:
-r sorts file names in reverse order
-t sorts files in last modified
-u sorts file names in last access time
3. Cat
4. Cp source destination
The file systems
1.Cp source dir/dest source copied to dest under
dir
2. Cp source dir source retains its name under dir
3. Co source1 source2 dir source 1&2 copied to
dir
4. Cp i source dest : over writes until Y or N
5. Cp dir1 dir2
6. Rm filename
6. Rm * : removes all files
7. Rm i file1 file2 : asks confirmation for file 1& 2
The file systems
Mv move command used to rename (move) files,
it has 2 functions, 1. re name file or directory 2.
moves a group of files to a dir

1. Mv source desti
2. Mv file1 file2 dir
3. Mv dir1 dir2
4. Wc filename
5. Wc l file name
6. Wc w filename
7. Wc c filename
The file systems
1. Cmp file1 file2
If different files, then
File1 file 2 differ: char 10, line 1
Identical file, then
$_
File attribute
1. Ls l : listing file with attribute
Attributes:
File type and permissions
Links
Ownership
Group ownership
Size
Last modified time
File name
File attribute
1. Ls ld dir : list file details in dir
2. $id : disp user id
File permissions
rwx all three, read, write and execute
r-x read and execute
r-- - only read

3. Chmod changing file premissions


Syn: chmod category operation permissions fname
File attribute
1. $chmod u+x filename
Category:
U user
G- group
O others
A- all
Operations :
+ assign permission
-Remove permission
=assign absolute permission
File attribute
1. Chown chaning ownership
Chown ownername filename;
2.Chgrp -Changing group owner
Process utilities
An instance of a program is called a Process. In
simple terms, any command that you give to your
Linux machine starts a new process.

Types of Processes:
Foreground Processes: They run on the screen and
need input from the user. For example Office
Programs
Background Processes: They run in the background
and usually do not need user input. For example
Antivirus.
Process utilities
By default, every process that you start runs in the
foreground. It gets its input from the keyboard and
sends its output to the screen.

A background process runs without being


connected to your keyboard. If the background
process requires any keyboard input, it waits.

Ls -command
Process utilities
UID- User ID that this process belongs to (the
person running it)
PID-Process ID
PPID-Parent process ID (the ID of the process that
started it)
C-CPU utilization of process
STIME-Process start time
TTY-Terminal type associated with the process
TIME- CPU time taken by the process
Process utilities
Stopping Processes
use the kill command to kill the process
$ kill PID
Parent and Child Processes
Each unix process has two ID numbers assigned to
it: The Process ID (pid) and the Parent process
ID (ppid). Each user process in the system has a
parent process.
Process utilities
a zombie process is a process that has completed
execution (via the exit system call) but still has an
entry in the process table: it is a process in the
"Terminated state".
An orphan process is a computer process whose
parent process has finished or terminated, though
it remains running itself.
Disk utilities
1.Du -To find out the disk usage summary of
a /home/tecmint directory tree and each of its sub
directories.
2.Du h : Using -h option with du command
provides results in Human Readable Format.
Means you can see sizes
in Bytes, Kilobytes, Megabytes, Gigabytes etc.
3. du-s : To get the summary of a grand total disk
usage size of an directory use the option -s
Disk utilities
4. du a: Using -a flag with du command
displays the disk usage of all the files and
directories.
5. du-ah : Using -a flag along with -h displays
disk usage of all files and folders in human
readeable format.
6. 6. du k: Find out the disk usage of a directory
tree with its subtress in Kilobyte blcoks. Use the
-k
Networking utilities
1. The ping command sends an echo request to a
host available on the network.
$ping hostname or ip-address

2. ftp stands for File Transfer Protocol. This utility


helps you upload and download your file from one
computer to another computer.
$ftp hostname or ip-address
Networking utilities
put filename - Uploads filename from the local machine to the
remote machine.
get filename- Downloads filename from the remote machine to the
local machine.
mput file list-Uploads more than one file from the local machine to
the remote machine.
mget file list-Downloads more than one file from the remote
machine to the local machine.
prompt off- Turns the prompt off. By default, you will receive a
prompt to upload or download files using mput or mget commands.
prompt on- Turns the prompt on.
Dir-Lists all the files available in the current directory of the remote
machine.
cd dirname- Changes directory to dirname on the remote machine.
lcd dirname-Changes directory to dirname on the local machine.
Quit- Helps logout from the current login.
Networking utilities
1 .Telnet is a utility that allows a computer user at
one site to make a connection, login and then
conduct work on a computer at another site.
C:>telnet amrood.com
2. The finger command displays information about
users on a given host. The host can be either local
or remote.ork on a computer at another site.
$ finger
Filters
1 .The name "grep" comes from the ed (a Unix line
editor) command g/re/pwhich means globally
search for a regular expression and print all lines
containing it.
$ls -l | grep "Aug
-v : Prints all lines that do not match pattern.
-n :Prints the matched line and its line number.
-l :Prints only the names of files with matching lines
(letter "l")
-c :Prints only the count of matching lines.
-i :Matches either upper or lowercase.
Filters
The sort command arranges lines of text
alphabetically or numerically
$sort food
-n : Sorts numerically, ignores blanks and tabs.
-r :Reverses the order of sort.
-f :Sorts upper and lowercase together.
+x :Ignores first x fields when sorting.
Text processing utilities and Backup utilities
Head
Tail
Sort
grep
Uniq: This command displays uniq lines of the
given files.
Cut : By using this command, we can extract the
required columns or fields from the file.
Paste : create new files by gluing together fields or
columns From two or more files.
Text processing utilities and Backup utilities
Join command: Join lines of two files based on a
common field.
comm command: comm - compare two sorted
files line by line.
Cmp command: cmp is a command line utility
for computer systems that use Unix. It compares
two files of any type and writes the results to
the standard output.
diff command: This command is used to display
file differences. It also tells you which lines in one
file have to be changed to make two files identical.
Text processing utilities and Backup utilities
tr command: (translate or transliterate) When
executed, the program reads from the standard
input and writes to the standard output
Shell Programming
A Shell provides you with an interface to the Unix
system. It gathers input from you and executes
programs based on that input. When a program
finishes executing, it displays that program's
output.
Shell is an environment in which we can run our
commands, programs, and shell scripts.
Shell Prompt
The prompt, $, which is called the command
prompt, is issued by the shell. While the prompt
is displayed, you can type a command.
Shell Programming
Shell Types
In Unix, there are two major types of shells
Bourne shell If you are using a Bourne-type shell,
the $ character is the default prompt.
C shell If you are using a C-type shell, the %
character is the default prompt.

echo "What is your name?


read PERSON
echo "Hello, $PERSON"
Shell Programming - pipes and redirection
redirection and pipes which allow the output (or
even input) of a program to be sent to a file or
another program.
Pipes : Pipes allow you to funnel the output from
one command into another where it will be
used as the input. In other words, the standard
output from one program becomes
the standard input for another.
ls -1 /dev | wc
Shell Programming - pipes and redirection
Redirection : Redirection is similar to pipes except
using files rather than another program. The
standard output for a program is the screen. Using
the > (greater than) symbol the output of a
program can be sent to a file.
Shell Programming - here documents
A here document is a special-purpose code block. It
uses a form of I/O redirection to feed a command list
to an interactive program or a command, such
as ftp, cat, or the ex text editor.
To create a here document use the following syntax:
command <<HERE
Text1
text2
testN
$varName
HERE
This type of redirection tells the shell to read input
from the current source (HERE) until a line containg
only word (HERE) is seen.
Shell Programming - variables
Local Variables A local variable is a variable that is present
within the current instance of the shell. It is not available to
programs that are started by the shell. They are set at the
command prompt.
Environment Variables An environment variable is available
to any child process of the shell. Some programs need
environment variables in order to function correctly. Usually, a
shell script defines only those environment variables that are
needed by the programs that it runs.
Shell Variables A shell variable is a special variable that is set
by the shell and is required by the shell in order to function
correctly. Some of these variables are environment variables
whereas others are local variables.
Shell Programming - metacharacters
The command options, option arguments and
command arguments are separated by the space
character. However, we can also use special
characters called metacharacters in a Linux
Shell Programming
Symbol Meaning `cmd` Command Substitution

> Output redirection $(cmd) Command Substitution

>> Output redirection (append) | The Pipe (|)

< Input redirection Command sequence, Sequences of


;
Commands
File substitution wildcard; zero or
*
more characters || OR conditional execution
File substitution wildcard; one
?
character && AND conditional execution

File substitution wildcard; any Group commands, Sequences of


[] ()
character between brackets Commands

`cmd` Command Substitution Run command in the background,


&
Background Processes
$(cmd) Command Substitution
# Comment
| The Pipe (|)

Command sequence, Sequences of $ Expand the value of a variable


;
Commands
Prevent or escape interpretation of the
\
File substitution wildcard; any next character
[]
character between brackets
<< Input redirection