0% found this document useful (0 votes)
67 views9 pages

Shell Commands-B.keerthi Samhitha

The document describes various shell commands used for file handling, text processing, system administration, process management, archiving, and advanced commands. It also provides examples of shell programs for searching a substring in text, a menu-based math calculator, converting filenames to uppercase, printing patterns using loops, and showing system information.

Uploaded by

Vamsi Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views9 pages

Shell Commands-B.keerthi Samhitha

The document describes various shell commands used for file handling, text processing, system administration, process management, archiving, and advanced commands. It also provides examples of shell programs for searching a substring in text, a menu-based math calculator, converting filenames to uppercase, printing patterns using loops, and showing system information.

Uploaded by

Vamsi Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SHELL COMMANDS

File Handling commands


• mkdir – make directories
Usage: mkdir [OPTION] DIRECTORY...
eg. mkdir prabhat
• ls – list directory contents
Usage: ls [OPTION]... [FILE]...
eg. ls, ls l,
ls prabhat
• cd – changes directories
Usage: cd [DIRECTORY]
eg. cd prabhat
 pwd -print name of current working directory
Usage: pwd
• vim – Vi Improved, a programmers text editor
Usage: vim [OPTION] [file]...
eg. vim file1.txt
cp – copy files and directories
Usage: cp [OPTION]... SOURCE DEST
eg. cp sample.txt sample_copy.txt
cp sample_copy.txt target_dir
 mv – move (rename) files
Usage: mv [OPTION]... SOURCE DEST
eg. mv source.txt target_dir
mv old.txt new.txt
rm remove
files or directories
Usage: rm [OPTION]... FILE...
eg. rm file1.txt , rm rf some_dir
• find – search for files in a directory hierarchy
Usage: find [OPTION] [path] [pattern]
eg. find file1.txt, find name file1.txt
• history – prints recently used commands
Usage: history
SHELL COMMANDS

Pattern
A Pattern is an expression that describes a set of strings which is used to give a concise
description
of a set, without having to list all elements.
eg. ab*cd matches anything that starts with ab and ends with cd etc.
ls *.txt – prints all text files

Text Processing
• cat – concatenate files and print on the standard output
Usage: cat [OPTION] [FILE]...
eg. cat file1.txt file2.txt
cat n file1.txt
• echo – display a line of text
Usage: echo [OPTION] [string] ...
eg. echo I love India
echo $HOME
• wc print
the number of newlines, words, and bytes in files
Usage: wc [OPTION]... [FILE]...
eg. wc file1.txt
wc L file1.txt
sort – sort lines of text files
Usage: sort [OPTION]... [FILE]...
eg. sort file1.txt
sort r file1.txt

System Administration
• chmod – change file access permissions
Usage: chmod [OPTION] [MODE] [FILE]
SHELL COMMANDS

eg. chmod 744 calculate.sh


• chown – change file owner and group
Usage: chown [OPTION]... OWNER[:[GROUP]] FILE...
eg. chown remo myfile.txt
su – change user ID
Usage: su [OPTION] [LOGIN]
eg. su remo, su
• passwd – update a user’s authentication tokens(s)
Usage: passwd [OPTION]
eg. Passwd
• who – show who is logged on
Usage: who [OPTION]
eg. who , who b, who q
Process management
ps – report a snapshot of the current processes
Usage: ps [OPTION]
eg. ps, ps el
• kill – to kill a process(using signal mechanism)
Usage: kill [OPTION] pid
eg. kill 9

Archival
tar – to archive a file
Usage: tar [OPTION] DEST SOURCE
eg. tar cvf
/home/archive.tar /home/original tar xvf/home/archive.tar
• zip – package and compress (archive) files
Usage: zip [OPTION] DEST SOURSE
eg. zip original.zip original
• unzip – list, test and extract compressed files in a ZIP archive
Usage: unzip filename
eg. unzip original.zip
 du – estimate file space usage
SHELL COMMANDS

Usage: du [OPTION]... [FILE]...


eg. du
• df – report filesystem disk space usage
Usage: df [OPTION]... [FILE]...
eg. df
• quota – display disk usage and limits
Usage: quota [OPTION]
eg. quota v

Advanced Commands
• reboot – reboot the system
Usage: reboot [OPTION]
eg. reboot
• poweroff – power off the system
Usage: poweroff [OPTION]
eg. Poweroff
SHELL COMMANDS

SHELL PROGRAMS
SEARCHING A SUBSTRING IN GIVEN TEXT

PROGRAM:

echo Enter main string:


read main
l1=`echo $main | wc -c`
l1=`expr $l1 - 1`
echo Enter sub string:
read sub
l2=`echo $sub | wc -c`
l2=`expr $l2 - 1`
n=1
m=1
pos=0
while [ $n -le $l1 ]
do
a=`echo $main | cut -c $n`
b=`echo $sub | cut -c $m`
if [ $a = $b ]
then
n=`expr $n + 1`
m=`expr $m + 1`
pos=`expr $n - $l2`
r=`expr $m - 1`
if [ $r -eq $l2 ]
then
break
fi
else
pos=0
m=1
n=`expr $n + 1`
fi
done
echo Position of sub stringin main string is $pos
SHELL COMMANDS

OUTPUT:

Enter main string:

This is a shell pgm

Enter sub string:


shell

Position of sub stringin main string is 11

MENU BASED MATH CALCULATOR

PROGRAM:

echo “ Menu Based Calculator”


echo "Enter the Operands"
read a
read b
echo "Enter the Operator"
read o
case $o in
"+" ) echo “$a + $b” = `expr $a + $b`;;
"-" ) echo “$a + $b” = `expr $a - $b`;;
"*" ) echo “$a + $b” = `expr $a * $b`;;
"/" ) echo “$a + $b” = `expr $a / $b`;;
* ) echo " Inavlid Operation"
esac

OUTPUT:

Menu Based Calculator


Enter the Operands
4
6
Enter the Operator
+
4 + 6 = 10
SHELL COMMANDS

CONVERTING ALL FILENAMES FROM LOWERCASE TO UPPERCASE

PROGRAM

for i in *
do
echo Before Converting to uppercase the filename is
echo $i
j=`echo $i | tr '[a-z]' '[A-Z]'`
echo After Converting to uppercase the filename is
echo $j
mv $i $j
done

OUTPUT

Before Converting to upper case the filename is


cse.sh
After Converting to uppercase the filename is
CSE.SH

PRINTING PATTERN USING LOOP STATEMENT

PROGRAM

echo "Enter the Limit "


read n
echo "Pattern"
for (( i = 1 ; i < $n ; i++ ))
do
for (( j = 1 ; j <= i ; j++ ))
do
echo -n " $ "
done
echo " "
done
SHELL COMMANDS

OUTPUT

Enter the Limit

Pattern

$
$ $
$ $ $

CONVERTING THE FILENAME FROM UPPERCASE TO LOWERCASE

PROGRAM

echo –n “Enter the Filename”


read filename
if [ ! -f $filename ];
then
echo “Filename $filename does not exists”
exit 1
fi
tr ‘[A-Z]’ ‘[a-z]’ < $filename

OUTPUT

Enter the Filename


CSE.sh

cse.sh
SHELL COMMANDS

SHOWING VARIOUS SYSTEM INFORMATION

PROGRAM

echo "SYSTEM INFORMATION"


echo “Hello ,$LOGNAME”
echo “Current Date is = $(date)”
echo “User is ‘who I am’”
echo “Current Directory = $(pwd)”
echo "Network Name and Node Name = $(uname -n)"
echo "Kernal Name =$(uname -s)"
echo "Kernal Version=$(uname -v)"
echo "Kernal Release =$(uname -r)"
echo "Kernal OS =$(uname -o)"
echo “Proessor Type = $(uname -p)”
echo “Kernel Machine Information = $(uname –m)”
echo "All Information =$(uname -a)"

OUTPUT

SYSTEM INFORMATION
Hello, 3CSE-A
Current date is = Mar 17 08:38:58 IST 2014
Kernal Name = Linux
User is Who I am
Current Directory = 11scs122
Network name and Node name = linuxmint
Kernal Versio n= #1-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010
Kernal OS = GNU/Linux
kernal release =2.6.32-21-generic
Kernal Processor Type = 2.6.33.85.fcl3.i686.PAE
Kernal All Information = Linux main lab 2.6.33.85.fcl.3 i686.PAE
= #1-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010
I686 i686 i686 GNU/Linux

You might also like