31 C
Jaipur
Thursday, October 29, 2020

grep Command – Linux Commands Guide

Must read

America And Social Media Sites Have A Love-Hate Relationship

The U.S. is the birthplace of many of the major media sites we use today. Still, the American population shares a love-hate relationship with...

grep stands for Global Regular Expression Print. grep command is available in Unix/Linux based operating systems. As the full-form of the tool suggests that it is used for searching any text or expression in the given file(s).

It is useful when you are searching for a line in a file that contains a specific keyword. grep command can also use options to advance the search query.

The basic syntax of the grep command –

grep expression filename

The basic syntax of the command requires at least two inputs. The first input is the expression or keyword that you want to search for. The second argument is the file to search.

For the demonstration purposes, I have created four files in a directory called ‘Files’. Each file contains some lines of text.

Files
Files

I will start from the basic usage of the grep command and further advance the query using the tools options.

grep argument_1 file.txt
grep basic search
grep basic search

As you can see, the above command has grabbed the line from file.txt that contains the expression ‘argument_1‘. It’s simple. Right?

grep command – search from multiple files

Similarly, we can also search for any expression in multiple files by passing files names separated with space.

grep expression_4 file.txt file1.txt
grep multiple files
grep search multiple files

grep command – Search all files in a directory

You can also search all the files stored in a directory at once. Here is how you can do it –

grep argument_4 *
grep search all files in directory
grep search all files in directory

grep – Search files in all subdirectories (Recursive search)

If the files are stored in multiple subdirectories, then it is possible to search all the files in the main directory and subdirectories at the same time using -r option.

grep -r argument_3 *
grep search recursive
grep search recursive

Print only a number of lines from results

By default, grep will print all the lines that contain the given expression. For example, if I search for the word “Hello” in a file, there is a possibility that the word “Hello” is mentioned multiple times in that file. So the command will print all the lines that contain the word Hello.

grep command output
grep command output

This behavior can be modified by providing the -m option to the command.

grep -m 1 argument_1 file.txt
limit output lines
limit output lines

Print bytes offset with the line

To print the bytes offset with the found line, pass the -b option to the command.

grep -b argument_1 file.txt
grep print bytes offset
grep print bytes offset

Print line number with line

You can also print the line number before each line that it finds.

grep -n argument_1 file.txt
print line number
print line number

Hide filename from lines

If you search multiple files, by default the tool also prints the filename before each line. To hide the line number, use -h argument.

print filename with lines
print filename with lines
grep -h argument_1 file.txt file1.txt
Hide filename from lines
Hide filename from lines

Print only matching expression

By default, the tool prints the whole line with the expression. If you only want to print the expression, use -o option.

grep -o argument_2 file.txt file1.txt
Print only matching word
Print only matching word

grep command – Search files match the specific pattern

To only search files that match a specific pattern, use the -R option in the command.

grep -R --include=*.txt argument_3 *
search files with pattern
search files with pattern

Exclude files & directories match the specific pattern

To exclude files and directories from the search that match the specific pattern, use -R --exclude option in the command.

grep -R --exclude=*.txt argument_3 *
Exclude files match specific pattern
Exclude files match specific pattern

grep command – Exclude directories match the specific pattern

To exclude directories from the search, use -R --exclude-dir option in the command.

grep -R exclude-dir=Files argument_3 *
Exclude directories match specific pattern
Exclude directories match specific pattern

grep command – Print only filenames without a match

To print filenames that do match the expression, use the -L option in the command.

grep -L argument_3 file.txt file1.txt file2.txt
print filenames without match
print filenames without match

Print only filenames with a match

To only print filenames that match the expression, use -l option in the command.

print filenames with match
print filenames with match

Combine options

If you want to pass multiple options in a single query, this is how you can do that –

grep -nohR --include=*.txt argument_3
grep combine options
grep combine options

Conclusion

So there you have it. grep command is useful when writing bash scripts, or performing a search for an expression in a directory structure with hundreds of files. Just practice the command and try to combine options together to further filter the results as you wish.

If you want more help, please refer to the man pages or leave a comment below this article.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

America And Social Media Sites Have A Love-Hate Relationship

The U.S. is the birthplace of many of the major media sites we use today. Still, the American population shares a love-hate relationship with...

Big Tech CEOs And Section 230 Hearing: 5 Biggest Takeaways

The Senate Commerce Committee Section 230 hearing that took place in the U.S. included CEOs of Facebook, Google, and Twitter. What started with Trump’s...