0% found this document useful (0 votes)
25 views25 pages

LR02

Uploaded by

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

LR02

Uploaded by

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

Lab Report

Only for course Teacher


Needs Developing Sufficient Above Total
Improvement Average Mark

Allocate mark & Percentage 25% 50% 75% 100% 5


Clarity 1
Content Quality 2
Spelling & Grammar 1
Organization and 1
Formatting
Total obtained mark

Comments

Semester: Spring 2024


Student Name:
Student ID:0242220005341
Batch: 39 Section:B
Course Code: SE 233 Course Name: Operating System & System
Programming Lab
Course Teacher Name: Samia Sayed
Designation: Lecturer
Submission Date: 7/05/2024
Introduction

The grep command, which stands for "global regular expression print," is widely known
for its ability to do regular expression-based text searches, which makes it an essential
tool for tasks like text processing and pattern matching. In the meantime, the find
command, which offers flexibility and precision in file searches, excels at navigating
directory structures to discover files based on several criteria. Conversely, the locate
command uses a pre-configured database to search the filesystem for filenames quickly,
however it has some restrictions.

In this lab report, we examine the features and uses of these commands, looking at their
choices, syntax, and real-world applications. By using practical experimentation and
analysis, we hope to learn more about the functions of grep, find, and locate in the
Topic 1.1 :grep command With example and description

The grep command in Unix/Linux is a powerful tool used for searching and
manipulating text patterns within files. Its name is derived from the ed (editor)
command g/re/p (globally search for a regular expression and print matching lines),
which reflects its core functionality. grep is widely used by programmers, system
administrators, and users alike for its efficiency and versatility in handling text data. In
this article, we will explore the various aspects of the grep command.

Syntax of grep Command in Unix/Linux:

grep [options] pattern [files]

Here,

[options]: These are command-line flags that modify the behavior of grep.

[pattern]: This is the regular expression we want to search for.

[file]: This is the name of the file(s) we want to search within. we can specify

multiple files for simultaneous searching.

Options Available in grep Command:

A Practical Illustration of the Linux grep command:


Daffodildatabase.txt link of the text file used to test the commands
1. Case insensitive search:
The -i option enables to search for a string case insensitively in the given file. It matches the
words like “UNIX”, “Unix”, “unix”.

grep -i "junior" daffodilsadabse.txt

Output:
Description:

Here's a breakdown of the command grep -i "junior" daffodilsadabse.txt:

 -grep: This is a command-line utility used for searching plain text files for specific
patterns.
 -i: This option tells grep to perform a case-insensitive search. In other words, it
won't differentiate between uppercase and lowercase letters when matching the
pattern.
 "junior": This is the pattern you're searching for within the file. In this case, it's
looking for lines containing the word "junior" (or "Junior", "JUNIOR", etc., due to
the -i flag).
 daffodilsadabse.txt: This is the text file where grep will search for the pattern.
 Overall, this command searches the file "daffodilsadabse.txt" for any lines
containing the word "junior", regardless of its case.

2. Displaying the Count of Number of Matches Using grep:

We can just display the files that contains the given string/pattern.
The command -
grep -c "Junior" daffodildatabase.txt
Description:

This command uses grep to search for the pattern "Junior" in the file "daffodildatabase.txt",
but with an additional option:

 grep: Same as before, this is the command-line utility for searching text files.
 -c: This option instructs grep to count the number of lines in the file that match the
pattern instead of printing the lines themselves.
 "Junior": The pattern you're searching for. Just like before, this searches for the
exact string "Junior".
 daffodildatabase.txt: The text file where grep will perform the search.

In simpler terms, this command counts how many times the word "Junior" (case-
sensitive) appears in the file "daffodildatabase.txt".

If we want to avoid case sensitivity in this command we have to use this command
grep -c -i "Junior" daffodildatabase.txt

We can even remove the invited command of the pattern the output would be the same

grep -c -i Junior daffodildatabase.txt

Description:
Functionality:

 This command searches the file "daffodildatabase.txt" and counts the occurrences of
lines containing the word "Junior" (case-insensitive).

Breakdown:

 grep: The command-line utility for searching text files.


 -c: Instructs grep to count the matching lines instead of printing them.
 -i: Makes the search case-insensitive. So, it will find "Junior", "JUNIOR", "junior",
etc.
 Junior: The pattern to search for in the file.
 daffodildatabase.txt: The text file where the search will be conducted.

Output:

The command will output a single number representing the total number of lines in
"daffodildatabase.txt" that contain the word "Junior" (regardless of case).

3. Display the File Names that Matches the Pattern Using grep:

The command we have to used to show this is

grep -l "Science" daffodildatabse.txt coursedatabase.txt

Description:

The command grep -l "Science" daffodildatabse.txt coursedatabase.txt uses grep to search


for lines containing the word "Science" but with a specific twist:

 grep: The command-line utility for searching text files.


 -l: This option tells grep to only list the filenames containing the pattern, instead of
printing the matching lines themselves.
 "Science": The pattern to search for in the files.
 daffodildatabse.txt coursedatabase.txt: These are the two text files that will be
searched.

In essence, this command searches for the presence of the word "Science" in the files
"daffodildatabse.txt" and "coursedatabase.txt" and outputs the filenames that contain
it.
Here's a breakdown of the output:

There are two possible scenarios:

1. Both files contain "Science":


The command will print:

daffodildatabse.txt
coursedatabase.txt

2. Only one or none of the files contain "Science":


The command will print nothing because the -l option only outputs filenames with
matches.

Key points:

 grep -l is useful when you want to know which files contain a specific pattern without
seeing the actual matching lines.
 This command won't differentiate between "Science", "science", or other variations of
the word unless you use the -i flag for a case-insensitive search.

If we want ot avoid case sensitivity we have to add -i command along with the -l

Here is the command


grep -l -i "science" daffodildatabse.txt coursedatabase.txt

4. Checking for the Whole Words in a File Using grep:

By default, grep matches the given string/pattern even if it is found as a substring in a file.
The -w option to grep makes it match only the whole words.
command-
grep -w "Senior" daffodildatabse.txt

Output-
Description:

The command grep -w "Senior" daffodildatabse.txt searches for the term "Senior" in the file
"daffodildatabse.txt" with an added precision:

 grep: Same as before, this is the workhorse for searching text files.
 -w: This option tells grep to only find whole words that match the pattern, not parts of
words.
 "Senior": The pattern you're searching for. It needs to be an entire word, not just
appearing within another word.
 daffodildatabse.txt: The text file where grep will perform the search.

In simpler terms, this command searches "daffodildatabse.txt" for lines where


"Senior" appears as a complete word, not just a substring.

For example:

 If a line says "Computer Science Senior", this command would find it.
 However, if a line says "We are looking for Seniors...", it wouldn't be found because
"Seniors" is plural and not a standalone word.

6. Show Line Number While Displaying the Output Using grep -n :

To show the line number of file with the line matched.

grep -n "Senior" daffodildatabase.txt

output-
Description:
The command grep -n "Senior" daffodildatabase.txt searches for the term "Senior" in the file
"daffodildatabase.txt" while providing additional information in the output:

 grep: The familiar command-line tool for searching text files.


 -n: This option instructs grep to print the line number along with any matching lines.
 "Senior": The pattern you're searching for. It looks for lines containing the exact
word "Senior" (case-sensitive).
 daffodildatabase.txt: The text file where grep will conduct the search.

Description:

This command searches for all occurrences of the word "Senior" (case-sensitive) within
"daffodildatabase.txt" and displays the following in the output:

 Line number: This precedes each matching line, indicating where "Senior" appears in
the file.
 Matching line: The actual line from the file that contains "Senior".

7. Inverting the Pattern Match Using grep:


You can display the lines that are not matched with the specified search string pattern using
the -v option.

grep -v "Junior" daffodildatabase.txt

output-

Description:

The command grep -v "Junior" daffodildatabase.txt searches the file "daffodildatabase.txt"


for lines that don't contain the word "Junior" (case-sensitive):

 grep: The command-line utility for searching text files.exclamation


 -v: This option tells grep to print the lines that don't match the pattern.exclamation
 "Junior": The pattern you're searching for. It looks for lines containing the exact
word "Junior".
 daffodildatabase.txt: The text file where grep will perform the search.

8. Matching the Lines that Start with a String Using grep


The ^ regular expression pattern specifies the start of a line. This can be used in grep to
match the lines which start with the given string or pattern.

grep "^Student" daffodildatabase.txt

output-

Description:

The command grep "^Student" daffodildatabase.txt searches for lines in


"daffodildatabase.txt" that start with the word "Student" (case-sensitive):

 grep: The trusty tool for searching text files.


 ^: This metacharacter matches the beginning of a line.
 "Student": The pattern you're searching for. It needs to be the first word on the line.
 daffodildatabase.txt: The text file where grep will conduct the search.

Description:

This command specifically searches for lines in "daffodildatabase.txt" where "Student" is


the first word, not just anywhere within the line.

9. Matching the Lines that End with a String Using grep

The $ regular expression pattern specifies the end of a line. This can be used in grep to
match the lines which end with the given string or pattern.

grep "Sophomore$" daffodildatabase.txt

output-
Description:

The command grep "Sophomore$" daffodildatabase.txt searches for lines in


"daffodildatabase.txt" that end with the word "Sophomore" (case-sensitive):

 grep: The familiar command-line tool for searching text files.


 "Sophomore": The pattern you're searching for. It needs to be the last word on the line.
 $: This metacharacter matches the end of a line.
 daffodildatabase.txt: The text file where grep will perform the search.

Description:

This command uses grep to find lines where "Sophomore" appears at the very end of the line in
"daffodildatabase.txt".

10.Specifies expression with -e option


Can use multiple times :
grep –e "Junior" –e "Senior" daffodildatabase.txt
output-

Description:

The command grep –e "Junior" –e "Senior" daffodildatabase.txt searches for lines in


"daffodildatabase.txt" containing either "Junior" or "Senior" (case-sensitive):

 grep: The command-line tool for searching text files.


 -e: This option allows you to specify multiple search patterns. You can use it multiple
times with different patterns.
 "Junior": The first pattern you're searching for. It looks for lines containing the exact
word "Junior".
 "Senior": The second pattern you're searching for. It looks for lines containing the exact
word "Senior".
 daffodildatabase.txt: The text file where grep will conduct the search.

Description:

This command essentially performs an OR search using grep. It will display any line in
"daffodildatabase.txt" that contains either "Junior" or "Senior" (or even both).

11. -f file option Takes patterns from file, one per line :
grep –f pattern.txt testdb.txt

Description-
The command grep -f pattern.txt testdb.txt searches for lines in the file "testdb.txt" that
match any of the patterns listed in a separate file named "pattern.txt".

Here's a breakdown of the command:

 grep: The command-line utility used for searching plain text files.
 -f: This option tells grep to read the search patterns from a file instead of specifying
them on the command line.
 pattern.txt: This is the file containing the list of patterns to search for. Each line in
"pattern.txt" represents a separate pattern.
 testdb.txt: The target text file where grep will search for lines matching the patterns
in "pattern.txt".

In simpler terms:

1. grep opens "pattern.txt" and reads the search patterns line by line.
2. It then goes through each line in "testdb.txt".
3. For each line in "testdb.txt", grep checks if it matches any of the patterns from
"pattern.txt".
4. The output will only display the lines from "testdb.txt" that contain at least one of the
patterns found in "pattern.txt".

Here are some additional points to consider:

 The content of "pattern.txt" can be anything you want to search for. It can include
words, phrases, regular expressions, or any combination of these.
 The order of patterns in "pattern.txt" doesn't affect the search results. grep searches
for any matching pattern in each line of "testdb.txt".
 If "pattern.txt" is empty or doesn't exist, grep will usually exit with an error message.

12. Print n Specific Lines from a File Using grep:


-A prints the searched line and n lines after the result.
-B prints the searched line and n lines before the result.
-C prints the searched line and n lines after and before the result.

❖ grep -A[NumberOfLines(n)] [search] [file]



❖ grep -B[NumberOfLines(n)] [search] [file]

❖ grep -C[NumberOfLines(n)] [search] [file]
Output-1grep -A6 Senior daffodildatabase.txt

Output-2 grep -A6 Senior daffodildatabase.txt

Output-3 grep -C6 Biology daffodildatabase.txt

Description -

These commands utilize grep to search for a pattern in a file but also display additional
context around the matching lines. Let's break down each variation:

1. grep -A[NumberOfLines(n)] [search] [file]

❖ -A: This option tells grep to print the matching line itself plus the
[NumberOfLines(n)] lines after it.
❖ [NumberOfLines(n)]: This is replaced with an actual number of lines you want to
display after the match.
❖ [search]: The pattern you're searching for in the file.
❖ [file]: The text file where grep will perform the search.

This command searches for the word "error" in "systemlog.txt" and displays the line
containing "error" along with the following 3 lines (because -A3 is specified). This helps
you understand the context surrounding the error message.

2. grep -B[NumberOfLines(n)] [search] [file]

❖ -B: This option tells grep to print the matching line itself plus the
[NumberOfLines(n)] lines before it.
❖ [NumberOfLines(n)]: This is replaced with an actual number of lines you want to
display before the match.
❖ [search]: The pattern you're searching for in the file.
❖ [file]: The text file where grep will perform the search.

This command searches for the word "warning" in "programlog.txt" and displays the line
containing "warning" along with the previous 2 lines (because -B2 is specified). This helps
you see what led up to the warning message.

3. grep -C[NumberOfLines(n)] [search] [file]

❖ -C: This option tells grep to print the matching line itself plus [NumberOfLines(n)]
lines before and after it.
❖ [NumberOfLines(n)]: This is replaced with an actual number of lines you want to
display in both directions from the match.
❖ [search]: The pattern you're searching for in the file.
❖ [file]: The text file where grep will perform the search.

13. Search Recursively for a Pattern in the Directory:


-R prints the searched pattern in the given directory recursively in all the files.

grep -R [Search] [directory]

output- grep -iR library /home/sadabyte


Description:

The command grep -iR library /home/sadabyte searches for the word "library" (case-
insensitive) recursively within the directory "/home/sadabyte" and its subdirectories.

Here's a breakdown of the options used:

❖ grep: The command-line utility for searching text files.


❖ -i: This option makes the search case-insensitive. So, it will find "library", "Library",
"LIBRARY", etc.
❖ -R: This option tells grep to search recursively within directories. It will search the
"/home/sadabyte" directory and any subdirectories it contains.
❖ library: The pattern you're searching for in the files.
❖ /home/sadabyte: The starting directory where the recursive search will begin.

Explanation:

1. grep starts searching for files containing the word "library" (regardless of case) within
the directory "/home/sadabyte".
2. The -R option instructs grep to descend into any subdirectories found within
"/home/sadabyte" and continue searching for "library" in those subdirectories as well.
3. grep will report any files containing "library" (case-insensitive) along with the full
path to the file.
1.2locate command in Linux with Examples

Locate command in Linux is used to find the files by name. There are two most widely used file-
searching utilities accessible to users called to find and locate. The locate utility works better and faster
than the find command counterpart because instead of searching the file system when a file search is
initiated, it would look through a database. This database contains bits and parts of files and their
corresponding paths on your system. By default, locate command does not check whether the files
found in the database still exist and it never reports files created after the most recent update of the
relevant database.

Syntax of `locate` command in Linux :

locate [OPTION]... PATTERN..

Options Available in `locate` command in Linux:

1. -A, --all: It is used to display only entries that match all PATTERNs instead of requiring only one of
them to match.
Command:
Locate -A library
Description:

The command locate -A library attempts to search for files containing the word "library" on your Linux
system. Here's a breakdown of the command:

❖ locate: This is a command-line tool used to find the location of files based on their filenames or
content (depending on the system configuration).
❖ -A: This is an option for locate. By default, locate only searches for filenames. The -A option
tells it to also search within the content of files.
❖ library: This is the search term you're interested in. locate will search for files containing the
word "library".

2. -b, --basename: It is used to match only the base name against the specified patterns.
locate -b Experiment8.pdf

Description:
❖ Function: Searches for files and directories whose base names match the specified pattern.
❖ Use Case: Useful when you know the name of the file or directory but not its exact location
within the filesystem.
❖ Note: The locate command relies on a pre-built database (typically updated by a periodic system
cron job) of filenames and their corresponding paths. It's faster than using find command but
might not be as up-to-date since it doesn't search the filesystem directly.

3. -c, --count: It is used for writing the number matching entries instead of writing file names on
standard output.

locate -c “*.svg”

Description:
The locate command with the -c option and a wildcard pattern like *.svg is used to count the number of
files in the system that match the specified pattern.
❖ Command: locate -c "*.svg"
❖ Function: Counts the number of files and directories with names ending in ".svg".
❖ Use Case: Useful when you want to know how many SVG files are present in the system
without listing their paths.
❖ Note: The *.svg pattern matches all files with names ending in ".svg".

4. -e, --existing: It is used to display only entries that refer to existing files during the
command is executed.
Locate -e “*.html”

Description:

❖ Command: locate -e
❖ Function: Restricts the search to only return results for existing files and directories.
❖ Use Case: Useful when you want to ensure that the search results correspond to
currently existing files.
❖ Note: Without the -e option, locate may still return paths for files that have been
deleted or moved since the last database update.

5. -i, --ignore-case: It is used to ignore case sensitivity of the specified patterns.

locate -i “DaffOdilDataBase.txt”

Description:

❖ Command: locate -i "DaffOdilDataBase.txt"


❖ Function: Searches for files and directories with names matching
"DaffOdilDataBase.txt" regardless of case sensitivity.
❖ Use Case: Useful when you're unsure of the exact capitalization of the filename or
when you want to perform a case-insensitive search.
❖ Note: By default, locate is case-sensitive, so without the -i option, it would only
match filenames exactly matching the specified case.

6. -m, --mmap: It is used to ignore the compatibility with BSD, and GNU locate.

locate -m library.txt
Locate -m pattern

Description:
❖ Command: locate -m
❖ Function: Limits the number of search results returned by locate.
❖ Use Case: Useful when you expect a large number of matches but only want to see a subset of
them.
❖ Note: The results returned are not necessarily sorted in any specific order.
1.3 Find Command in Linux/Unix with Examples
The find command helps us to find a particular file within a directory. It is used to find the list of files
for the various conditions like permission, user ownership, modification, date/time, size, and more.

In Unix-like and other operating systems, the find command is a command-line utility that finds files on
the basis of a few user-specified formats and either prints all matched object's pathname or, if other
actions are requested, implements that action on all matched objects.

It starts a search from a wanted to start location and, after that, recursively traverses the directories
(nodes) of a hierarchical structure (generally a tree). The find command can search and traverse from
different file partition systems belonging to a single or more storage device under the starting directory.

The search format contains a pattern for matching against the filename or the time range for matching
against the modification time or file access time. The find command provides a list of every file under
the current working directory by default. However, users can restrict the search to a desired maximum
level upon the starting directory.

Syntax of Find Command in Linux :

find [path] [options] [expression]

Description:
❖ path: Starting directory for the search.
❖ Example: find /path/to/search
❖ options: Additional settings or conditions for the search.
❖ Example: find /path/to/search -type f -name "*.txt"
❖ expression: Criteria for filtering and locating files.
❖ Example: find /path/to/search -type d -name "docs"

1. -name pattern: It checks that the file name is the same as the given shell-glob pattern or not.

find path -name “filename.txt”


Description:
The find command is used to search for files and directories within a specified directory hierarchy based
on a wide range of criteria, including filenames, file types, permissions, ownership, and more.

Basic Syntax- find <directory> <options> <expression>

❖ <directory>: Specifies the starting directory for the search.


❖ <options>: Optional flags that modify the behavior of the find command.
❖ <expression>: Specifies the search criteria and actions to be performed on matching files and
directories

2. -type type: It checks that the file is a provided type.


find [path] -type [option]
Finding all the directories:
Finding files :
Find path -type f

Description:
 Option: -type
Function: Specifies the type of file to search for.
 Arguments: It accepts various arguments to specify different types of files:
 f: Regular file.
 d: Directory.
 l: Symbolic link.
 c: Character device file.
 b: Block device file.
 p: Named pipe (FIFO).
 s: Socket.

3.-exec program [argument ...];: It always gives the true value. It executes a program with the fixed
given arguments and the current file path.
find . -type f -name "*.png" -exec ls -l {} \;

Description :

 find: This is the command itself. It is used to search for files and directories within a directory
hierarchy.
 .: This specifies the starting directory for the search. In this case, . represents the current
directory.
 -type f: This option specifies that only files should be considered in the search. It filters out
directories.
 -name "*.png": This option specifies a filter for filenames. It tells find to only consider files
whose names match the pattern "*.png", meaning files with the .png extension.
 -exec ls -l {} \;: This is the action to be taken for each file found. It tells find to execute the ls -l
command on each file. Here's what each part means:
 -exec: This option indicates that the following command should be executed.
 ls -l: This is the command to be executed on each file found. ls lists directory contents, and -l
specifies a long listing format, which includes detailed information about each file.
 {}: This is a placeholder that represents the path to each file found by find.
 \;: This signifies the end of the -exec command. It's necessary to terminate the -exec command
properly

4. -print0: It always gives the true value. It prints the current file name and a null character to stdout.
Not needed by POSIX.
find /path/to/search -type f -name "*.txt" -print

Description:
 /path/to/search is the directory where you want to start searching.
 -type f specifies that you're interested in files (not directories).
 -name "*.txt" is a filter that specifies you're only interested in files with a .txt extension.
 -print is the action to be taken for each file found. It simply prints the pathname of each file that
matches the criteria.

5--maxdepth levels: Restricts the search to a specified directory depth.


Find . -maxdepth 5 -type f -name “*.txt”

6. -delete : Deletes files that match the specified criteria.


find . -type f -name "*.jpg" -delete

Description:
 find: The command to locate files and directories based on criteria.
 -delete: This option instructs find to delete any files or directories that match the search criteria.

Important Points:

 Destructive: find -delete is extremely powerful and can lead to accidental deletion of important
data if not used cautiously.
 Confirmation Missing: There's no built-in confirmation prompt before deletion. Once
executed, the files are gone.

7.-iname pattern : Case-insensitive version of `-name`. Searches for files with a specific name or
pattern, regardless of case.
Thank You

You might also like