27062024123806PM
27062024123806PM
COMMANDS
1) cal:- Displays a calendar
Syntax:- cal [options] [ month ] [year]
Description :-
cal displays a simple calendar. If arguments are not specified, the current month is
displayed. The switching options are as follows:
-1 Display single (current) month output. (This is the default.)
-3 Display prev/current/next month output
-s Display Sunday as the first day of the week (This is the default.)
-m Display Monday as the first day of the week
-j Display Julian dates (days one-based, numbered from January 1)
-y Display a calendar for the current year
Example:-
$cal
or
$cal 02 2016
Feb 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
Example:-
$ pwd
/home/kumar
$ cd progs
$ pwd
/home/kumar/progs
$ cd ..
/home/kumar
6) exit :- It is used to terminate a program, shell or log you out of a network normally.
Syntax :- exit
8) who :- who command can list the names of users currently logged in, their terminal, the
time they have been logged in, and the name of the host from which they have logged in.
Syntax :- who [options] [file]
Description:-
am i Print the username of the invoking user, The 'am' and 'i' must be space separated.
-b Prints time of last system boot.
-d print dead processes.
-H Print column headings above the output.
Include idle time as HOURS:MINUTES. An idle time of . indicates activity within the
-l
last minute.
-m Same as who am i.
-q Prints only the usernames and the user count/total no of users logged in.
Example :-
$ who
dietstaffpts/1 2016-02-20 22:42 (:0.0)
dietstaffpts/2 2016-02-20 09:30 (:0.0)
Here first column shows user name, second shows name of the terminal the user is working
on. Third& fourth column shows date and time of logging, last column shows machine name.
-p Allow users to remove the directory and its parent directories which become empty.
12) bc:- bc command is used for command line calculator. It is similar to basic calculator. By using
which we can do basic mathematical calculations.
Syntax :- bc [options]
Description :-
bc is a language that supports arbitrary precision numbers with interactive execution of
statements.
bc starts by processing code from all the files listed on the command line in the order listed.
After all files have been processed, bc reads from the standard input. All code is executed as
it is read.
Example:-
$ bc
bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free
software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 2*3 6
The above command used is for mathematical calculations.
$ bc -l
bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free
software with ABSOLUTELY NO WARRANTY.For details type `warranty'.11+2 13
The above command displays the sum of '11+2'.
$ bc calc.txt
bc 1.06 Copyright 1991-1994,1997,1998,2000 Free Software Foundation,Inc. This is free
software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 13
'calc.txt' file have the following code:11+2. Get the input from file and displays the output.
14) tty:- Print the file name of the terminal connected to standard input.
Syntax :- tty
Description :-
tty writes the name of the terminal that is connected to standard input onto standard output.
Command is very simple and needs no arguments.
Example :-
$tty
/student/tty10
Example :-
$ sty
speed 19200 baud, 25 rows, 79 columns
kill = ^U
$ cat file1.c
process management
memory management
file mgmt
$ cat file1.c >> file2.c
It can concatenate the contents of two files. For this you have to use append output
redirection operator.
The contents of file2.cwill be appended to file1.c.
17) cp:- cp command copy files from one location to another. If the destination is an existing file, then the
file is overwritten; if the destination is an existing directory, the file is copied into the directory (the
directory is not overwritten).
Syntax :- cp [options]... source destination
Description:-
Here, after cp command contents of both source file and destination file files are the same.
It will copy the content of source file to destination file.
If the destination file doesn’t exist, it will be created.
If it exists then it will be overwritten without any warning.
If there is only one file to be copied then destination can be the ordinary file or the directory
file.
-a archive files
-f force copy by removing the destination file if needed
-i interactive - ask before overwrite
-l link files instead of copy
-L follow symbolic links
-n no file overwrite
-u update - copy when source is newer than dest
Example:-
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
$ cp file1 file2
The above cp command copies the content of file1.php to file2.php.
Copy folder and subfolders:
$ cp-R scripts scripts1
The above cp command copy the folder and subfolders from scripts to scripts1
os.
-f mv will move the file(s) without prompting even if it is writing over an existing
target. Note that this is the default if the standard input is not a terminal
-i Prompts before overwriting another file
Example:-
$ cat file1
Memory
Process
Files
$ mv file1 file2
rename file1 to file2
If the destination file doesn’t exist it will be created. mv can also be used to rename a
directory. A group of files can also be moved to a directory. mv doesn’t prompt for
overwriting destination file if it exists.
20) nl :-
nl numbers the lines in a file.
Syntax: - nl [OPTION] [FILE]
Example :-
$cat list.txt
apples
oranges
potatoes
lemons
garlic
$nl list.txt
1 apples
2 oranges
3 potatoes
4 lemons
5 garlic
In the above example, we use the cat command to display the contents of list.txt. Then we
use nl to number each line and display the result to standard output.
$nl list.txt > nlist.txt
$cat nlist.txt
1 apples
2 oranges
3 potatoes
4 lemons
5 garlic
In the above example, we run the same nl command, but redirect the output to a new file,
nlist.txt. Then we use cat to display the results.
21) cut :- cut command is used to cut out selected fields of each line of a file. The cut command uses
delimiters to determine where to split fields.
Syntax :- cut [options] filename
Description :-
file A path name of an input file. If no file operands are specified, or if a file operand is -, the
standard input will be used
-c The list following -c specifies character positions
-d The character following -d is the field delimiter
-f select only these fields on each line
-b Select only the bytes from each line as specified in LIST
Example :-
For example, let's say you have a file named data.txt which contains the following text:
one two three four five
alpha beta gamma delta epsilon
In this example, each of these words is separated by a tab character, not spaces. The tab
character is the default delimiter of cut, so it will by default consider a field to be anything
delimited by a tab.
To "cut" only the third field of each line, use the command:
$ cut -d’ ‘ -f 3 data.txt
three
gamma
let's say you want the third field and every field after it, omitting the first two fields. In this
case, you could use the command:
$ cut -d’ ‘ -f 3- data.txt
three four five
gamma delta epsilon
$ cut -c 3 file.txt
r
m
For example, to output only the third-through-twelfth character of every line of data.txt, use
the command:
$ cut -c 3-12 data.txt
e two thre
pha beta g
22) paste:- paste command is used to paste the content from one file to another file. It is also used to
set column format for each line.
Syntax :- paste [option] file
Description :-
Paste prints lines consisting of sequentially corresponding lines of each specified file. In the
output the original lines are separated by TABs. The output line is terminated with a newline.
Display the contents of file myfile.txt, beginning at the first line containing the string "hope".
24) cmp:- It compares two files and tells you which line numbers are different.
Syntax : - cmp [options..] file1 file2
Description :-
Let’s create a file named os2. And use cmp command to compare os and os1files.
- c Output differing bytes as characters.
-l Print the byte number (decimal) and the differing byte values (octal) for each difference.
- s Prints nothing for differing files, return exit status only.
- c Output differing bytes as characters.
Example:-
$ cat file1
memory
process
files
$ cat > file2
memory
process
files mgmt
$ cmp file1 file2
File1 file2 differ: char 21, line 3
The two files are compared byte by byte and the location of the first mismatch is echoed to
the screen. cmp doesn’t bother about possible subsequent mismatches.
For example let’s create two files named file1 and file2 with following data.
$ cat > file1
c.k.shukla
chanchalsanghvi
s.n.dasgupta
sumit [1] + Stopped
Example :-
$ cat file1
f1.c
f2.c
f3.c
f4.c
f5.c
$ cat file2
f1.c
f3.c
f4.c
f6.c
f7.c
Now, When you run the comm command on these files, this is what you get:
$ comm file1 file2
f1.c
f2.c
f3.c
f4.c
f5.c
f6.c
f7.c
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
The output is split in 3 columns. Column1 indicates files which are unique in file1, column 2
indicates files unique to file2. Column 3 indicates files common between them. comm
command provides some real good options with which you can filter the output better.
Now, say you want to find out only the list of files which were there in the older version but
not in the newer version:
$ comm -23 file1 file2
f2.c
f5.c
The option -23 indicates to remove the second and third columns from the comm command
output, and hence we are left with only the first column which is the files unique in file1.
Similarly, to find out the list of files which were not there in the old version, but has been
added in the new version:
$ comm -13 file1 file2
f6.c
f7.c
As explained above, -13 option tells to remove the first and third columns from the comm
output.
Finally, to know the list of files which have been retained, or common in both the versions:
$ comm -12 file1 file2
f1.c
f3.c
f4.c
When you apply comm command on files, the files should be sorted. This command works
only on sorted files.
26) diff:- It is used to find differences between two files.
Syntax :- diff [options..] fileone filetwo
Description :-
Diff is the third command that can be used to display file differences. Unlike its fellow
members ,cmp and comm. , it also tells us which lines in one file have to be changed to
make the two files identical.
-b Ignore any changes which only change the amount of whitespace (such as spaces or
tabs).
-w Ignore whitespace entirely.
-B Ignore blank lines when calculating differences.
-y Display output in two columns.
-i Ignore changes in case.consider upper- and lower-case letters equivalent.
Example:-
$ cat 1.txt
aaa
bbb
ccc
ddd
eee
fff
ggg
$ cat 2.txt
bbb
cc
ddd
eee
fff
ggg
hhh
$ diff 1.txt 2.txt
1d0
< aaa
3c2
< ccc
---
>cc
7a7
> hhh
Lines like "1d0" and "3c2" are the coordinates and types of the differences between the two
compared files, while lines like "< aaa" and "> hhh" are the differences themselves.
diff change notation includes 2 numbers and a character between them. Characters tell you
what kind of change was discovered:
d – a line was deleted
c – a line was changed
a – a line was added
Number to the left of the character gives you the line number in the original (first) file, and
the number to the right of the character tells you the line number in the second file used in
comparison.
So, looking at the two text files and the diff output above, you can see what happened:
1d0
< aaa
This means that 1 line was deleted. < aaa suggests that the aaa line is present only in the
original file.
3c2
< ccc
---
>cc
And this means that the line number 3 has changed. You can see how this confirms that in
the first file the line was "ccc", and in the second it now is "c c".
7a7
> hhh
Finally, this confirms that one new line appeared in the second file, it's "hhh" in the line
number 7.
27) chmod:- chmod command allows you to alter / Change access rights to files and directories.
Syntax:- chmod [options] [MODE] FileName
Description :-
chmod command is used to set the permissions of one or more files for all three categories
of users (user,group and others ). It can be run only by the user and super user. Command
can be used in two ways. Let’s first take a look at the abbreviations used by chmod
command.
Category Operation Permission
u User + Assigns permission r Read permission
g Group - Removes permission w Write permission
o Others = Assigns absolute x Execute permission
File Permission :
# File Permission
0 none
1 execute only
2 write only
3 write and execute
4 read only
5 read and execute
6 read and write
7 set all permissions
Relative permissions
When changing permissions in a relative manner, chmod only changes the permissions
specified in the command line and leaves the other permissions unchanged. In this mode it
uses the following syntax:
let’s first check the permission of file shortlist. It shows read/write permission. Let’s assign
execute permission to this file for owner.
$ ls –l
-rw-r--r—1 shortlist None 241 Feb 21 04:02
$ chmod u+x shortlist
$ ls -l
-rwxr--r-- 1 shortlist None 241 Feb 21 04:02
Here chmod uses expression u+x means – u shows user category that is user, + shows we
need to assign the permission and x shows execute permission. Sameway we can assign
other permissions.
Absolute permissions
Sometimes you don’t need to know what a file’s current permissions are, but want to set all
nine permission bits explicitly. The expression used by chmod here is a string of three octal
numbers. Each type of permission is assigned a number as shown:
Read permission-4
Write permission-2
Execute permission -1
For each category we add numbers that represent the assigned permission. For instance , 6
represents read and write permission , and 7 represents all permissions.
Example :
$ chmod 666 myfile or(chmod u=rw,g=rw,o=rwmyfile) shortlist
Shows read and wrie (4 +2) permissions for all three types of users.
28) chown:- Command for system V that changes the owner of a file.
Syntax :- chown [options] newowner filename/directoryname
Description:-
Change the permission on files that are in the subdirectories of the directory that you are
-R
currently in.
-c Change the permission for each file.
Prevents chown from displaying error messages when it is unable to change the ownership
-f
of a file.
Example :-
$ ls -l demo.txt
-rw-r--r-- 1 root root 0 Jan 31 05:48 demo.txt
$ chownvivek demo.txt
-rw-r--r-- 1 vivek root 0 Jan 31 05:48 demo.txt
29) chgrp:- chgrp command is used to change the group of the file or directory. This is an admin
command. Root user only can change the group of the file or directory.
Syntax:- chgrp [options] newgroup filename/directoryname
Description:-
Change the permission on files that are in the subdirectories of the directory that you
-R
are currently in.
-c Change the permission for each file.
-f Force. Do not report errors.
Example :-
$ ls -l demo.txt
-rw-r--r-- 1 root root 0 Jan 31 05:48 demo.txt
$ chgrp vivek demo.txt
-rw-r--r-- 1 root vivek 0 Jan 31 05:48 demo.txt
30) file:- file command tells you if the object you are looking at is a file or a directory.
Syntax:- file [options] directoryname/filename
Description:-
File command is used to determine the type of file, especially of an ordinary file. We can use
it with one or more filenames as arguments. For example we can use file command to check
the type of the os1 file that we have created.
$ file os1
os1: short english text
31) finger:- finger command displays the user's login name, real name, terminal name and write status
(as a ''*'' after the terminal name if write permission is denied), idle time, login time, office location
and office phone number.
Syntax:- finger [username]
Description :-
-l Force long output format
-s Force short output format
Example :-
$ finger rahul
Login: abc Name: (null)
Directory: /home/abc Shell: /bin/bash
On since Mon Nov 1 18:45 (IST) on :0 (messages off)
On since Mon Nov 1 18:46 (IST) on pts/0 from :0.0
New mail received Thu Feb 7 10:33 2015 (IST)
Unread since Mon Feb 8 12:59 2016 (IST)
No Plan.
35) ps:- It is used to report the process status. ps is the short name for Process Status.
Syntax:- ps [options]
Description :-
-a List information about all processes most frequently requested: all those except process
group leaders and processes not associated with a terminal
-A List information for all processes. Identical to -e, below
-f Generate a full listing
-j Print session ID and process group ID
-l Generate a long listing
Example :-
$ps
PID TTY TIME CMD
291 console 0:00 bash
36) ln :- ln command is used to create link to a file (or) directory. It helps to provide soft link for desired
files.
Syntax:- ln [options] existingfile(or directory)name newfile(or directory)name
Description:-
What Is A Link?
A link is an entry in your file system which connects a filename to the actual bytes of data on
the disk. More than one filename can "link" to the same data. Here's an example. Let's
create a file named file1.txt:
$ echo "This is a file." > file1.txt
This command echoes the string "This is a file". Normally this would simply echo to our
terminal, but the > operator redirects the string's text to a file, in this case file1.txt
When this file was created, the operating system wrote the bytes to a location on the disk
and also linked that data to a filename, file1.txt so that we can refer to the file in commands
and arguments.
If you rename the file, the contents of the file are not altered; only the information that points
to it.
The filename and the file's data are two separate entities.
File1.txt
“This is a file””
What the link command does is allow us to manually create a link to file data that already
exists.
So, let's use link to create our own link to the file data we just created. In essence, we'll
create another file name for the data that already exists.
$ link file1.txt file2.txt
The important thing to realize is that we did not make a copy of this data. Both filenames
point to the same bytes of data on the disk. Here's an illustration to help you visualize it:
File1.txt File2.txt
“This is a file”
If we change the contents of the data pointed to by either one of these files, the other file's
contents are changed as well. Let's append a line to one of them using the >>operator:
$ echo "It points to data on the disk." >> file1.txt
Now let's look at the contents of file1.txt:
$ cat file1.txt
This is a file.
now let's look at the second file, the one we created with the link command.
$ cat file2.txt
This is a file.
ln, by default, creates a hard link just like link does. So this ln command:
$ ln file1.txt file2.txt
It is the same as the following link command. Because, both commands create a hard link
named file2.txt which links to the data offile1.txt.
$ link file1.txt file2.txt
However, we can also use ln to create symbolic links with the -s option. So the command:
$ ln -s file1.txt file2.txt
It will create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example,
here's an illustration to help you visualize our symbolic link:
File1.txt File2.txt
“This is a file””
You should also be aware that, unlike hard links, removing the file (or directory) that a
symlink(symbolic linkl) points to will break the link. So if we create file1.txt:
$ echo "This is a file." > file1.txt
Now, create a symbolic link to it:
$ ln -s file1.txt file2.txt
we can cat either one of these to see the contents:
$ cat file1.txt
This is a file.
$ cat file2.txt
This is a file.
But, if we remove file1.txt:
$ rm file1.txt
we can no longer access the data it contained with our symlink:
$ cat file2.txt
cat: file2.txt: No such file or directory
-s Makes it so that it creates a symbolic link
-f If the destination file or files already exist, overwrite them.
37) head:- head command is used to display the first ten lines of a file, and also specifies how many lines
to display.
Syntax:- head [options] filename
Description:-
Head command displays the top of the file. When used without an option , it displays the first
ten lines of the file.
-n To specify how many lines you want to display.
The number option-argument must be a decimal integer whose sign affects
-n number
the location in the file, measured in lines.
The number option-argument must be a decimal integer whose sign affects
-c number
the location in the file, measured in bytes.
Example :-
$ head myfile.txt
Display the first ten lines of myfile.txt.
$ head -15 myfile.txt
Display the first fifteen lines of myfile.txt.
$ head -c 20 myfile.txt
Will output only the first twenty bytes (characters) of myfile.txt. Newlines count as a single
character, so if head prints out a newline, it will count it as a byte.
$ head -n 5K myfile.txt
38) tail:- tail command is used to display the last or bottom part of the file. By default it displays last 10
lines of a file.
Syntax :- tail [options] filename
Description:-
-l To specify the units of lines.
-b To specify the units of blocks.
-n To specify how many lines you want to display.
The number option-argument must be a decimal integer whose sign
-c number
affects the location in the file, measured in bytes.
The number option-argument must be a decimal integer whose sign
-n number
affects the location in the file, measured in lines.
Example : -
$ tail index.txt
It displays the last 10 lines of 'index.txt'.
$ tail -2 index.txt
It displays the last 2 lines of 'index.txt'.
$ tail -n 5 index.txt
It displays the last 5 lines of 'index.txt'.
$ tail -c 5 index.txt
It displays the last 5 characters of 'index.txt'.
Example:-
$ cat > data.txt
apples
oranges
pears
kiwis
bananas
$ sort data.txt
apples
bananas
kiwis
oranges
pears
Note that this command does not actually change the input file, data.txt. If you want to write
the output to a new file, output.txt, redirect the output like this:
$ sort data.txt > output.txt
it will not display any output, but will create the file output.txt with the same sorted data from
the previous command. To check the output, use the cat command:
$ cat output.txt
apples
bananas
kiwis
oranges
pears
You can also use the built-in sort option -o, which allows you to specify an output file:
$ sort -o output.txt data.txt
Using the -o option is functionally the same as redirecting the output to a file; neither one has
an advantage over the other.
Sorting In Reverse Order :
You can perform a reverse-order sort using the -r flag. For example, the following command:
$ sort -r data.txt
pears
oranges
kiwis
bananas
apples
Checking For Sorted Order
If you just want to check to see if your input file is already sorted, use the -c option:
$ sort -c data.txt
If your data is unsorted, you will receive an informational message reporting the line number
of the first unsorted data, and what the unsorted data is.
40) find:- Finds one or more files assuming that you know their approximate path.
Syntax :- find [options] path
Description :-
Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory
hierarchy
path A path name of a starting point in the directory hierarchy
-maxdepth Descend at most levels (a non-negative integer) levels of directories below
the command line arguments.
-i ignore the case in the current directory and sub-directories.
-size Find file based on size
Example:-
$ find -name "sum.java"
./bkp/sum.java
./sum.java
$ uniq example.txt
unix operating system
unix dedicated server
linux dedicated server
The -c option is used to find how many times each line occurs in the file. It prefixes each line
with the count.
$ uniq -c example.txt
2 unix operating system
1 unix dedicated server
1 linux dedicated server
You can print only the lines that occur more than once in a file using the -d option.
$ uniq -d example.txt
unix operating system
The -D option prints all the duplicate lines.
$ uniq -D example.txt
unix operating system
unix operating system
Server
43) history:- history command is used to list out the recently executed commands in the number line
order.
Syntax:- history [options]
Description:-
The history command performs one of several operations related to recently-executed
commands recorded in a history list. Each of these recorded commands is referred to as an
``event''.
ttyname If you wish to talk to a user who is logged in more than once, the ttyname argument
may be used to indicate the appropriate terminal name, where ttyname is of the form
'ttyXX' or 'pts/X'.
45) grep:- It selects and prints the lines from a file which matches a given string or pattern.
Syntax:- grep [options] pattern [file]
Description:-
This command searches the specified input fully for a match with the supplied pattern and
displays it.
While forming the patterns to be searched we can use shell match characters, or regular
expressions.
Let us begin with the simplest example of usage of grep.
directory.
Example:-
$ pwd
/home/abc
47) wc:- Word Count (wc) command counts and displays the number of lines, words, character and
number of bytes enclosed in a file.
Syntax: - wc [options] [filename]
Description:-
This command counts lines, words and characters depending on the options used. It takes
one or more filenames as its arguments and displays four-columnar output. For example
let’s read our os1 file. And we use wc command with that filename.
-l print the newline counts.
-w print the word counts.
-c print the byte counts.
-m print the character counts.
-L print the length of the longest line.
Example :-
$cat myfile.txt
Red Hat
CentOS
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Pearl Linux
Slackware
Mandriva
The ‘wc‘ command without passing any parameter will display a basic result of ”myfile.txt‘
file. The three numbers shown below are 12 (number of lines), 16 (number of words) and
112 (number of bytes) of the file.
$wc myfile.txt
12 16 112 myfile.txt
To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from
a given file. Say, the following command will display the count of newlines in a file. In the
output the first filed assigned as count and second field is the name of file.
$wc -l myfile.txt
12 myfile.txt
Using ‘-w‘ argument with ‘wc‘ command prints the number of words in a file. Type the
48) man:- man command which is short for manual, provides in depth information about the requested
command (or) allows users to search for commands related to a particular keyword.
Syntax:- man commandname [options]
Description :-
-a Print a one-line help message and exit.
-k Searches for keywords in all of the manuals available.
Example:-
$ man mkdir
Display the information about mkdir command
49) | (Pipeline command):- Used to combine more than one command. Takes output of 1st command as
input of 2nd command.
Syntax:- commmand1| command2|……
Example:-
$ls -l | grep "Feb"
-rw-r--r-- 1 dietstaffdietstaff 336 Feb 19 14:41 calc1.sh
-rw-r--r—1 dietstaffdietstaff 410 Feb 19 14:28 calc.sh
drwxr-xr-x 7 dietstaffdietstaff 4096 Feb 24 09:04 Desktop
drwxr-xr-x 7 dietstaffdietstaff 12288Feb 26 07:44 Downloads
drwxr-xr-x 3 dietstaffdietstaff 4096 Feb 26 08:55 lab
drwxr-xr-x 2 dietstaffdietstaff 4096 Feb 24 08:00 shellscript
-rw-r--r—1 dietstaffdietstaff 81 Feb 24 12:34 temp3.sh
preceded by a + symbol, followed by the % operator, and a single character describing the
format.
Format
%a Abbreviated weekday(Tue).
%A Full weekday(Tuesday).
%b Abbreviated month name(Jan).
%B Full month name(January).
%c Country-specific date and time format..
%D Date in the format %m/%d/%y.
%j Julian day of year (001-366).
%p String to indicate a.m. or p.m.
%T Time in the format %H:%M:%S.
%t Tab space.
%V Week number in year (01-52); start week on Monday.
Example:-
$ date
Fri Feb 19 09:55:15 IST 2016
$ date +%m
02
$ date +%h
Feb
$ date +%y
16
$ date +"Date is %D %t Time is %T"
date is 19/02/16 Time is 10:52:34
To know the week number of the year,
$ date -V
11
To set the date,
$ date -s "10/08/2016 11:37:23"
The above command will print Wed Oct 08 11:37:23 IST 2016
2) wall :- send a message to everybody's terminal.
Syntax :- wall [ message ]
Wall sends a message to everybody logged in with their mesg(1) permission set to yes. The
message can be given as an argument to wall, or it can be sent to wall's standard input.
When using the standard input from a terminal, the message should be terminated with the
EOF key (usually Control-D).
The length of the message is limited to 20 lines.
Example : -
$sudo wall message.txt
Using the sudo command to run wall as the superuser, sends the contents ofmessage.txt to
all users.
SHELL SCRIPTS
1) Write a shell script to scans the name of the command and executes it.
Program :-
echo "enter command name"
read cmd
$cmd
Output :-
enter command name
cal
February 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
2) Write a shell script Which works like calculator and performs below operations Addition
, Subtract ,Division ,Multiplication
Program :-
i) using if..elif statement
echo " Enter one no."
read n1
echo "Enter second no."
read n2
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
if [ $ch = "1" ]
then
sum=`expr $n1 + $n2`
echo "Sum ="$sum
elif [ $ch = "2" ]
then
sum=`expr $n1 - $n2`
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
Output :-
Enter one no.
32
Enter second no.
12
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
2
Sub = 20
Output :-
Enter one no.
32
Enter second no.
22
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
2
Sub = 10
Do u want to continue ? y/n
N
3) Write a shell script to print the pyramid structure for the given number.
Program :-
echo "enter the number"
read n
printf "\n"
for((i=1;i<=$n;i++))
do
for((j=1;j<=$i;j++))
do
printf "$j"
done
printf "\n"
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
done
Output :-
enter the number
4
1
12
123
1234
4) Write a shell script to find the largest among the 3 given numbers.
Program :-
clear
echo "Enter first number: "
read a
echo "Enter second number: "
read b
echo "Enter third number: "
read c
if [ $a -ge $b -a $a -ge $c ]
then
echo "$a is largest integer"
elif [ $b -ge $a -a $b -ge $c ]
then
echo "$b is largest integer"
elif [ $c -ge $a -a $c -ge $b ]
then
echo "$c is largest integer"
fi
Output :-
Enter first number:
22
Enter second number:
33
Enter third number:
42
44 is largest integer
clear
fact=1
echo "Enter number to find factorial : "
read n
a=$n
#if enter value less than 0
if [ $n -le 0 ]
then
echo "invalid number"
exit
fi
#factorial logic
while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "Factorial for $a is $fact"
Output :-
Enter number to find factorial :
5
Factorial for 5 is 120
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo $m
fi
m=`expr $m + 1`
done
Output :-
enter the range
10
the prime no are
2
3
5
7
Output :-
bash pr81.sh 123
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
Output :-
How many number of terms to be generated?
5
Fibonacci Series up to 5 terms :
0
1
1
2
3
9) Write a shell script to check whether the given number is Perfect or not.
Program :-
echo Enter a number
read no
i=1
ans=0
while [ $i -le `expr $no / 2` ]
do
if [ `expr $no % $i` -eq 0 ]
then
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
Output :-
Enter a number
6
6 is perfect
Enter a number
10
10 is NOT perfect
10) Write a shell script which displays a list of all files in the current directory to which you
have read, write and execute permissions
Program :-
for File in *
do
if [ -r $File -a -w $File -a -x $File ]
then
echo $File
fi
done
Output :-
Desktop
Documents
Downloads
lab
Music
Pictures
Public
shellscript
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
Templates
Videos
11) Write a shell script that deletes all the files in the current directory which are 0 bytes in
length.
Program :-
clear
find . -name "*" -size -1k –delete
echo “files deleted”
Output :-
files deleted
12) Write a shell script to check whether the given string is Palindrome or not.
Program :-
clear
echo "Enter the string:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
Output :-
Enter the string:
abba
String is palindrome
abc
String is not palindrome
13) Write a shell script to display the digits which are in odd position in a given 5 digit
number
Program :-
echo "Enter a 5 digit number"
read num
n=1
while [ $n -le 5 ]
do
a=`echo $num | cut -c $n`
echo $a
n=`expr $n + 2`
done
Output :-
Enter a 5 digit number
12345
1
3
5
14) Write a shell script tocheck given year is leap year or not.
Program :-
clear
echo "enter any year"
read num
if [ `expr $num % 4` -eq 0 ]
then
if [ `expr $num % 100` -eq 0 -a `expr $num % 400` -ne 0 ]
then
echo "Not a leap year"
else
echo "Leap year "
fi
else
echo "Not a leap year"
fi
Output :-
enter any year
2016
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
Leap year
15) Write a shell script tofind the value of one number raised to the power of another.
Program :-
echo "Input number"
read no
echo "Input power"
read power
counter=0
ans=1
while [ $power -ne $counter ]
do
ans=`echo $ans \* $no | bc`
counter=`echo $counter + 1 | bc`
done
echo "$no power of $power is $ans"
Output :-
Input number
5
Input power
3
5 power of 3 is 125
16) Write a shell script to display the following details in a pay list Pay slip details, House
rent allowance, Dearness allowance, Provident fund. HRA is to be calculated at the rate
of 20% of basic, DA at the rate of 40% of basic and PF at the rate of 10% of basic.
Program :-
i="y"
while [ $i = "y" ]
do
echo "Please enter your Basic:"
read basic
echo "PAY SLIP DETAILS"
echo "1. HOUSE RENT ALLOWANCE"
echo "2. DEARNESS ALLOWANCE"
echo "3. PROVIDENT FUND"
echo "your choice:"
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
read ch
case $ch in
1) hra=`expr $basic \* 20 / 100`
echo Your HOUSE RENT ALLOWANCE is Rs. $hra;;
2) da=`expr $basic \* 40 / 100`
echo Your DEARNESS ALLOWANCE is Rs. $da;;
3) pf=`expr $basic \* 10 / 100`
echo Your PPOVIDENT FUND is Rs. $pf;;
*) echo "Not a valid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done
Output :-
Please enter your Basic:
1000
PAY SLIP DETAILS
1. HOUSE RENT ALLOWANCE
2. DEARNESS ALLOWANCE
3. PROVIDENT FUND
your choice:
1
Your HOUSE RENT ALLOWANCE is Rs. 200
Do u want to continue ?
done
echo "the sum of $a is $sum"
Output :-
enter the number
3355
the sum of 3355 is 16
18) Write a shell script that greets the user by saying Good Morning, Good Afternoon, and
Good Evening according to the system time.
Program :-
clear
#hours=`date|cut -c 12-13`
hours=`date +%H`
if [ $hours -le 12 ]
then
echo "Good Morning"
elif [ $hours -le 16 ]
then
echo "Good Afternoon"
elif [ $hours -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi
Output :-
Good Afternoon
19) Write a shell script to generate mark sheet of a student. Take 3 subjects, calculate and
display total marks, percentage and Class obtained by the student.
Program :-
Clear
echo "Enter the five subject marks for the student"
read s1 s2 s3
sum1=`expr $s1 + $s2 + $s3`
echo "Sum of 5 subjects are: " $sum1
per=`expr $sum1 / 3`
echo " Percentage: " $per
if [ $per -ge 60 ]
[STUDENT NAME] PAGE NUMBER
LOGO
SUBJECT CODE – SUBJECT NAME
then
echo "You get Distinction"
elif [ $per -ge 50 ]
then
echo “You get First classâ€
elif [ $per -ge 40 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
Output :-
Enter the five subject marks for the student
78 88 92
Sum of 5 subjects are: 258
Percentage: 86
You get Distinction
20) Write a shell script that finds total no. of users and finds out how many of them are
currently logged in.
Program :-
cat /etc/passwd>user.txt
set `wc -l user.txt`
log=`who|wc -l`
echo "There are $1 users in network "
echo "There are $log user loged in right now"
Output :-
There are 49 users in network
There are 2 user loged in right now