It selects and prints the lines from a file which matches a given string or pattern.
Syntax:- grep [options] pattern [file]
- Also, This command searches the specified input fully for a match with the supplied pattern and displays it.
- While forming the patterns to be searched we can use shell match characters or regular expressions.
- So, Let us begin with the simplest example of usage of gre.
- Ignore case distinctions
- Invert the sense of matching, to select non-matching lines.
- Select only those lines containing matches that form whole words.
- So, Select only matches that exactly match the whole line.
- print a count of matching lines for each input file.
$ grep “Error” logfile.txt
- This searches for the string “Error” in the log file and prints all the lines that have the word “Error”.
$ grep “string” file1 file2
- Searching for a string of multiple files.
$ grep -i “UNIX” file.txt
- The -i option enables to search for a string case-insensitively in the given file. Moreover, It matches the words like “UNIX”, “Unix”, “Unix”.
$ grep -w “world” file.txt
- By default, grep matches the given string/pattern even if it found a substring in a file. The -w option to grep makes it match only the whole words. $ grp -c “sting” file.txt
- So, We can find the number of lines that match the given string/pattern $ grp -l “string” *
- Moreover, We can just display the files that contain the given string/pattern. $ grep -n “string” file.txt
- Also, We can make the grep command to display the position of the line which contains the matched string in a file using the -n option
Displaying your current directory name (Print working directory).
Syntax: pwd [options]
- At the time of logging in the user is placed in the specific directory of the file system. You can move around from one directory to another, but any point of time, you are located in only one directory. This directory is known as your current directory.
- pwd command tells your current directory.