Mental Health Awareness - a toolkit for supporting young minds
Unix lab manual
1. Systems lab MCCS1.8
Cycle-1
1.unix commands:
a. Text processing and backup utilities:
1. head : It displays the first few lines of one or more files (page) .
Syntax: $ head <filename>
It displays the first 10 lines of the file.
Ex: [root@dcalabunixserver ~]# cat aaa
i am a good girl
hello
hw r u
i am fine
how is it
are there any thing
india is my
county
all are fine
it is better
dont do that
where are u
hello hw r u
is it okk
what is that
thank u
bye
2. [root@dcalabunixserver ~]# head aaa
i am a good girl
hello
hw r u
i am fine
how is it
are there any thing
india is my
county
all are fine
it is better
Syntax: $ head –n <filename>
Ex: [root@dcalabunixserver ~]# head -3 aaa
i am a good girl
hello
hw r u
syntax: $ cat <filename> | head -n - It display first n lines
Ex: [root@dcalabunixserver ~]# cat aaa | head -4
i am a good girl
hello
hw r u
i am fine
2. tail : It displays last few records of one or more file
3. Syntax: tail <filename>
It displays last 10 lines.
Ex: [root@dcalabunixserver ~]# tail aaa
county
all are fine
it is better
dont do that
where are u
hello hw r u
is it okk
what is that
thank u
bye
Syntax: tail –n <filename>
Ex: [root@dcalabunixserver ~]# tail -2 aaa
thank u
bye
syntax: cat <filename> | tail -n
Ex: [root@dcalabunixserver ~]# cat aaa | tail -1
bye
If u want to redirect to permanent
Ex: $ head -20 <filename> |tail -5 > file1 (file1 is newfile)
Ex: $ tail +30 <filename> |head -5 > file1 (file1 is newfile)
3. tr : Translate characters by characters
4. Translate or delete characters
Syntax: $ tr [option] <filename>
Ex: $ [root@dcalabunixserver srilu]# cat f3
welcome to first mtech
2010-11
[root@dcalabunixserver srilu]# tr "wel" "*"<f3
***com* to first mt*ch
2010-11
Duplicate characters avoid with sequential order
Ex: $ tr -s “r” < filename
Delete the characters
Syntax: $ tr -d “r” < filename
[root@dcalabunixserver srilu]# tr -d "e"<f6
wlcom to adithya
collg
of technology
4. Sort: sort lines of text files according to the first character
Syntax: sort [option] <filename>
Options:
-b, --ignore-leading-blanks ignore leading blanks
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < âJANâ < ... < âDECâ
-n, --numeric-sort compare according to string numerical value
-r, --reverse reverse the result of comparisons
5. Ex : sort student
student filename
01 vijay 60 90
12 anil 70 75
32 sujan 65 40
04 hari 70 45
22 raju 85 50
5. Cut: Remove sections from each line of files.
1. Character cutting
Syntax: $ cut -c <filename>
First character of the all lines
[root@dcalabunixserver srilu]# cut -c 5-10 f6
ome to
ege
echnol
Ex: $ cut -c1 <filename>
To cut the first Character Of the all records
Ex: $ cut -c8 <filename>
To cut the 8th character in each line
Ex: $ cut -c4, 8 <filename>
To cut 4th and 8th Character In each line
Ex: $ cut -c4 -8 <filename>
To cut 4th to 8th Characters range
2. Field cutting
6. -d field separator
Syntax: $ cut -d “:” -f2 <filename>
Cut 2nd field in all records
Ex: [root@dcalabunixserver srilu]# cat>bbb
aaa:fff:hhhh
ttt:
ghh:tgyh:yuu
ert:hgjjk:hhh
[3]+ Stopped cat > bbb
[root@dcalabunixserver srilu]# cut -d : -f1 bbb
aaa
ttt
ghh
6. Uniq: Remove duplicate lines from a sorted file
Syntax: $ uniq [option] <filename>
Options:
-c, --count
prefix lines by the number of occurrences
-d, --repeated
only print duplicate lines
-D, --all-repeated[=delimit-method] print all duplicate lines
delimit-method={none(default),prepend,separate} Delimiting is done with blank
lines.
-f, --skip-fields=N
avoid comparing the first N fields
-i, --ignore-case
7. ignore differences in case when comparing
-s, --skip-chars=N
avoid comparing the first N characters
-u, --unique
only print unique lines
-w, --check-chars=N
compare no more than N characters in lines
Displays only the Duplicate records
Ex: $ uniq -d <filename >
[root@dcalabunixserver srilu]# cat>ccc
aaa
bbb
aaa
aaa
[7]+ Stopped cat > ccc
[root@dcalabunixserver srilu]# uniq -d ccc
aaa
Ex: $ uniq -D <filename>
Print all duplicate Records and demeliting is done with blank lines
Ex: $ uniq –c <filename>
Prefix lines by the Number of occurrences
Ex: [root@dcalabunixserver srilu]# uniq -c ccc
1 aaa
1 bbb
2 aaa
Diff: find differences between two files
Syntax: diff [options] from-file to-file
8. Options:
-a Treat all files as text and compare them line-by-line, even if they do not seem to be text.
-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank lines.
[root@dcalabunixserver srilu]# cat>lucky
apple
bannana
orange
pineapple
cat
rat
[9]+ Stopped cat > lucky
[root@dcalabunixserver srilu]# cat>keertu
apple
banana
oringe
pineapple
mat
pen
[10]+ Stopped cat > keertu
[root@dcalabunixserver srilu]# diff lucky keertu
2,3c2,3
< bannana
9. < orange
---
> banana
> oringe
5,6c5,6
< cat
< rat
---
> mat
> pen
Comm.: compare two sorted files line by line
Syntax: comm [OPTION]... FILE1 FILE2
Ex: [root@dcalabunixserver srilu]# comm lucky keertu
apple
banana
bannana
orange
oringe
pineapple
cat
mat
pen
rat
7. tee : It displays and redirects the same time
10. read from standard input and write to standard output and files
Ex: $ ls -l <filename>
$ ls -l | tee <filename>
$ ls --full-time | tee -a <filename> - appending
existing file
Note: without | (pipe symbol) tee filter don’t works
8. FIND COMMANDS
It search for files in a directory hierarchy
Syntax: find [path...] [expression]
/ - root
~ - Home Directory
. - Present working directory
Ex: $ find /bin –type f
It searches the files and directories absolute path of the root.
Ex: $ find ~ -type d
It searches the directories and subdirectories in the Home directory
Ex: $ find ~ -type l
It searches the linked files
Ex: $ find ~ -type f -name “file1”
It searches the absolute path of the filename
11. Ex: $ find ~ -type d -name “raj”
It searches the absolute path of the Directories
Ex: $ find ~ -type f -name “file1” exce cat{};
It searches the absolute path of the filename and displays the contents of file.
Ex: $ find ~ -type f -name “file1” exce cat {} exce rm{};
It searches the absolute path of the filename and displays the content of file, and removes at the
same time.
Ex: $ find ~ -type f -perm 644
It searches the file under with 644 permission
Ex: $ find ~ -type f -perm 644 -exce chmod 640 {};
It searches the filename under with permission 644 and change the file permissions to 640.
Ex: $ find ~ -type f size 0;
It Searches the Zero byte files
Linux: 512 bytes – 1 block
Ex: $ find ~ -type f size 100c (Exactly 100 bytes file)
$ find ~ -type f size +100c (above 100 bytes file)
$ find ~ -type f size -100c (below 100 bytes file)
Ex: $ find ~ -type d size 8b
12. It searches the 8bytes Directories
Ex: $ find ~ -type f -i num “1098”
It searches the file with inode number
Ex: $ find ~ type d – inum “1024”
It searches the directories with inode number
Time
-a – Access
-c – Changed
-m – Modified
Ex: $ find ~ -type f –a min 30
It searches the file before 30 min access
Ex: $ find ~ -type f –a time –n/n/+n
It searches the file before few days
9. Grep
The grep utilities are a family of Unix tools, including grep, egrep, and fgrep, that perform
repetitive searching tasks. The tools in the grep family are very similar, and all are used for
searching the contents of files for information that matches particular criteria. For most purposes,
you'll want to use fgrep, since it's generally the fastest
The general syntax of the grep commands is:
Syntax: grep [-options] pattern [filename]
13. You can use fgrep to find all the lines of a file that contain a particular word. For example, to list
all the lines of a file named my file in the current directory that contain the word "dog", enter at
the Unix prompt:
Ex: fgrep dog myfile
This will also return lines where "dog" is embedded in larger words, such as "dogma" or
"dogged". You can use the -w option with the grep command to return only lines where "dog" is
included as a separate word:
Ex: grep -w dog myfile
To search for several words separated by spaces, enclose the whole search string in quotes, for
example:
Ex: fgrep "dog named Checkers" myfile
The fgrep command is case sensitive; specifying "dog" will not match "Dog" or "DOG". You can
use the -i option with the grep command to match both upper- and lowercase letters:
Ex: grep -i dog myfile
To list the lines of myfile that do not contain "dog", use the -v option:
Ex: fgrep -v dog myfile
If you want to search for lines that contain any of several different words, you can create a
second file (named second file in the following example) that contains those words, and then use
the -f option:
Ex: fgrep -f second file my file
You can also use wildcards to instruct fgrep to search any files that match a particular pattern.
For example, if you wanted to find lines containing "dog" in any of the files in your directory
with names beginning with "my", you could enter:
14. Ex: fgrep dog my*
This command would search files with names such as my file, my.hw1, and my stuff in the
current directory. Each line returned would be prefaced with the name of the file where the
match was found.
By using pipes and/or redirection, you can use the output from any of these commands with
other Unix tools, such as more, sort, and cut. For example, to print the fifth word of every line of
my file containing "dog", sort the words alphabetically, and then filter the output through the
more command for easy reading, you would enter at the Unix prompt:
Ex: fgrep dog myfile | cut -f5 -d" " | sort | more
If you want to save the output in a file in the current directory named new file, enter:
Ex: fgrep dog my file | cut -f5 -d" " | sort > new file
$ grep –n <file> - To display record number
$ grep -i<file> - To ignore record
$ grep –c <file> - To count in how many records expression
$ grep –E <file> - To search for multiple expression
$ grep –L <file> - It give the file name and which the
regular expression
$ grep –r <file> - To search regressively and present working
15. directory
$ grep –w <file> - To search for the exact match for the word
$ grep –s <file> - To suppress errors
$ grep “jai” <file> - It prints the expressive record which
have got the “jai”
$ grep –n “jai” <file> - It display the record number and
expression what we give “jai”
$ grep –in “jay” <file> - It ignore and prints the record
“Jay”
$ grep –E “jay prem” <file> - It search for multiple
expressions and prints the record
$ grep –l “jay”* - It displays the filename which the
“Jay” expression is
AWK
AWK is a simple and elegant pattern scanning and processing language
AWK is also the most portable scripting language
16. It was created in late 70th of the last century. The name was composed from the initial letters of
three original authors Alfred V. Aho, Brian W. Kernighan, and Peter J. Weinberger. It is
commonly used as a command-line filter in pipes to reformat the output of other commands. It's
the precursor and the main inspiration of Perl. Although originated in Unix it is available and
widely used in Windows environment too.
AWK takes two inputs: data file and command file. The command file can be absent and
necessary commands can be passed as augments. As Ronald P. Loui aptly noted awk is very
under appreciated language:
The main advantage of AWK is that unlike Perl and other "scripting monsters" that it is very
slim without feature creep so characteristic of Perl and thus it can be very efficiently used with
pipes. Also it has rather simple, clean syntax and like much heavier TCL can be used with C for
"dual-language" implementations.
awk's favor compared to perl:
- awk is simpler (especially important if deciding which to learn first)
- awk syntax is far more regular (another advantage for the
beginner, even without considering syntax-highlighting editors)
- you may already know awk well enough for the task at hand
- you may have only awk installed
- awk can be smaller, thus much quicker to execute for small programs
- awk variables don't have `$' in front of them :-)
- clear perl code is better than unclear awk code; but NOTHING comes
close to unclear perl code
The basic function of awk is to search files for lines (or other units of text) that contain certain
patterns. When a line matches one of the patterns, awk performs specified actions on that line.
awk keeps processing input lines in this way until it reaches the end of the input files
Syntax : awk [option] ‘selection criteria {action}’ <file>
Options : -F - To specify the field separator
-f - To invoke the source code
{action} - Only the print action
17. predefined variables in awk
all predefined variables are in upper cases
FS - Input field separator
OFS - Ouput field separator
NF - Number of fields
NR - Record numbers or No. of records
$ - Fields in awk
Comparation Operator in awk
> - Grater than
>= - Grater than equal
< - Less than
<= - Less than equal
== - Equal to
!= - Not equal
~ - Matching
!~ - not matching
Logical Operator
&& - AND
|| - OR
18. awk has got 3 sections
1. BIGIN
2. MIDDLE
3. END
Begin is keyword for the begin section the variable can be assign in begin section
All the operator in the middle sections, Middle is not keyword for the middle section
What ever u print every thing in the section, End is the keyword for the end section
$ awk ‘/ajay/{print}’<file>
It prints the all the records
$ awk ‘/ajay|ramu/{print}’<file>
To search for multiple expressions and print
$ awk ‘NR==4{print}’ <file>
To print specific record
$ awk ‘NR==3,NR==7{print}’<file>
To print range of records
$ awk ‘NR>4{print}’ <file>
To print all the records which are >4
$ awk ‘NR>={print}’ <file>
$ awk ‘NR<4{print}’ <file>
To print all the records which are <4
$ awk ‘NR<=4{print}’ <file>
if u want print only specific fields
$ awk –F “:” ‘NR==4 {print $1, $3, $4}’ <file>
simple awk program emulates the cat utility; it copies whatever you type on the keyboard
to its standard output (why this works is explained shortly).
$ awk '{ print }'
Now is the time for all good men
19. -| Now is the time for all good men
to come to the aid of their country.
-| to come to the aid of their country.
Four score and seven years ago, ...
-| Four score and seven years ago, ...
What, me worry?
-| What, me worry?
Ctrl-d
Print the length of the longest input line:
awk '{if(length($0)> max)max = length($0) }
END { print max }' data
Print every line that is longer than 80 characters:
awk 'length($0) > 80' data
The sole rule has a relational expression as its pattern and it has no action—so the default
action, printing the record, is used.
Print the length of the longest line in data:
expand data | awk '{if x < length()) x =length()}
END {print "maximum line length is " x}'
The input is processed by the expand utility to change tabs into spaces, so the widths
compared are actually the right-margin columns.
c Print every line that has at least one field:
awk 'NF > 0' data
This is an easy way to delete blank lines from a file (or rather, to create a new file similar
to the old file but from which the blank lines have been removed).
t Print seven random numbers from 0 to 100, inclusive:
awk 'BEGIN {for(i=1;i<=7;i++)print int(101*rand())}'
a Print the total number of bytes used by files:
ls -l files | awk '{ x += $5 }
END { print "total bytes: " x }'
Print the total number of kilobytes used by files:
ls -l files | awk '{ x += $5 }
END { print "total K-bytes: " x + 1023)/1024 }'
Print a sorted list of the login names of all users:
awk -F: '{ print $1 }' /etc/passwd | sort
Count the lines in a file:
20. awk 'END { print NR }' data
Print the even-numbered lines in the data file:
awk 'NR % 2 == 0' data
If you use the expression `NR % 2 == 1' instead, the program would print the odd-
numbered lines
EXAMPLES
# is the comment character for awk. 'field' means 'column'
# Print first two fields in opposite order:
awk '{ print $2, $1 }' file
# Print lines longer than 72 characters:
awk 'length > 72' file
# Print length of string in 2nd column
awk '{print length($2)}' file
# Add up first column, print sum and average:
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
# Print fields in reverse order:
awk '{for i = NF; i > 0; --i)print $i }' file
# Print the last line
{line = $0}
END {print line}
# Print the total number of lines that
contain the word Pat
/Pat/ {nlines = nlines + 1}
END {print nlines}
21. # Print all lines between start/stop pairs:
awk '/start/, /stop/' file
# Print all lines whose first field is
different from previous one:
awk '$1 != prev { print; prev = $1 }' file
# Print column 3 if column 1 > column 2:
awk '$1 > $2 {print $3}' file
# Print line if column 3 > column 2:
awk '$3 > $2' file
# Count number of lines where col3 >col 1
awk '$3 > $1 {print i + "1"; i++}' file
# Print sequence number and then column 1 of file:
awk '{print NR, $1}' file
# Print every line after erasing the 2nd field
awk '{$2 = ""; print}' file
# Print hi 28 times
yes | head -28 | awk '{ print "hi" }'
# Print hi.0010 to hi.0099 (NOTE IRAF USERS!)
yes | head -90 | awk '{printf("hi00%2.0f n",
NR+9)}'
# Replace every field by its absolute value
{ for (i = 1; i <= NF; i=i+1) if ($i < 0)
$i = -$i print}
# If you have another character that delimits fields, use the -F option
# For example, to print out the phone number for Jones in the following file,
# 000902|Beavis|Theodore|333-242-2222|149092
22. # 000901|Jones|Bill|532-382-0342|234023
# ...
# type
awk -F"|" '$2=="Jones"{print $4}' filename
# Some looping for printouts
BEGIN{
for (i=875;i>833;i--){
printf "lprm -Plw %dn", i
} exit
}
Formatted printouts are of the form printf( "formatn", value1, value2, ... valueN)
e.g. printf("howdy %-8s What it is bro. %.2fn", $1, $2*$3)
%s = string
%-8s = 8 character string left justified
%.2f = number with 2 places after .
%6.2f = field 6 chars with 2 chars after .
n is newline
t is a tab
# Print frequency histogram of column of numbers
$2 <= 0.1 {na=na+1}
($2 > 0.1) && ($2 <= 0.2) {nb = nb+1}
($2 > 0.2) && ($2 <= 0.3) {nc = nc+1}
($2 > 0.3) && ($2 <= 0.4) {nd = nd+1}
($2 > 0.4) && ($2 <= 0.5) {ne = ne+1}
($2 > 0.5) && ($2 <= 0.6) {nf = nf+1}
($2 > 0.6) && ($2 <= 0.7) {ng = ng+1}
($2 > 0.7) && ($2 <= 0.8) {nh = nh+1}
($2 > 0.8) && ($2 <= 0.9) {ni = ni+1}
($2 > 0.9) {nj = nj+1}
END {print na, nb, nc, nd, ne, nf, ng, nh, ni, nj, NR}
# Find maximum and minimum values present in
column 1
NR == 1 {m=$1 ; p=$1}
$1 >= m {m = $1}
$1 <= p {p = $1}
END { print "Max = " m, " Min = " p }
# Example of defining variables, multiple
commands on one line
NR == 1 {prev=$4; preva = $1; prevb = $2; n=0; sum=0}
23. $4 != prev {print preva, prevb, prev, sum/n; n=0; sum=0; prev = $4; preva = $1;
prevb = $2}
$4 == prev {n++; sum=sum+$5/$6}
END {print preva, prevb, prev, sum/n}
# Example of using substrings
# substr($2,9,7) picks out characters 9 thru 15 of column 2
{print "imarith", substr($2,1,7) " - " $3, "out."substr($2,5,3)}
{print "imarith", substr($2,9,7) " - " $3, "out."substr($2,13,3)}
{print "imarith", substr($2,17,7) " - " $3, "out."substr($2,21,3)}
print "imarith", substr($2,25,7) " - " $3, "out."substr($2,29,3)}
1. Renaming within the name:
ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh
(although in some cases it will fail, as in file_old_and_old)
2. Remove only files:
ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
or with awk alone:
ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
Be careful when trying this out in your home directory. We remove files!
3. Remove only directories
ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
or
ls -p | grep /$ | wk '{print "rm -r "$1}'
or with awk alone:
ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
Be careful when trying this out in your home directory. We remove things!
4. Killing processes by name (in this example we kill the process called netscape):
kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'`
Disk utilities:
1.df(disk free): df command reports the number of free disk bloks and inode available on all
mounted file systems or on a given name.
Syntax: df [option] [file system]
Options:
h displays information in readable format.
i displays information about free inodes.
24. Ex: [root@dcalabunixserver ~]# du srilu
4 srilu/d3
8 srilu/dir2/dir1
24 srilu/dir2
44 srilu
[root@dcalabunixserver ~]# du -b srilu
4096 srilu/d3
4137 srilu/dir2/dir1
8325 srilu/dir2
16632 srilu
2.du(disk usage):du command prints disk usage,i.e.,the number of 512 bytes blocks used by
each named directory and its subdirectory(default is current directory).
Syntax: du [option] [directories]
Options:
a print usage of all files
r print “can not open” message if a file or directory is inaccessible.
s print only the grand total for each named directory
Ex: [root@dcalabunixserver ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
75766816 4990352 66865604 7% /
/dev/hda1 101086 12167 83700 13% /boot
tmpfs 513468 0 513468 0% /dev/shm
3.mount(mounting file system): mount connect the specified device (which is actually a file
system) to the directory specifed the member files of the files system mounted becomes the
members of the directory on which they are mounted .
Syntax: mount [derivename directory] [option]
Options:
-r the file system will be mounted as “read Only” the default option is “r/w”
4. umount(dismounting file system): “umount” disconnect should be used only after making
sure that the particular file system is not busy that the particular file system is not busy. Unlike
the case of “mount”, you do not have to specify the mount point. Use “sync” before using
“umount”.
Syntax: umount [device 4name]
25. b. Process utilities:
Process: A process is a program that is being executed. In unix multiple processes can run
concurrently.
Users: Typically there are number of users on a unix system. Each user is identified by a unique
user name and user-id. Unix uses the user name in several different ways:
1. To report usage of system resources.
2. To display the list of users on the system.
3. To implement system and file security.
Usergroups: A user may be part of one or more usergroups.
PID: In multiuser environment, there are number of processes that are being executed. To keep
track of process that is being executed, unix assigns a unique process identification number(PID)
to each active process.
1.ps (process status): The ps command displays information about the individual processe that
are executing on the system.
Syntax: $ps [options]
Options:
-a reports information about all process
-l report information in long format
Ex: [root@dcalabunixserver srilu]# ps
PID TTY TIME CMD
3138 pts/1 00:00:00 bash
3179 pts/1 00:00:00 ps
2. kill: Terminating a process
Kill is used to terminate the execution of a background process. The process-id of the
process to be terminated must be specified with kill command. If process id ‘0 (zero)’ is
specified, all processes in the process are signed.
Syntax: $kill <process id>
Options:
n where n is larger than 0. The process with pid n will be signaled.
0 All processes in the current process group are signaled.
-1 All processes with pid larger than 1 will be signaled.
Ex: [root@dcalabunixserver srilu]# kill -9 0
26. 3.at: at command allows the user to specify the time when a command is to be executed. The
command takes two arguments, the time at which the command is to be executed, and the
command to be executed.
Syntax: at time[date] [increment]
Ex: [root@dcalabunixserver srilu]# [root@dcalabunixserver srilu]# at 12.30pm jan 13
at> cat f3
at> <EOT>
job 3 at 2011-01-13 12:30
4.nice: Unix also allows the user to specify the priority of commands. The nice command is used
to run a command at a specified scheduling priority.
Syntax: nice [-increment] command [argument]
Ex: [root@dcalabunixserver ~]# nice
nice
0
5. who: who command shows who is logged on,as well as information about the system.
Syntax: $who
Ex: [root@dcalabunixserver srilu]# who
root :0 2011-01-13 09:26
root pts/1 2011-01-13 09:49 (172.16.6.250)
6.who am i: Displays current user.
Syntax:$who am i
Ex: [root@dcalabunixserver srilu]# who am i
root pts/1 2011-01-13 09:49 (172.16.6.250)
7.w: w command printf summaries of system usage, currently logged-in users, and what they are
doing.
Syntax: w [option] [user]
Options:
-u Ignores the username while figuring out the current process and cpu times. To
demonstrate this, do a "su" and do a "w" and a "w -u".
-s Use the short format. Donât print the login time, JCPU or PCPU times.
-l Display information in long format.
27. -V Display version information.
Ex: [root@dcalabunixserver srilu]# w
10:22:43 up 1:07, 2 users, load average: 0.10, 0.02, 0.01
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root :0 - 09:26 ?xdm? 13.03s 0.11s /usr/bin/gnome-session
root pts/1 172.16.6.250 09:49 0.00s 0.07s 0.00s w
8. File level security:
Unix uses an elaborate method of file access permissions to maintain file security.
File access permissions: Each file and directory has an
1. An owner: The user who created the file.
2. A group: A group of users have access to the file.
3. Others: Other users of the system.
The permissions can be granted or denied to these three classes of users.
Three types of file access permissions are there, they are
read: A file can be read, displayed on terminal, copied and compiled.
write: A file can be read modified and deleted.
executed: A file can be executed as a program.
They are represented as
-rwx------
drwxr--r--
-rwx-w--w-
- - File
d - Directory
w - Wrire
r - Read
x - Executable
Note: Without write permissions we can’t copy, remove, modify, move, crate
drwxrwxrwx
777
Directory – 777
File – 666
Default Directory Permissions – 755
Default File Permissions – 644
Read –4
Write –2
Executable –1
Total Permissions 7
28. chmod ( change mode): The chmod command changes file access permissions for a file.
Syntax: chmod mode files
The mode is the permission to be assigned. Mode can be specified in two ways
1. Numeric Method
2. Symbolic Method
1. Numeric Method:
Syntax: $ chmod [permissions] <file/Directory>
Ex: [root@dcalabunixserver srilu]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jan 12 09:36 d3
-rw-r--r-- 2 root root 41 Jan 12 07:40 f8
[root@dcalabunixserver srilu]# chmod 624 f8
[root@dcalabunixserver srilu]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jan 12 09:36 d3
-rw--w-r-- 2 root root 41 Jan 12 07:40 f8
2. Symbolic Method:
User – u
Group – g
Others – o
All –a
Read – r
Write – w
Execute – x
Syntax: $ chmod [options] <File/Directory>
Ex: [root@dcalabunixserver srilu]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jan 12 09:36 d3
-rw-r--r-- 1 root root 31 Jan 10 13:22 f3
[root@dcalabunixserver srilu]# chmod o=rwx,g=r,u=x f3
[root@dcalabunixserver srilu]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jan 12 09:36 d3
---xr--rwx 1 root root 31 Jan 10 13:22 f3
To Add Write permission to Group and Others
Ex: [root@dcalabunixserver srilu]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Jan 12 09:36 d3
[root@dcalabunixserver srilu]# chmod g+w,o+w d3
[root@dcalabunixserver srilu]# ls -l
total 24
drwxrwxrwx 2 root root 4096 Jan 12 09:36 d3
To Remove the Write Permission to User
29. Ex: [root@dcalabunixserver srilu]# ls –l
-rw---x--- 1 root root 41 Jan 12 07:44 f6
[root@dcalabunixserver srilu]# chmod u-w f6
[root@dcalabunixserver srilu]# ls –l
-r----x--- 1 root root 41 Jan 12 07:44 f6
To Remove the Write and execute permissions to Group and Others
Ex: [root@dcalabunixserver srilu]# ls -l
total 24
drwxrwxrwx 2 root root 4096 Jan 12 09:36 d3
[root@dcalabunixserver srilu]# chmod g-wx,o-wx d3
[root@dcalabunixserver srilu]# ls -l
total 24
drwxr--r-- 2 root root 4096 Jan 12 09:36 d3
Append the Write permissions to all
Ex: $chmod u+w, g+w, o+w Dir1
(or)
$chmod a+w Dir1
UMASK (User File Creation Mask)
- cuting, remove, hidden
By Default umask value is 022
Directory File
777 666
022 022
755 644
Permissions
Umask File/Directory
number
0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx
Ex: [root@dcalabunixserver srilu]# ls -l
total 24
30. d--------- 2 root root 4096 Jan 12 09:36 d3
[root@dcalabunixserver srilu]# chmod 111 d3
[root@dcalabunixserver srilu]# ls -l
total 24
d--x--x--x 2 root root 4096 Jan 12 09:36 d3
9. chown (change owner) & chgrp (change group): chown changes file ownership and
reassign the ownership of the file from one user to another.
Syntax: chown [option] newowner filename
Options:
-c, --changes-like verbose but report only when a change is made
-h, --no-dereference-affect each symbolic link instead of any referenced file
Ex: [root@dcalabunixserver srilu]# ls -l
---xr--rwx 1 root root 31 Jan 10 13:22 f3
[root@dcalabunixserver srilu]# chown mic f3
[root@dcalabunixserver srilu]# ls -l
---xr--rwx 1 mic root 31 Jan 10 13:22 f3
Chgrp also changes ownership. It changes the group ownership of the file.
Syntax: chgrp [option] groupname filename
Options:
-c, --changes- like verbose but report only when a change is made
-h, --no-dereference-affect each symbolic link instead of any referenced file --preserve-root
fail to operate recursively on â/â
-f, --silent, --quiet suppress most error message
-R, --recursive operate on files and directories recursively
Ex: [root@dcalabunixserver srilu]# ls -l
---xr--rwx 1 mic root 31 Jan 10 13:22 f3
[root@dcalabunixserver srilu]# chgrp mic f3
[root@dcalabunixserver srilu]# ls -l
---xr--rwx 1 mic mic 31 Jan 10 13:22 f3
10.newgrp : changes group.
Syntax: newgrp group
[root@dcalabunixserver srilu]# newgrp mic
c.Networking commands:
1. ftp:This command is used to connect to any other computer on your network running ftp.
Start it with the following
[root@dcalabunixserver ~]# ftp
ftp
ftp> ?
31. Here in the ftp prompt we have to specify special ftp commands.Type a ? or help to
prompt commands.
The following table lists the most frequently used commands
Command Result
ascii Uses, ascii as a file transfer type
bell Rings the bell when file transfer is
completed
Binary Uses binary as file transfer type
quit or bye Terminates ftp session
close Ends ftp connection with remote machine,
but keeps local ftp program running
cd Changes directory on remote machine
get filename Gets the filename from remote machine
pwd Lists the current working directory on
remote machine
ftp> open 172.16.6.100
Connected to 172.16.6.100.
220 dcalabunixserver FTP server (Version 5.60) ready.
Name (172.16.6.100:mic):sri
530 please specify password
Password:
530 login successful.
Remote system type is UNIX.
ftp>pwd
313 “home/mic”
ftp>ls
-rw-r--r-- 1 mic mic 109 Aug 18 18:46 A
-rw-r--r—1 mic mic 113 Aug 18 18:49 a.c
-rw-r--r—1 mic mic 76 Oct 21 13:09 add1.sh
drwxr-xr-x 2 mic mic 4096 Jan 5 13:28 Desktop
ftp> quit
221 Goodbye.
2. rlogin (Remote login):The rlogin command allows you to remotely login another
computer on your network.
32. [mic@dcalabunixserver ~]$ rlogin root
123456
3. telnet: It is a command used to communicate with another host.
[mic@dcalabunixserver ~]$ pwd
/home/mic
[mic@dcalabunixserver ~]$ telnet 172.16.6.100
Trying 172.16.6.100...
Connected to 172.16.6.100 (172.16.6.100).
Escape character is '^]'.
Red Hat Enterprise Linux Server release 5.5 (Tikanga)
Kernel 2.6.18-194.el5 on an i686
login: mca0933
Password:
Last login: Tue Oct 19 09:40:20 from 172.16.6.31
[mca0933@dcalabunixserver ~]$ pwd
/home/mca0933
[mca0933@dcalabunixserver ~]$ exit
Connection closed by foreign host.
[mic@dcalabunixserver ~]$ pwd
/home/mic
4. Finger: This command displays information about system users.
Options:
-s display the output in short form
-l display th output in long form
-m couese finger to search only in login names that match with argument name.
Ex: [mic@dcalabunixserver ~]$ finger mic
Login: mic Name: (null)
Directory: /home/mic Shell: /bin/bash
On since Wed Jan 12 10:51 (IST) on pts/1 from 172.16.6.250
No mail.
No Plan.
[mic@dcalabunixserver ~]$ finger -s mic
Login Name Tty Idle Login Time Office Office Phone
mic pts/1 Jan 12 10:51 (172.16.6.250)
5. Wall: For broad casting messages.
[root@dcalabunixserver ~]# wall hai
Broadcast message from root (pts/2) (Wed Jan 12 13:20:15 2011):
hai
33. d.File handling utilities:
1. pwd (print working directory): Displays the current working directory.
Syntax: $ pwd
Ex: [root@dcalabunixserver ~] # pwd
/root
2. cat command:displaying and creating files
To Create a File:
Syntax: $ cat < [option] > <filename>
The option can be any one of the below
S.no Option Function
1. -v It is used to display non
printable characters.
2. -n It is used to numbering lines in
output.
3. -b It used to number nonblank
output lines.
4. -e It display $ at end of each line.
5. -A Shows all
6. -s never more than one single
blank line
Ex: [root@dcalabunixserver ~]#cat > file1
Hello..
This is my first File
Have a Nice Day
Bye
Ctrl+d (Save)
To View a already existing File:
34. Syntax: $ cat <filename>
Ex: [root@dcalabunixserver ~]#cat file1
Hello..
This is my first File
Have a Nice Day
Bye
Cat command can also be used to accept more than one file as an argument.
Syntax: $cat file1 file2
Ex: [root@dcalabunixserver ~]#cat f1 f2
This is simple file
This is a simple file2
The contents of second file are shown immediately after the first file.
To append data to an existing file
Syntax: $ cat >> <filename>
Ex: [root@dcalabunixserver ~]#cat >> file1
Hello..
This is my first File
Have a Nice Day
Bye
Good mornig
Welcome to new world
To Create a Multiple file with help of cat command
Syntax: $ cat <filename1 filename2 ...filename (n) >
Ex[root@dcalabunixserver ~]#cat >f1 >f2 >f3 >f4
Hello....
This is file number f4
We create multiple files
35. ^d (Save)
Display the record number to the particular file
Syntax: $ cat –n <filename>
Ex: [root@dcalabunixserver ~]# cat -n f1
1 welcome to adithya
2 college
3 of technology
To ignore the blank Records
Syntax: $ cat –b <filename>
Ex: [root@dcalabunixserver ~]# cat -n f4
1 hgjfdh
2
3 sdjfhvidf
4
5 sdklfjgvij
[root@dcalabunixserver ~]# cat -b f4
1 hgjfdh
2 sdjfhvidf
3 sdklfjgvij
Create a Hidden file
Syntax: $ cat [option] <filename>
Ex: $ cat >.file1
[root@dcalabunixserver ~]# cat >.f5
hai
hello
Display the record with single blank line.
[root@dcalabunixserver ~]# cat f6
gdfahfj
36. gfhdghj
hgdfuhdi
jhdjfjik
[root@dcalabunixserver ~]# cat -s f6
gdfahfj
gfhdghj
hgdfuhdi
jhdjfjik
[root@dcalabunixserver ~]# cat f5
cat: f5: No such file or directory
file names with common strings can be displayed using cat as:
syntax:$cat file?
Ex: [root@dcalabunixserver ~]# cat f?
welcome to adithya
college
of technology
welcome to first mtech
2010-11
welcome to first mtech
37. 2010-11
hgjfdh
sdjfhvidf
sdklfjgvij
gdfahfj
gfhdghj
hgdfuhdi
jhdjfjik
3.CC command: used to compile one or more C source files.
Syntax: $cc first.c
4. ls(list) :
It is a command to list the files and directories in the present working Directory
$ ls - a : It is a command to display all files and Directories including hidden files and
Directories.
$ls * : List information about the Files (the current directory by default). Sort entries
alphabetically
$ls ~ : It list the all Backup files
$ls @ : It list the all linked files and Directories
38. $ls -d : It Displays the present working Directory.
$ls -i : It Displays the inode numbers of files and Directories
$ls -s : It Displays the sizes in blocks (Files & Directories)
$ls -l : It Displays the long listing files and directories in present working directory
Listing directory contents:
$ ls list a directory
$ ls -l list a directory in long (detailed) format
For example:
$ ls -l
drwxr-xr-x 4 vijay user 1024 Jun 18 09:40 WAITRON_EARNINGS
-rw-r--r-- 1 kiran user 767392 Jun 6 14:28 scanlib.tar.gz
^^^^ ^ ^ ^ ^ ^ ^ ^
|| | | | | | | | | |
|| | | | owner group size date time name
|| | | number of links to file or directory contents
| | | permissions for world(others)
| | permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, and others...
ls -ld * List all the file and directory names in the current directory using long format. Without
the "d" option, ls would list the contents of any sub-directory of the current. With the "d" option,
ls just lists them like regular files.
$ ls -al : It Displays including hidden and log listing files and Directories
$ ls -m : It Displays all files and Directories with separated by comma (,)
$ ls -ls : It Displays all long listing Directories
$ ls --full-time : It Displays files and Directories with total information date and time
$ ls -nl : It Displays the long listing files andDirectories according to modification Time
$ ls -rtl : It Displays the file and Directories with reverse order
$ ls -R : It Displays the all files and Directories Regressively (order by order)
$ ls -l : It Displays the files and Directories in a single column (vertical)
39. $ ls -x : It Displays the files and Directories with multiple columns
For Ex:
[root@dcalabunixserver ~]# ls
2 echocli lockings.c ser
A echocli.c mani sig.c
a.c echoser menud.sh sri
add1.sh echoser.c pollcli.c sser.c
add.sh exam1.c pollser.c strclic.c
anaconda-ks.cfg exam.c pser.c strclii.c
[root@dcalabunixserver ~]# ls -l
total 16148
-rw-r--r-- 1 root root 2 Oct 21 13:46 2
-rw-r--r-- 1 root root 109 Aug 18 18:46 A
-rw-r--r—1 root root 113 Aug 18 18:49 a.c
-rw-r--r—1 root root 76 Oct 21 13:09 add1.sh
drwxr-xr-x 2 root root 4096 Jan 5 13:28 Desktop
-rw-r--r-- 1 root root 121 Oct 21 13:12 e2.c
-rw-r--r-- 1 root root 118 Oct 21 13:17 e3.c
4. MOVE COMMANDS:
This command is used to move the files and directories one place to another place
Syntax: $ mv [option] <Source file/Directory> <Target file/Directory>
Options:
S.no Option Function
40. 1. -b The file1 move to file2 with
backup.
2. -f The file1 move to file2 with
forcibly.
3. -if The file1 move to file2 with
interactive and forcibly mode.
These are basically 3 types
1. File to File
2. File to Directory
3. Directory to Directory
1. File to File
Syntax: $ mv [option] <Source file> <Target file>
[root@dcalabunixserver ~]# mv old new
[root@dcalabunixserver ~]# cat old
cat: old: No such file or directory
[root@dcalabunixserver ~]# cat new
hello
hw r u
this is a book
Ex: $mv -b file1 file2
Note: The file1 move to file2 with backup
Ex: $ mv -f file1 file2
Note: The file1 move to file2 with forcibly
Ex: $ mv -if file1 file2
Note: The file1 move to file2 with interactive and forcibly mode
2. File to Directory
Syntax: $ mv [option] <Source file> <Target Directory>
Ex: [root@dcalabunixserver ~]# cd srilu
41. [root@dcalabunixserver srilu]# ls
dir1 dir2 f1 f2 f3
[root@dcalabunixserver srilu]# cd
[root@dcalabunixserver ~]# mv f4 ./srilu/f4
[root@dcalabunixserver ~]# cd srilu
[root@dcalabunixserver srilu]# ls
dir1 dir2 f1 f2 f3 f4
Note: The file1 moves to Directory (Dir1)
Ex: $ mv -b file1 Dir1
Note: The file1 move to Dir1 with backup mode
Ex: $mv -f file1 Dir1
Note: The file1 move to Dir1 with forcibly mode
Ex: $mv -if file1 Dir1
Note: The file1 move to Dir1 with interactive and forcibly mode.
3. Directory to Directory
Syntax: $ mv [option] <Source Directory> <Target Directory>
Ex: $ [root@dcalabunixserver srilu]# ls
dir1 dir2 f1 f2 f3
[root@dcalabunixserver srilu]# mv dir1 dir2
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f4
[root@dcalabunixserver dir2]# ls
dir1 f2 f3
Ex: [root@dcalabunixserver srilu]# cp ./dir2/dir1/* ../
cp: overwrite `../f1'? y
[root@dcalabunixserver srilu]# ls -a
. .. dir2 f1 f2 f3 f4
Note: The content of Dir1 moves to Dir2
Ex: $ mv -b Dir1 Dir2
Note: The contents of Dir1 move to Dir2 with backup
Ex: $ mv -f Dir1 Dir2
Note: The contents of Dir1 moves to Dir2 with forcibly
Ex: $ mv -if Dir1 Dir2
Note: The contents of Dir1 moves to Dir2 with interactive and forcibly mode.
5. COPY COMMANDS: Like mv command, cp is used to create new files or move the contents
of file to another location . Unlike move, however, cp leaves the original file intact at its
42. location.
Syntax: $cp [option] <source file> <Destination file>
Options ,
-a, --archive
same as -dR --preserve=all
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
-d same as --no-dereference --preserve=link
-f, --force if an existing destination file cannot be opened, remove it and try again
-i, --interactive prompt before overwrite
-H follow command-line symbolic links
-l, --link - link files instead of copying
-L, --dereference - always follow symbolic links
-P, --no-dereference
These are basically 3 types
1. File to a File
2. File to a Dictionary
3. Directory to Directory
File to File
Syntax : $cp <source file> <Destination file>
Ex : [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f4
[root@dcalabunixserver srilu]# cp f4 f5
43. [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f4 f5
....new file
Ex :[root@dcalabunixserver srilu]# cat f1
welcome to adithya
college
of technology
[root@dcalabunixserver srilu]# cat f2
welcome to first mtech
2010-11
[root@dcalabunixserver srilu]# cp f1 f2
cp: overwrite `f2'? y
[root@dcalabunixserver srilu]# cat f2
welcome to adithya
college
of technology
... Existing file
Note: The content of john file overwrite with venkat
Ex ; $cp -f venkat kumar
Note : It is copy the file forcibly venkat to kumar without any permission
Ex : $cp -i venkat gandhi
Note : It is copy the file interactive mode venkat to kumar with permissions
2. File to Directory
Syntax ; $ cp <source file> <Destination Directory>
Ex ; [root@dcalabunixserver dir2]# ls
dir1 f2 f3
44. [root@dcalabunixserver dir2]# cd ..
[root@dcalabunixserver srilu]# cp f5 dir2
[root@dcalabunixserver srilu]# cd dir2
[root@dcalabunixserver dir2]# ls
dir1 f2 f3 f5
Ex : [root@dcalabunixserver ~]# cp -d f1 srilu
[root@dcalabunixserver ~]# cd srilu
[root@dcalabunixserver srilu]# ls
f1
Note : It copys the f1file to srilu Directory with backup
Ex : [root@dcalabunixserver ~]# cp -bf f1 srilu
[root@dcalabunixserver ~]# cd srilu
[root@dcalabunixserver srilu]# ls
f1
Note ; It copys the f1 file to srilu Directory with forcibly backup
Ex : $ [root@dcalabunixserver ~]# cp -ibf f1 srilu
[root@dcalabunixserver ~]# cd srilu
[root@dcalabunixserver srilu]# ls
f1
Note : It copys the f1 file to srilu Directory with backup interactively and forcibly
Ex : [root@dcalabunixserver srilu]# ls
f1
[root@dcalabunixserver srilu]# cd
[root@dcalabunixserver ~]# cp f1 f2 f3 srilu
[root@dcalabunixserver ~]# cd srilu
[root@dcalabunixserver srilu]# ls
f1 f2 f3
Note : It copies the no of file to Directory
3. Directory to Directory
Syntax : $ cp <Source Directory> <Destination Directory>
Ex : $ cp Dravid Ganguly
Ex : $ cp -r Mody Venkat
Note : It copies the Directory to Directory (including all files and Directories)
45. Ex : $ cp -ir <Directory 1> <Directory 2>
Note : It copies the Directory to Directory (including with all files and directories with
interactive mode)
Ex : $ cp -rf <Dir 1 > <Dir 2> <Dir 3> <Destination
Directory>
Note : It copies the multiple Directories with forcibly and recessively mode
6. rm(removing a file):
The rm command is used to delete a file.
To Remove a file
Syntax; $ rm [option] <filename>
Options:
Remove (unlink) the FILE(s).
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
-r, -R, --recursive
remove directories and their contents recursively
-v, --verbose
explain what is being done
By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove
each listed directory, too, along with all of its contents.
Ex: [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f4 f5
[root@dcalabunixserver srilu]# rm f5
rm: remove regular file `f5'? y
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f4
To Remove Multiple Files
Syntax: $ rm [option] <filenames >
46. Ex: $ rm f1 f2 f3 f4
To remove files Forcibly
Syntax: $ rm [option] <filename>
Ex: [root@dcalabunixserver srilu]# rm -f f4
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3
To Remove Interactive Mode
Syntax: $ rm [option] <filename>
Ex: [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3
[root@dcalabunixserver srilu]# rm -i f3
rm: remove regular file `f3'? n
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3
To Remove Directory
Syntax: $ rm [option] <Directory Name>
To Remove all Directories and subdirectories
Syntax: $ rm -r <Directory Name>
Ex: $ [root@dcalabunixserver srilu]# rm -r dir3
rm: descend into directory `dir3'? y
rm: remove directory `dir3/aaa'? y
rm: descend into directory `dir3/d2'? y
rm: descend into directory `dir3/d2/d3'? y
rm: remove directory `dir3/d2/d3/d4'? y
rm: remove directory `dir3/d2/d3'? y
rm: remove directory `dir3/d2'? y
rm: remove directory `dir3'? y
[root@dcalabunixserver srilu]# ls
dir2 dir4 f3 f5 f6 f7 f8
To Remove Directories Forcibly
Syntax; $ rm -rf <Directory Name>
Ex: $ [root@dcalabunixserver srilu]# rm -rf dir4
47. [root@dcalabunixserver srilu]# ls
dir2 f3 f5 f6 f7 f8
7. touch: The touch command is used to update the timestamp on the named file. The time stamp
the last time the file was altered or accessed.
Syntax: touch [option] filename
Options:
-a change only the access time
-c, --no-create - do not create any files
-f (ignored)
-m change only the modification time
-r, --reference=FILE- use this fileâs times instead of current time
Ex: [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3
[root@dcalabunixserver srilu]# touch f5
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5
Note: If the file name issued as an argument does not eists, touch creates it as an empty file.
Ex: [root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5
[root@dcalabunixserver srilu]# touch -c f6
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5
8. wc : wc command to determine the length of a given file. By default, the output shows the
length in lines, words, characters.
48. Syntax: $ wc [option] <filename>
Options:
-c, --bytes
print the byte counts
-m, --chars
print the character counts
-l, --lines
print the newline counts
-L, --max-line-length
print the length of the longest line
-w, --words
print the word counts
Ex: [root@dcalabunixserver srilu]# wc f1
3 6 41 f1
no of new lines:
Ex: [root@dcalabunixserver srilu]# cat f1
welcome to adithya
college
of technology
[root@dcalabunixserver srilu]# wc -l f1
3 f1
8. ln(creating links): ln command is used to create links for files and allowing them to be
accessed by different names.
Call the link function to create a link to a file.
Call the link function to create a link named FILE2 to an existing FILE1
1. Hard link
2. Soft Link
Syntax: ln [option] file1 file2
ln [option] file directory
49. Options:
-b like --backup but does not accept an argument
-f, --force-remove existing destination files
-n, --no-dereference-treat destination that is a symlink to a directory as if it were a normal
file
-i, --interactive-prompt whether to remove destinations
-s, --symbolic-make symbolic links instead of hard links
1. Hard link: Hard link can be build single file system
Hard link can recognized it links same permissions and same size, same i-node number
If the data in source is loss we can get it from hard link
i-node number is given space allocated
i-node numbers can recognized different logical names
Syntax : ln <source file> <target file>
Ex: [root@dcalabunixserver srilu]# ls -l
total 16
drwxr-xr-x 3 root root 4096 Jan 11 12:45 dir2
-rw-r--r-- 1 root root 41 Jan 12 07:40 f2
-rw-r--r-- 1 root root 31 Jan 10 13:22 f3
-rw-r--r-- 1 root root 0 Jan 12 07:40 f5
-rw-r--r-- 1 root root 41 Jan 12 07:44 f6
[root@dcalabunixserver srilu]# ln f2 f1
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5 f6
[root@dcalabunixserver srilu]# ln f2 f7
[root@dcalabunixserver srilu]# ls -l
50. total 24
drwxr-xr-x 3 root root 4096 Jan 11 12:45 dir2
-rw-r--r-- 3 root root 41 Jan 12 07:40 f1
-rw-r--r-- 3 root root 41 Jan 12 07:40 f2
-rw-r--r-- 1 root root 31 Jan 10 13:22 f3
-rw-r--r-- 1 root root 0 Jan 12 07:40 f5
-rw-r--r-- 1 root root 41 Jan 12 07:44 f6
-rw-r--r-- 3 root root 41 Jan 12 07:40 f7
[root@dcalabunixserver srilu]# ln f2 f8
[root@dcalabunixserver srilu]# ls -l
total 28
drwxr-xr-x 3 root root 4096 Jan 11 12:45 dir2
-rw-r--r-- 4 root root 41 Jan 12 07:40 f1
-rw-r--r-- 4 root root 41 Jan 12 07:40 f2
-rw-r--r-- 1 root root 31 Jan 10 13:22 f3
-rw-r--r-- 1 root root 0 Jan 12 07:40 f5
-rw-r--r-- 1 root root 41 Jan 12 07:44 f6
-rw-r--r-- 4 root root 41 Jan 12 07:40 f7
-rw-r--r-- 4 root root 41 Jan 12 07:40 f8
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5 f6 f7 f8
[root@dcalabunixserver srilu]# rm f2
rm: remove regular file `f2'? y
[root@dcalabunixserver srilu]# ls
dir2 f1 f3 f5 f6 f7 f8
51. 2.Soft link:
It can be built across the file system
If the source file is delete we can't retrieve from target file
Syntax : ln -s <source file> <target file>
Ex: [root@dcalabunixserver srilu]# ln -s f2 f9
[root@dcalabunixserver srilu]# ls -l
total 28
drwxr-xr-x 3 root root 4096 Jan 11 12:45 dir2
-rw-r--r-- 4 root root 41 Jan 12 07:40 f1
-rw-r--r-- 4 root root 41 Jan 12 07:40 f2
-rw-r--r-- 1 root root 31 Jan 10 13:22 f3
-rw-r--r-- 1 root root 0 Jan 12 07:40 f5
-rw-r--r-- 1 root root 41 Jan 12 07:44 f6
-rw-r--r-- 4 root root 41 Jan 12 07:40 f7
-rw-r--r-- 4 root root 41 Jan 12 07:40 f8
lrwxrwxrwx 1 root root 2 Jan 12 08:12 f9 -> f2
[root@dcalabunixserver srilu]# ls
dir2 f1 f2 f3 f5 f6 f7 f8
[root@dcalabunixserver srilu]# rm f2
rm: remove regular file `f2'? y
[root@dcalabunixserver srilu]# ls
dir2 f1 f3 f5 f6 f7 f8 f9
9. unlink: use unlink to delete the name and possibly the file it refer’s to.
52. Syntax:$ unlink <filename>
Ex: [root@dcalabunixserver srilu]# ls
dir2 f1 f3 f5 f6 f7 f8
[root@dcalabunixserver srilu]# unlink f1
[root@dcalabunixserver srilu]# ls
dir2 f3 f5 f6 f7 f8
10. mkdir & rmdir: (Making and Removing directories)
mkdir creates the new directory in which we can store files and other directories.
Syntax: $ mkdir [option] <Directory name>
Options:
-m, --mode=MODE
set permission mode (as in chmod), not rwxrwxrwx - umask
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
print a message for each created directory
-Z, --context=CONTEXT (SELinux) set security context to CONTEXT
Ex: [root@dcalabunixserver srilu]# mkdir dir3
[root@dcalabunixserver srilu]# ls
dir2 dir3 f3 f5 f6 f7 f8
Create Multiple Directories
Syntax: $ mkdir [option] <Directory names>
Ex: [root@dcalabunixserver srilu]# ls
dir2 dir3 f3 f5 f6 f7 f8
[root@dcalabunixserver srilu]# mkdir dir4 dir5
[root@dcalabunixserver srilu]# ls
dir2 dir3 dir4 dir5 f3 f5 f6 f7 f8
Create Multi – level Directories
Syntax: $ mkdir -p <Directory names>
53. Ex: [root@dcalabunixserver dir3]# mkdir -p d2/d3/d4
[root@dcalabunixserver dir3]# ls
d2
[root@dcalabunixserver dir3]# cd d2
[root@dcalabunixserver d2]# ls
d3
[root@dcalabunixserver d2]# cd d3
[root@dcalabunixserver d3]# ls
d4
Create Multiple Sub directories
Syntax: $ mkdir -p <Directory na
mes>
Ex: [root@dcalabunixserver srilu]# mkdir -p dir3/aaa dir4/bbb
[root@dcalabunixserver srilu]# cd dir3
[root@dcalabunixserver dir3]# ls
aaa d2
[root@dcalabunixserver dir3]# cd ..
[root@dcalabunixserver srilu]# cd dir4
[root@dcalabunixserver dir4]# ls
bbb
rmdir: used to remove directories if they are empty.
Syntax:rmdir [option] <directory name>
Ex: [root@dcalabunixserver srilu]# ls
dir2 dir3 dir4 dir5 f3 f5 f6 f7 f8
[root@dcalabunixserver srilu]# rmdir dir5
[root@dcalabunixserver srilu]# ls
dir2 dir3 dir4 f3 f5 f6 f7 f8
11. cd(change directory): The cd command enables you to move around with in the file system.
Used without arguments it returns to your home directory. To move to another directory
directory name is required as an argument.
Syntax: cd <directory name>
To Change the Directory
Syntax: $ cd <Directory name>
Ex: [root@dcalabunixserver dir3]# cd aaa
54. [root@dcalabunixserver aaa]#
To change the directory forward
Syntax: $ cd ..
Ex: [root@dcalabunixserver aaa]# cd ..
[root@dcalabunixserver dir3]#
To Move parent Directory (root)
Syntax: $ cd
Ex: [root@dcalabunixserver dir3]# cd
[root@dcalabunixserver ~]#
55. 2. Shell programming
i.AIM: To print the factorial of first n natural numbers.
SOURCE CODE:
[root@dcalabunixserver srilu]$cat factorial.sh
echo "Enter n value"
read n
let i=1
let f=1
while [ $i -le $n ]
do
let f=f*i
let i=i+1
done
echo " factorial of $n is $f"
[root@dcalabunixserver srilu]$ sh factorial.sh
Enter n value
5
factorial of 5 is 120
56. ii.AIM: To list the files in the current directory to which the user has read, write and
execute permissions.
SOURCE CODE:
[root@dcalabunixserver srilu]$ cat dirlist.sh
ls > c
# cat c
exec < c
while read line
do
if test -f $lite
then
if test -r $line
then
if test -w $line
then
if test -x $line
then
echo $line
fi
57. fi
fi
fi
done
[root@dcalabunixserver srilu]$ sh dirlist.sh
a.out sample11.txt
iii.AIM: perform the following string operations.
a. To extract a substring from a given string.
SOURCE CODE:
[root@dcalabunixserver srilu]$ cat sample.sh
echo "enter any string"
read s
echo "enter range"
read n
read m
echo "`$s|cut -c $m-$n`"
[root@dcalabunixserver srilu]$ sh sample.sh
enter any string
anusha
enter range
2
5
Nush
b. To find the length of a given string.
58. SOURCE CODE:
echo "enter string"
cat - |wc -c
[root@dcalabunixserver srilu]$ sh len.sh
enter string
computers9
3. simulate the following CPU scheduling algorithms.
a. Round Robin
AIM: Write a C program for ROUND ROBIN CPU scheduling algorithm.
SOURCE CODE:
#include<stdio.h>
main()
{
int s[10],p[10],n,i,j,wi=0,w[10],t[10], st[10],tq,tst=0;
int tt=0,tw=0;
float aw at;
printf("enter no.of process");
scanf("%d",&n);
printf("n enter time quanum");
scanf("%d",&tq);
printf("n enter process&service time");
for(i=0;i<n;i++)
scanf("%d%d",&p[i],&s[i]);
for(i=0;i<n;i++)
st[i]=s[i];
tst=tst+s[i];
for(j=0;j<tst;j++)
for(i=0;i<n;i++)
{
if(s[i]>tq)
{
s[i]=s[i]-tq;
w1=w1+tq;
t[i]=w1;
w[i]=t[i]-st[i];
}
else if(s[i]!=0)
65. 4. write programs for fork, vfork.
AIM: Write a C program that illustrates the creation of child
process using fork( ) system call
Algorithm:
1. Start
2. Declare pid
3. create new process using fork( ) system call
4. If pid!=0 then
5. Display parent process getpid(),getppid().
6. Else
7. Display child process getpid().getppid().
8. End
SOURCE CODE:
#include<stdio.h>
int main( )
{
printf(“original process with pid %d ppid %dn”,
getpid() ,getppid());
pid=fork();
if(pid!=0)
printf(“parent process with pid %d ppid %d n”,
getpid(),getppid());
else
{
sleep(5);
printf(“child process with pid %d ppid %dn”,
66. getpid(),getppid());
}
printf(“ pid %d terminates “,getpid());
}
Output:
original process with pid 3456 and ppid 3525
child process with pid 3457 and ppid 3456
pid 3457 terminates
parent process with pid 3456 and ppid 3525
pid 3456 terminates
AIM: Write a C program that illustrates the creation of child
process using vfork( ) system call
Algorithm:
1. Start
2. Declare pid
3. create new process using vfork( ) system call
4. If pid!=0 then
5. Display parent process getpid(),getppid().
6. Else
7. Display child process getpid().getppid().
8. End
SOURCE CODE:
#include<stdio.h>
int main( )
{
printf(“original process with pid %d ppid %dn”,
getpid() ,getppid());
pid=vfork();
if(pid!=0)
printf(“parent process with pid %d ppid %d n”,
getpid(),getppid());
else
{
sleep(5);
printf(“child process with pid %d ppid %dn”,
getpid(),getppid());
}
67. printf(“ pid %d terminates “,getpid());
}
Output:
original process with pid 3456 and ppid 3525
child process with pid 3457 and ppid 3456
pid 3457 terminates
parent process with pid 3456 and ppid 3525
pid 3456 terminates