LR02
LR02
Comments
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.
Here,
[options]: These are command-line flags that modify the behavior of grep.
[file]: This is the name of the file(s) we want to search within. we can specify
Output:
Description:
-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.
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
Description:
Functionality:
This command searches the file "daffodildatabase.txt" and counts the occurrences of
lines containing the word "Junior" (case-insensitive).
Breakdown:
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:
Description:
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:
daffodildatabse.txt
coursedatabase.txt
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
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.
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.
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:
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".
output-
Description:
output-
Description:
Description:
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.
output-
Description:
Description:
This command uses grep to find lines where "Sophomore" appears at the very end of the line in
"daffodildatabase.txt".
Description:
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".
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".
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.
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:
❖ -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.
❖ -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.
❖ -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.
The command grep -iR library /home/sadabyte searches for the word "library" (case-
insensitive) recursively within the directory "/home/sadabyte" and its subdirectories.
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.
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.
locate -i “DaffOdilDataBase.txt”
Description:
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.
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.
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.
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