Linux Regular Commands

Linux

Linux
Check number of CPU’s

$cat /proc/cpuinfo

Check memory information

cat /proc/meminfo

Check swap

$swapon -s

Kernel parameter

#/sbin/sysctl -a
cat /etc/sysctl.conf

System error log

/var/log/messages

Nic configuration

ifconfig -a

Directory Commands – mkdir

  • To create a new directory below current directory:

$mkdir new-dir-name

  • To create multiple versions of directory:

$mkdir -p dir1/dir2/dir3

  • Creates dir1 first. Creates dir2 under dir1. Creates dir3 under dir2
  • To create a directory with given permissions:

$mkdir -m 754 new-dir

  • new-dir will be created with 754 permissions

Directory Commands – rmdir

  • To remove a directory:

$rmdir dir-name

  • rmdir can remove only empty directories
  • To remove directory along parent directory:

$rmdir -p dir1/dir2/dir3

  • Deletes all the directories

Directory Commands – cd

  • Stands for change directory
  • Used to navigate among directories

$cd dir1

  • Takes to dir1 directory

$cd

  • Takes to the home directory
  • Can give full-path-name or relative-path-name as argument

UNIX commands – banner

  • Prints the given string in big letters

$banner <string>

UNIX commands – touch

  • Used to change the last access time of a file

$touch -a file1

  • Changes the access time of file1 with current time

$touch file1

  • Changes access time as well as modification time will be changed to current time

UNIX commands – file

  • Used to know the file type like shell script, executable

$file file-name

english text

UNIX file commands – wc

  • Used to know the number of characters, number of files and number of lines in a file

$wc file1

$wc -l file1

  • Displays the number of lines in file1
  • Can take the input from terminal directly

UNIX file commands – sort

  • Used to sort a file

$sort file1

  • Sorts file1 and displays the sorted file on terminal
  • Instead of displaying the sorted file on terminal, can be written in to a file by:

$sort –o file2 file1

  • Writes the sorted file1 in file2

UNIX file commands – cut

  • Cut is a filter.
  • Picks up given number of bytes from a given file by:

$cut -f 5,10 file1

  • Displays 5th field to 10th field in file1. Each field is assumed to be separated by TAB character

UNIX file commands – grep

  • This is the good example of unfriendly UNIX command
  • Acronym for ‘Globally search a Regular Expression and Print it’
  • Searches for a pattern in file(s)

$grep abc file1

  • Searches for the string ‘abc’ in file1

UNIX file commands – head

  • Displays only first few lines of a file as per the argument

$head -15 file1

  • Displays only first 15 lines of file1

UNIX file commands – tail

  • Displays only last few lines of a file as per the argument

$tail -15 file1

  • Displays the last 15 lines of file1

UNIX file commands – pg/more

  • Both commands work almost like the same with few differences

$pg +5 -20 -p -s file1

  • Starts displaying file1 20 lines per page, starting from 5th line, with prompt for each page of display

UNIX file commands – lp

  • Sends the user print job to the print queue

$lp file1 file2

  • request id is lp-23(2 files)
  • Many flags available for different ways of printing
  • Can take the input from terminal

UNIX file commands – lpstat

  • Gives the printer status like which is currently printing, how many jobs are still in queue etc

$lpstat -t

scheduler is running

system default destination : lp

device for lp: /dev/lp0

lp accepting requests since <date>

  • Queue information

UNIX file commands – cancel

  • Used to cancel the given print job

$cancel lp-15

request “lp-15,226 cancelled

UNIX file commands – compress

  • Two commands provided for file compression namely compress and pack
  • They behave the same way

$compress -v file1

file1: Compression: 90.1%

  • Compresses the file file1 and creates a file by name file1.z

UNIX file commands – uncompress

  • Used to unpack the compressed file by:

$uncompress file1

  • Pack and compress will untouch the file if there is no effective compression

UNIX file commands – man

  • UNIX provides on-line manuals by:

$man <command>

  • Displays all the information about <command>

I/O Redirection & Piping

  • I/O can be redirected using ‘<‘ & ‘>’
  • ‘<‘ symbol implies – take the input from a file instead of keyboard
  • ‘>’ symbol implies – write the output in the file instead of display unit
  • ‘2>’ symbols implies – write the error in a file rather than display unit

e.g.:abc<in-file>out-file 2>err-file

  • A ‘>>’ indicates the appending of data to a file if it exists, instead of over-writing
  • 1>&2 indicates – redirect Standard Output to Standard Error
  • <Command-1> | <Command-2> means – the standard output of Command-1 is going to be the standard input of Command-2
  • UNIX supports joining of commands through ‘|’ piping character

$ls | wc -l

  • The output of ls is the input for wc

$ls | wc -l > outfile

  • Output of wc will be written in outfile
  • Output of first process will be written to pipe and the pipe then hands it over to the next process.
  • tee can be used to capture the pipe file

$who | tee file1 | sort

  • Output of who will be written into file1 and the file will be sorted and the sorted file will be written to standard output

UNIX Command – ps

  • To see the processes running currently under UNIX can be had by:

$ps

PID   TTY  TIME  COMMAND

2266  tty2 0:06  sh

2269  tty2 0.02  ps

  • Process Status command is shortly known as ps

UNIX Background Process

  • Time consuming tasks may be run in background
  • Place ‘&’ at the end of the command, the process will go to background
  • Displays the PID after submission
  • Success or failure of the background process will not be reported
  • Better to redirect the background process output to some file. Otherwise, it will disturb the foreground process messages on the screen
  • Too many background processes will degrade the performance
  • If logged out, all the background processes will be terminated

UNIX Command – Sleep

This command is used to delay a process for a period

of time

For Example :

To run a command at regular intervals, enter:

while true

do

date

sleep 60

done

This shell procedure displays the date and time once a minute.

To stop it, press the Interrupt key sequence.

Killing a Process

  • To terminate a process

$kill PID

  • An user can kill only his processes

UNIX Communication – write

  • Any user can ‘write’ a message on other user’s terminal.
  • Other user should give permissions to write message

$write <user-name>

hello

^d (Ctrl+d)

  • Hello will be written on user-name’s terminal
  • The <User-name> user should be logged-in to receive the message
  • The recipient should give permissions for message
  • Message writing can be denied by:

$mesg -n

  • Super user can write to any terminal without permissions

Shell Programming

  • A shell program is a series of UNIX commands
  • Instead of typing the commands one after the other, the commands may be written in a file and execute the file.
  • Shell programming offers much more versatility than mere UNIX commands
  • Almost a system can be developed by using shell script with UNIX commands
  • UNIX shell is the interface between the operating system & user
  • It is an interpreter, not an executable
  • A shell script can be executed by giving execute permissions ($chmod +x shell) or by shell command ($sh shell)
  • Shell programming is so powerful, even system shutdown like tasks can be handled
  • Three shells available under UNIX – Bourne, C & Korn shell
  • The following shell scripts were written in Bourne shell
  • Bourne shell script will work with Korn shell and the reverse is not true
  • Bourne is the default shell in most of the installations
  • Shell scripts are used to accomplish variety of tasks like:
  • Customizing the working environment
  • Automating the daily tasks like backups etc
  • Automating repetitive tasks
  • Executing important system procedures like shutdown
  • When the user logins into the system, the UNIX starts a shell (sh) on that terminal which accepts commands from ‘$’ prompt
  • When a shell-script is given for execution, the shell executes the commands from the shell-script

Interactive Shell Programming

  • Two basic words in shell are read & echo
  • read accepts input
  • echo writes the output

echo Enter your name ?

read name

echo Good morning $name

  • Write the above 3 lines in a file name sh01

$sh01

Enter your name ?

Vijay

Good morning Vijay

  • The ‘’ symbols makes the shell to understand ‘?’ as an ordinary character rather than special character
  • Double quotes (“) may be used to display a string
  • name is a shell variable receives data from keyboard
  • $name will display the contents of the variable

Interactive Shell Programming

echo Enter three values X Y Z

read x y z

echo $x $y $z

  • If 1 2 3 entered, x will have 1, 2 to y & 3 to z
  • If 1 2 3 4 are entered, then z will have 3 4
  • If only 1, 2 are entered, then z will be assigned null value
  • If more arguments entered, last variable will be assigned rest. If less, null will be assigned
  • echo This is new line nAnd this is second line
  • The above will be displayed as

This is new line

And this is second line

  • The escape sequence was brought from C language
  • b is blank, t is tab char etc.
  • To make the cursor at the end of the echoed line

echo “Enter your Name  :c”

Enter your Name  :_ (Cursor waits here)

  • echo “07” gives bell

Shell Variables

  • They are an integral part of shell programming
  • Provide ability to store and manipulate data
  • Fully under the control of the shell
  • Can create and destroy the variables as the user wants

Shell Variables – Guidelines

  • Any combination of alphabet, numbers and underscore( _ )
  • No editing characters like commas, blanks
  • The first character of the name should be alpha or underscore
  • May be of any reasonable length
  • Case sensitive ie abc & Abc are different

Shell – Assigning values

  • Values can be assigned to variable using ‘=‘ equal sign

$name=Vijay

$echo $name

displays Vijay

Shell Variables

  • There are 2 types of variables
  • 1. UNIX defined or system defined variables
  • 2. User defined variables

System Variables

  • UNIX defines certain variables for its own usage

e.g.: PS1 stands for prompt string1

$PS1=ABC

  • From this time onwards, UNIX displays ABC as prompt
  • PS2 is prompt string 2, default value is ‘>’
  • PATH defines the directories to searched by shell to execute any command or file
  • HOME defines the default working directory
  • LOGNAME stores the log name of the user
  • MAIL stores the file name of the mailbox along path
  • MAILCHECK duration of the mail checking
  • SHELL keeps the default shell name
  • TERM gives the name of the terminal the user is using
  • All the above variable values can be known by set command

$set

  • Displays all the values

Tips & Traps

  • All the variables are string variables
  • $a=20. 20 is stored as a string not as number. No arithmetic can be carried on this
  • Use double quotes if the value contains more than one word

$name=“Vijay Suri”

  • Can assign more than one variable in a line

$name=“Vijay” age=20

  • Can display more than one variable in a line

$echo Name is $name and age is $age

  • All variables in a shell script are automatic variable. i.e. they will be created as soon as the shell script execution starts and dies as soon as the execution is over
  • A null variable can be created by

$a1=“” or a1=‘’ or a1=

  • Shell ignore if any shell variable is having null value

Unchanging Variable – readonly

  • User can make a variable unchanged during execution by:

$age=20

$readonly age

  • The the shell does not allow us to change the value

Wiping out Variable – unset

  • A variable can be removed from the shell by using unset command

$unset age

  • Unset can not be used for system variables
  • $unset PS1 is not allowed

Positional Parameters

  • Many occasions, a program expects the variables in a certain fashion. This is achieved through positional parameters from $0 through $9.
  • $0 is the program itself. Thus

$abc par1 par2 par3 par4 assigns

abc to $0, par1 to $1 … par4 to $4

Passing command line arguments

  • To write a shell by name sh02 which copies one file to another

echo Copying $1 to $2

cp $1 $2

  • By executing sh02

$sh02 file1 file2

Copying file1 to file2.

  • Positional parameters can not assigned values like $1=100

Shell Variables

  • To know the number of variables given for the shell through $#

$abc file1 file

echo $#

  • The above shell displays 3

Shell Variables – shift

  • Shell can handle only 9 variables at a time. To access more than 9, the shift command is used
  • $set You have the capacity to learn the shell programming in a very easy way
  • $echo $10 displays You0 as shell interprets $10 as $1 with 0.
  • $shift 5 : makes the 5th argument as $1 argument. Thus
  • $echo $1 displays learn
  • $* handles all positional parameters

Positional Parameters

Positional Parameters

If the number of parameters are greater than nine

The shift command can be used to shift the parameters

For Example :

$ set a b c d e f g h i j k l m n o p

$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9

a b c d e f g h i

$ shift 7

$ echo $1 $2 $3 $4 $5 $6 $7 $8 $9

h I j k l m n o p

Arithmetic in Shell

  • Write the following in a file and execute:

a=10 b=4

echo ‘expr $a + $b’

echo ‘expr $a – $b’

echo ‘expr $a * $b’

echo ‘expr $a / $b’

echo ‘expr $a % $b’    #modulus

  • On execution, the output is:

14

6

40

2

2

  • Anything after ‘#’ sign will be treated as comment
  • expr is the key word for doing arithmetic
  • A multiplication symbol (*) should be preceded by ‘’
  • Terms in expr should be separated by space
  • Parenthesis may be used for clarity of expression
  • Arithmetic in shell scripts
    • The expr can be used only for integers
    • For Floating point the command  bc is used

# example of floating point arithmetic program

a=15.5 b=7.8

c=‘echo $a + $b | bc‘   (returns 23.3)

d=‘echo $a – $b | bc‘   (returns 7.7)

e=‘echo $a * $b | bc‘  (returns 120.9)

f=‘echo $a / $b | bc‘    (returns 1)

UNIX decision loops

  • There are 4 decision making loops

if then fi

if then else fi

if then elif else fi

case – esac

UNIX – if then fi

  • Key word if and the delimiter is ‘fi’

if <command>

then

statements

fi

e.g.: if cp $1 $2

then echo “Copied successfully”

fi

UNIX – if then else fi

  • The structure of this construct is:

if <Condition>

then

statements

else

statements

fi

e.g.:

if cp $1 $2

then

echo File copied successfully

else

echo File copy failed

fi

Example “if – else – fi

# Example of numeric test

echo Enter basic salary

read bs

if [ $bs -lt 2500 ]

then

hra=‘echo $bs * 10 / 100 | bc‘

da=‘echo $bs * 90 / 100 bc‘

else

hra=500

da=‘echo $bs * 98 / 100 | bc‘

fi

gs=‘echo $bs + $hra + $da | bc‘

echo Gross salary = Rs. $gs

UNIX – if then elif else fi

  • Used for multilevel decision making.

if <condition> then

statements

elif <condition>

statements

else

statements

fi

UNIX – test

  • if depends upon the exit status of the command given
  • test verbs translates the result into success or failure
  • There are three tests namely
  • Numerical test
  • String test
  • File test

UNIX – test – numerical

  • Used to compare numerical

-gt = greater than

-lt  = less than

-ge = greater than or equal to

-le  = less than or equal to

-ne = not equal

-eq = equal

if [ $1 -lt 5 ]

then

echo the value is < 5

elif [ $1 -le 7 ]

echo the value is <= or equal 7

else

echo the value is > 7

fi

UNIX – test

  • Use square braces to avoid writing test
  • Provide a space after ‘[‘
  • Provide a space before ‘]’

UNIX – test – file

  • The following are the file related flags
  • -s returns True if the file exists and size > 0
  • -f returns True if the file exists and not directory
  • -d return True if the file exists and is a directory

e.g.:

if [ -f $1 ] then

echo File exists

fi

UNIX – test – String

  • s1 = s2 returns true if both are same
  • -n returns true if string length > 0

if [ $1 = $2 ] then

echo Both strings are same

else

echo $?

fi

  • $? Contains the value of the last command
  • 0 = true, 1 = false

UNIX logical conditions

  • -a stands for AND condition
  • -o stands for OR condition
  • -! Is negation
  • if <Condition-1> -a <Condition-2>
  • Returns true if both the conditions are true

if [ $1 -gt 60 ] -a [ $2 -lt 50 ] then

statements…

UNIX case

  • To handle multiple choices
  • case value in
  • choice 1) statements;;
  • choice 2) statements;;
  • *) statements;;
  • esac
  • esac is the delimiter of case
  • Used for menus
  • *) is the default choice
  • All choice statements should be terminated by double semicolon(;;)

UNIX case example

case $option in

1) echo Financial accounting;;

2) echo Materiel accounting;;

*) echo Invalid Opt – Try;;

esac

UNIX case

  • The choices many be in any order
  • case statement may be a shell variable or shell argument or output of a command
  • Need not be numbers – may be strings too

e.g.:

banana) statements;;

orange) statements;;

  • Multiple options can be grouped

e.g.:

case $1 in

banana | orange) echo Fruit;;

dog | pig) echo Animal;;

lion) echo Wild animal;;

esac

  • Can use shell’s pattern matching

e.g.:

case $1 in

[a-z]) echo Small alpha;;

[A-Z]) echo Capital;;

esac

UNIX Loop Controls

  • Provided 3 loop constructs namely:
  • while loop
  • for loop
  • until loop

UNIX – while loop

while <condition>

do

statements

done

  • done is the delimiter of do

UNIX – while example

count = 1

while [ $count -le 3 ]

do

echo Loop value $count

count = `expr $count + 1`

done

Examples of “while” loop

# calculation of simple interest

count=1

while [ $count -le 3 ]

do

echo “nEnter p,n,rc”

read p n r

si=‘echo $p * $n * $r /100 | bc‘

echo Simple interest = Rs.$si

count=‘expr $count + 1‘

done

# printing numbers using while

i=1

while [ $i -le 10 ]

do

echo $i

i=‘expr $i + 1‘

done

UNIX – while

  • The loop will continue so long as the condition is TRUE
  • When the condition is false, the next command after done will be executed
  • The condition can be any valid UNIX command
  • The while condition can be simple condition or complex condition
  • The condition should have an exit status. Otherwise, it may go into infinite loop

UNIX – until loop

until <condition>

do

statements

done

  • until continues its loop so long as the condition is false
  • except this, while & until are identical

Example – until loop

# printing numbers using until

i=1

until [ $i -gt 10 ]

do

echo $1

i=‘expr $i + 1‘

done

UNIX – for loop

  • Most frequently used loop

for control-var in value1 value2…

do

statements

done

  • for takes a list of variables

UNIX – for example

for word in $*

do

echo $word

done

Example – for loop

Example

# To print names of all sub-directories

for entry in *

do

if [ -d $entry ]

then

echo $entry

fi

done

UNIX – break statement

  • Used to break the current loop and comes out of the loop
  • Usually associated with if

if [ $1 -eq 5 ]

then

I = 2

break

fi

UNIX – continue statement

  • To take the control to the beginning of the loop bypassing the statements

I=1

while [ $I -le 5 ] then

do

I = `expr $I + 1`

continue

done

UNIX – Metacharacters

  • Called as regular expressions.
  • Classified as follows:

File name              : ? * […] [!…]

I/O redirection     : < > >> << m> m>&n

Process execution: ; ( ) & && ||

Positional paras   : $1..$9

Spl characters      : $0 $* [email protected] $# $! $$ $-

UNIX – File name

  • ls ?? – lists all files with 2 chars long
  • ls a* – lists all file names begin with a
  • ls [a-c]* -file names begin with a,b &c
  • ls [!a-c] – file names not starting with a,b,c

UNIX – I/O Redirection

  • < – take input from
  • > write output to
  • >> append output
  • << abc – takes the input till ‘abc’ encountered
  • m> filename – Makes filename as output of m
  • m<&n – Merges stdin, stdout and stderr

UNIX – Process Execution

  • ; – multiple commands separated by ‘;’
  • & – puts to background process
  • && means AND – Comd1 && Comd2 : Executes Comd2 if and only if Comd1 is successful
  • || means OR – Comd1 || Comd2 – Executes Comd2 if Comd1 fails

UNIX – Special Characters

  • $$ – Gives the Process id of the command
  • $? – Exit status of the last executed command
  • $! – PID of last background process
  • $# – Total number of positional parameters
  • $0 – Command name
  • $* – list all shell arguments

Tips & Traps

  • Try to use the absolute paths in the shell scripts. This saves the search time of the system for the command
  • Remove write permissions to files that are important. This even prevents the accidental update of the files
  • Compress the print/report files. These files occupies lot of disk space. Better pack them and keep
  • The compressed print files can be printed directly without uncompress them by using the pcat command
  • Untouched files in the directory may be found using find command.
  • $find . -mtime 10 -print
  • displays the files that are untouched for the last 10 days
  • Clean up of the directory should be done periodically. Otherwise, any amount disk space will be inadequate.

$pcat textfile.txt.z > /dev/lp0

  • The above command will send the print file directly to printer