Introduction to Unix - POS420Unix Lab Exercise Week 3 BTo.docxmariuse18nolet
Introduction to Unix - POS420
Unix Lab Exercise Week 3 B
Topics :
find
grep
sort
uniq
diff
awk
1. find command
1. To find all files havingan extension of .h in the directory /usr/include
$ find /usr/include -name *.h -print
2. To find all directories recursively (all sub directories also)
$ find . -type d -print
3. To print all the files in the current directory and its subdirectories
$ find . -print
4. To show files that have not been modified in the last day in /tmp directory
$ find /tmp -type f -mtime +1
5. To remove the above files
$ find /tmp -type f -mtime +1 -exec rm -f {} \;
or
$ find /tmp -type f -mtime +1 -ok rm -f {} \;
6. When you come out of vacation and If you forget where the file you were editing, find is very handy (like windows find). Let us search for the file "first.c" from root directory.
$ find / -name first.c -print
2. grep command
1. Get all the lines with the string -Smith” from the phonebook:
$ grep Smith phonebook
2. Get all the lines with the string “ Tom Smith” from the phonebook:
$ grep “Tom Smith” phonebook
3. Get the number of times the string “ksh” occurs in the file /etc/passwd :
$ grep -c ksh /etc/passwd
4. Get the all the lines having the string “Ken” in all the files in the directory :
$ grep Ken *
5. Search whether your friend tom has logged in :
$ who | grep tom
6. Get all the lines in the file intro not having the word “Unix” :
$ grep -v Unix intro
7. Search lower case or upper case pattern "Unix".
$ grep -i Unix intro
8. The -l option gives a list of files having the pattern "Unix".
$ grep -l Unix intro
9. Search for the characters the or The:
$ grep ' [tT]he intro
10. '^' character denotes the beginning of a line and '$' is the end of a line. Search for lines that start with the word 'The'.
$ grep ^The Unix intro
11. Search for lines that end in .pic
$ grep '\.pic$' filename --The escape sequence \ is used for the dot as . is a wild character.
12. Search for containing five-character patterns that start with a capital letter and end with a digit
$ grep '[A-Z]...[0-9]' filename
13. Th e-n option is used to display the line number that matched the pattern.
$ grep -n Unix filename
3. sort command
You might have created the file phonebook from Item No 1 above.
1. Sort the file phonebook:
$ sort phonebook
2. Sort the file phonebook by second column :
$ sort +1 phonebook
3. Sort the file by second column but distinct names:
$ sort +1 phonebook | uniq
4. Sort the file and redirect the output to another file called “sortedusers” :
$ sort phonebook > sortedusers
5. Display the file on the screen , sort and count lines
$ cat phone.
This is the sixth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
Introduction to Unix - POS420Unix Lab Exercise Week 3 BTo.docxmariuse18nolet
Introduction to Unix - POS420
Unix Lab Exercise Week 3 B
Topics :
find
grep
sort
uniq
diff
awk
1. find command
1. To find all files havingan extension of .h in the directory /usr/include
$ find /usr/include -name *.h -print
2. To find all directories recursively (all sub directories also)
$ find . -type d -print
3. To print all the files in the current directory and its subdirectories
$ find . -print
4. To show files that have not been modified in the last day in /tmp directory
$ find /tmp -type f -mtime +1
5. To remove the above files
$ find /tmp -type f -mtime +1 -exec rm -f {} \;
or
$ find /tmp -type f -mtime +1 -ok rm -f {} \;
6. When you come out of vacation and If you forget where the file you were editing, find is very handy (like windows find). Let us search for the file "first.c" from root directory.
$ find / -name first.c -print
2. grep command
1. Get all the lines with the string -Smith” from the phonebook:
$ grep Smith phonebook
2. Get all the lines with the string “ Tom Smith” from the phonebook:
$ grep “Tom Smith” phonebook
3. Get the number of times the string “ksh” occurs in the file /etc/passwd :
$ grep -c ksh /etc/passwd
4. Get the all the lines having the string “Ken” in all the files in the directory :
$ grep Ken *
5. Search whether your friend tom has logged in :
$ who | grep tom
6. Get all the lines in the file intro not having the word “Unix” :
$ grep -v Unix intro
7. Search lower case or upper case pattern "Unix".
$ grep -i Unix intro
8. The -l option gives a list of files having the pattern "Unix".
$ grep -l Unix intro
9. Search for the characters the or The:
$ grep ' [tT]he intro
10. '^' character denotes the beginning of a line and '$' is the end of a line. Search for lines that start with the word 'The'.
$ grep ^The Unix intro
11. Search for lines that end in .pic
$ grep '\.pic$' filename --The escape sequence \ is used for the dot as . is a wild character.
12. Search for containing five-character patterns that start with a capital letter and end with a digit
$ grep '[A-Z]...[0-9]' filename
13. Th e-n option is used to display the line number that matched the pattern.
$ grep -n Unix filename
3. sort command
You might have created the file phonebook from Item No 1 above.
1. Sort the file phonebook:
$ sort phonebook
2. Sort the file phonebook by second column :
$ sort +1 phonebook
3. Sort the file by second column but distinct names:
$ sort +1 phonebook | uniq
4. Sort the file and redirect the output to another file called “sortedusers” :
$ sort phonebook > sortedusers
5. Display the file on the screen , sort and count lines
$ cat phone.
This is the sixth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
Francesca Gottschalk - How can education support child empowerment.pptxEduSkills OECD
Francesca Gottschalk from the OECD’s Centre for Educational Research and Innovation presents at the Ask an Expert Webinar: How can education support child empowerment?
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Honest Reviews of Tim Han LMA Course Program.pptxtimhan337
Personal development courses are widely available today, with each one promising life-changing outcomes. Tim Han’s Life Mastery Achievers (LMA) Course has drawn a lot of interest. In addition to offering my frank assessment of Success Insider’s LMA Course, this piece examines the course’s effects via a variety of Tim Han LMA course reviews and Success Insider comments.
Acetabularia Information For Class 9 .docxvaibhavrinwa19
Acetabularia acetabulum is a single-celled green alga that in its vegetative state is morphologically differentiated into a basal rhizoid and an axially elongated stalk, which bears whorls of branching hairs. The single diploid nucleus resides in the rhizoid.
2. 2
Ex.No.2 SYSTEM VARIABLE
Date:
AIM:
To write a variable value type such as PATH,SHELL, HOST NAME and IFS etc.
PROGRAM:
echo $PATH
echo $HOME
echo $SHELL
echo $HOSTNAME
echo $IFS
OUTPUT:
RESULT:
Hence the output is verified.
3. 3
Ex.No.3 ADMINISTRATIVE COMMANDS
Date:
AIM:
To execute the various system administrative commands such as make directory, remove
directory and various list options.
OUTPUT:
RESULT:
Hence the output is verified.
4. 4
Ex.No.4 DISPLAY THE LIST OF USERS
Date
AIM:
To write a program to display the list of users currently logged in.
PROGRAM:
function line ()
{
echo “ ******* ”
}
echo “yours username: $ (echo $ user)”
line
echo “current date and time : $ (date)”
line
echo “ currently logged on yours: ”
who
line
OUTPUT:
yours user name:
******
current date and time:
thu mar 12 2020, 1.40pm
currently logged on yours:
mtnc lab4 ++y7 2020,1.40pm
********
RESULT:
Hence the output is verified.
5. 5
Ex.No.5 DISPLAY THE LIST OF USERS
Date
AIM:
To write a shell script that displays a list of all files in the current directory to which the user has
read ,write and execute permissions.
PROGRAM:
echo "enter the directory name"
read dir
if [ -d $dir ]
then
cd $dir
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$line has all permissions"
else
echo "files not having all permissions"
fi
fi
done
fi
OUTPUT:
RESULT :
Hence the output is verified.
6. 6
Ex.No.6 DELETE THE FILES
Date
AIM:
To write a shell script to delete all the temporary files.
PROGRAM:
rm -rf / tmp / *
rm -rf / var / tmp / *
fsck -A
exit
OUTPUT:
The file deleted.
RESULT:
Hence the output is verified.
7. 7
Ex.No.7 DISPLAY SOURCE CODE IN GIVEN FILE
Date
AIM:
To write the shell script that accepts a file name, starting and ending line numbers as arguments
and display all the lines between the given line numbers.
PROCEDURE:
1. Start
2. Get the filename.
3. Get the starting and ending line number.
4. Using -n and cat command.
5. Display the given lines of the files.
6. Stop.
PROGRAM:
echo "enter the file name"
read fname
echo "enter the starting line number"
read s
echo "enter the ending number"
read n
sed -n $s,$np $fname | cat > newline
cat newline
OUTPUT:
RESULT:
Hence the output is verified.
8. 8
Ex.No.8 DELETE A LINE CONTAINING SPECIFIED WORD
Date
AIM:
To write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
PROCEDURE:
1. Start.
2. Get the file names as command line arguments.
3. Write “enter the word to search and delete”.
4. Read word.
5. For each argument repeat step 6 to step 8.
6. if it is file then go to next step else display “error in file”.
7. Search the lines not contain specified “word”.
8. Write the above result into same file.
9. Stop.
PROGRAM:
if [ $# -eq 0 ]
then
echo NO ARUGUMENTS
else
pattern=$1
shift
for fname in $*
do
if [ -f $fname ]
then
echo DELETING $pattern FROM $fname
sed '/'$pattern'/d' $fname
else
echo $fname NOT FOUND
fi
done
fi
10. 10
Ex.No.9 FILES OR DIRECTORY DOCUMENTS.
Date
AIM:
To write a shell script that receives any number of file names as its arguments, check if every
argument supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the
number of lines on its to be reported.
PROCEDURE:
1. Start.
2. Get the file names and command line arguments.
3. Check the file is directory or file.
4. Display the number of lines in the file.
5. stop.
PROGRAM:
for x in $*
do
if [ -f $x ]
then
echo "$x is a file"
echo "no of lines in the files are"
wc -l $x
elif [ -d $x ]
then
echo "$x is a directory"
else
echo "enter valid filename or directory name"
fi
done
11. 11
OUTPUT:
RESULT:
Hence the output is verified.
Ex. No.10 .ARITHMETIC AND LOGICAL CALCULATIONS
Date:
AIM:
To write a simple shell script for basic arithmetic and logical calculations.
ALGORITHM:
1. Start the program.
2. Read the two numbers.
3. Perform the arithmetic operations for addition, subtraction, multiplication and division.
4. Compare the two variables ,to find the arithmetic and logical calculations.
5. Display the arithmetic and logical values.
6. Stop.
12. 12
PROGRAM:
#!/bin/sh
a=10
b=10
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a * $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
OUTPUT:
13. 13
RESULT:
Hence the output is verified.
Ex .No. 11.FACTORIAL OF THE GIVEN NUMBER
Date:
AIM:
To write a shell script to find the factorial of a given number.
PROCEDURE:
1. start
2. get the number as “num”
3. calculate the factorial number
4. display the number
5. stop
SHELL COMMAND:
echo "enter a number"
read num
fact=1
while [ $num -gt 1 ]
14. 14
do
fact=$(( fact * num ))
num=$(( num -1 ))
done
echo $fact
OUTPUT:
RESULT:
hence the output is verified.
15. 15
Ex. No.12. STRING OPERATIONS
Date:
AIM:
To write a shell script to perform various operations on given string.
PROCEDURE:
1. start
2. get two strings
3. to perform the string operation such as string compare, string length, reverse the string,
combine the string etc.
4. display the result string
5. stop
SHELL COMMAND:
clear
choice=y
while [ "$choice" = "y" ]
do
echo "____________________________________________"
echo "1. COMPARE TWO STRINGS"
echo "2. JOIN TWO STRINGS"
echo "3. FIND THE LENGTH OF A GIVEN STRING"
echo "4. OCCURRENCE OF CHARACTER AND WORDS"
echo "5. REVERSE THE STRING"
echo "6. EXIT"
echo "____________________________________________"
echo "Enter Choice: "
read ch
echo "____________________________________________"
case $ch in
1)
echo "Enter String1: "
read str1
echo "Enter String2: "
read str2
if [ $str1 = $str2 ]
then
echo "String is equal"
else
echo "String is not equal"
fi
;;
2)
echo "Enter String1: "
read str1
17. 17
case $choice in
Y|y) choice=y;;
N|n) choice=n;;
*) choice=y;;
esac
done
OUTPUT:
RESULT:
Hence the output is verified.
18. 18
Ex. No.13 VOWELS CHECKING
Date:
AIM:
To write an awk script to find the number of lines in a file that do not contain vowels I (or) o
PROCEDURE:
1. start the program
2. to create the text file containing text with vowels and non vowels
3. read the file name (that is text)
4. check the words in the file not containing vowels
5. display the number of lines not containing vowels
6. stop
PROGRAM:
#!/bin/bash
echo "Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels are: ",count}' $file
OUTPUT:
new
20. 20
Ex. No.14. CHARACTER, WORDS AND LINE IN A FILE
Date:
AIM:
To write an awk script to find the number of character, words and lines
PROCEDURE:
1. start
2. get source file name
3. print character
4. print word
5. print lines
6. stop
PROGRAM:
echo Enter the filename
read file
w=`cat $file | wc -w`
c=`cat $file | wc -c`
l=`grep -c "." $file`
echo Number of characters in $file is $c
echo Number of words in $file is $w
echo Number of lines in $file is $l
OUTPUT:
22. 22
Ex No.15 .BINARY SEARCH
Date:
AIM:
To write a shell script to search an element from an array using binary search
PROCEDURE:
1. Start
2. get the size of an array
3. get the elements one by one
4. get the target value
5. start with the middle element. If the target value is equal to the middle element of the array, then
return the index of the middle element. If not compare the middle with other value
6. when a match is found, print successful search
7. if no match is found, print unsuccessful search
8. stop the program
PROGRAM:
echo Enter array limit
read limit
echo Enter elements
n=1
while [ $n -le $limit ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo Enter key element
read key
low=1
high=$n
found=0
while [ $found -eq 0 -a $high -gt $low ]
do
mid=`expr ( $low + $high ) / 2`
eval t=$arr$mid
if [ $key -eq $t ]
then
found=1
elif [ $key -lt $t ]
then
high=`expr $mid - 1`
else
low=`expr $mid + 1`
fi
done
23. 23
if [ $found -eq 0 ]
then
echo Unsuccessfull search
else
echo Successfull search
fi
OUTPUT:
RESULT:
Hence the output is verified
24. 24
Ex. No.16. FILE OPERATIONS
Date:
AIM:
To write a c program that takes one or more file/directory names as command line input and
reports the following information on the file.
A) file type
B) number of links
C)take of last access
D)read,write and execute permissions
PROCEDURE:
1. get the file name in the command line.
2. Display the node number
3. report the file or directory
4. display the number of link
5. display the time of last access
6. display the modification time
7. display the last change time
8. display the read,write and execute permissions
9. stop
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<time.h>
int main(int argc,char *argv[])
{
char *at,*mt,*ct; struct stat buf; stat(argv[1],&buf);
printf("the inode no is : %dn",buf.st_ino); if(S_ISDIR(buf.st_mode))
printf("it is a directory"); if(S_ISREG(buf.st_mode)) printf("it is regurlar filen");
printf("the no of links is %dn",buf.st_nlink); at=ctime(&buf.st_atime);
printf("the time of last access is %sn",at); mt=ctime(&buf.st_mtime);
printf("the modification time is :%sn",mt); ct=ctime(&buf.st_ctime);
printf("time of last change is : %sn",ct); if((buf.st_mode&S_IRUSR)==S_IRUSR)
printf("user has read permissionn"); if((buf.st_mode &S_IWUSR)==S_IWUSR)
printf("user has write permisssionn"); if((buf.st_mode &S_IXUSR)==S_IXUSR)
printf("user has execute permissionn"); if((buf.st_mode & S_IRGRP)==S_IRGRP)
printf("group has read permissionn"); if((buf.st_mode &S_IWGRP)==S_IWGRP)
printf("group has write permissionn"); if((buf.st_mode & S_IXGRP)==S_IXGRP)
printf("group has execute permissionn"); if((buf.st_mode &S_IROTH)==S_IROTH)
printf("others has read permissionn"); if((buf.st_mode &S_IWOTH)==S_IWOTH)
25. 25
printf("others has write permissionn"); if((buf.st_mode &S_IXOTH)==S_IXOTH)
printf("others has execute permisssionn");
}
OUTPUT:
$ gedit sample.c
$ gcc sample.c – o sample
$./ sample
the node number is :4
it is a directory
the number of links is 5
the time of last access is: 12.30 pm
the modification time: 12:40 pm
time of last change :12.40 pm
user has read permission
user has write permission
user has execute permissions
group has read
others has read permissions
others has write permissions
others has execute permissions
RESULT:
Hence the output is verified
26. 26
Ex. No.17 SUSPENDING AND RESUMING PROCESS
Date:
AIM:
To write the program for suspending and resuming process
PROCEDURE:
1. start
2. get the file name
3. call the process
4. enter suspend time of child process
5. enter parent sleep time
6. resume child process
7. terminate the child process
8. end the parent process
PROGRAM:
#include<stdio.h>
#include <ospace/linux.h>
int child _function( )
{
while (true)
{
printf(“child loop n”);
os_this_ process::sleep(1);
}
return 0;
}
int main()
{
os_linux_toolkit initialize;
os_process child(child function);
os_this_porocess::sleep(4);
print(“child.suspend()n”);
child.suspend();
printf(“parent sleep for 4 seconds n”);
os_this _process::sleep(4);
printf(“child.resume()n”);
child.resume();
os_this_process::sleep (4);
printf(“child.terminate()”);
child.terminate();
print(“parent finished”);
return 0;
}