GREP Command In Linux

Photo of author

By Vijay Singh Khatri

The ability to use commands to perform the easiest to complex task in the operating system makes Linux stand apart from both Microsoft Windows and Apple’s Mac Os. In addition to this, in Linux, there are multiple commands to perform a single task. As a result, you can type in the command that you remember, and it will work perfectly without showing you any errors. One of the most used and well-known Linux commands is GREP, and we are going to show you how to use it along with its variations in this article.

A GREP command in Linux is more like a filter for the character search in the text documents. But this is a simplification of its usage, and we will be diving deep to help our readers find out what are the benefits and features of the GREP command. At the same time, we will be discussing some of its alternatives that can be used instead of the GREP command in Linux.

What Is Linux?

Before we move any further, we need to provide our readers a little background of what Linux is in terms of the operating system. Linux is one of the first open-source operating systems that directly manages the system’s hardware and resources such as CPU, GPU, and storage, without needing to rely on any extra layer of contact between the hardware and software. Linux is installed between the application layer and the hardware layer. Therefore, it is used to connect your programs to your computer’s hardware resources.

Linux is more or less based on UNIX OS, but with time, Linux has evolved and it can now run a number of hardware from phones to supercomputers. Every Linux-based operating system has its own Linux Kernel. The main task of Linux Kernel is to manage the hardware resources present in the system. Also, it comes with software packages that lead to the formation of the rest of the operating system.

Furthermore, Linux provides users with components like GNU tools which can be used to manage the resources provided by the Kernel. Also, it makes users install additional software in their OS, tweak the performance and make changes in the security settings according to their needs. Linux has a number of tools and programs like GNU tools which come together to form a functional operating system. One thing a user needs to keep in mind is that Linux is an open-source OS. Thus, the combination of software might have slight differences between various distributors.

Brief History Of Linux

As we said earlier, Linux is based on the UNIX operating system. The creator of Linux, Torvalds, was still in his college when he first developed a working prototype of Linux, which was quite similar to the MINIX. In 1991, Torvalds came out with the 0.02 version of Linux, which also included Linux Kernel version 1.0. During this time, an American software developer along with FSF tried to create an open-source UNIX-like operating system which was known as GNU.

If we compare the development process of Toravlads with Stallman and ESF, we get to know that Stallman was developing the OS by first creating the utilities for the operating system. Once these utilities were made, they were added to the Linux Kernel so as to create a complete GNU/Linux-based OS.

Once Linux was developed, it was accessible to the public free of cost. Throughout the 1990s, Linux development was controlled by hobbyists. Linux is not user-friendly compared to Mac Os and Windows, but it is a more reliable and efficient operating system that rarely experiences abrupt crashes or shutdowns.

If we look at the usage of Linux today, we can see it’s being used in supercomputers, servers, home desktops, smartphones, DVD players, routers, modems, and even in cars. Lastly, Android is also based on Linux, and in its core, it uses the Linux kernel because the developers at Google can easily modify the kernel to make it work with the Android system.

Why Is Linux A Better Operating System Than Microsoft’s Windows?

Now that you know the basics of Linux let’s talk about some of the benefits which a user gets to enjoy when using Linux as their operating system. In addition to this, we will compare Linux with Windows to provide a better picture of how Linux could be a better OS for a user.

1. Open Source

The very first thing which makes Linux favorable over Windows is its complete transparency under the hood. Think of an OS as your car. When you purchase a car, you want to see the engine, which is the heart of the car. Linux, being an open-source project, allows users to look at its source code and will enable users to make changes to it. On the other hand, for Windows, all the code files are locked and can’t be accessed by the user owner of the Windows copy.

2. Secure

There is no doubt about Windows being more vulnerable than the two when it comes to online attacks. But, Linux is less vulnerable than Windows, and it is more secure. When you install Windows, you have to download an Antivirus to keep your computer protected and malware. But with Linux, you do not require any antivirus installation.

Indeed, there are some tools to keep the Linux OS safe from threats, but they are pretty unnecessary due to the inbuilt protection which comes with Linux. In short, Linux is built to be secure by nature. Thus, it not only saves your files and private data but also helps in saving you money that would have been spent on buying an annual subscription to Antivirus.

3. Linux Can Work On Older Computers

If you look at the latest requirements for Windows 11, you can see the bump in the minimum hardware requirements. Some of the tech enthusiasts even claim that upcoming windows 11 will not work on machines that are using processors and other hardware which are older than three years and more. Speaking of compatibility, a Linux operating system can work on the smallest of computers called Raspberry Pies, which comes with bare-metal hardware.

Also, as we pointed out earlier, Linux works as a middle layer between the software layer and hardware layer. Thus, it is more efficient in the usage of hardware, which makes Linux an ideal OS for both low-end and high-end hardware. As a result, you will find that most of the companies which are providing server hosting services prefer running Linux OS on their dedicated and shared servers.

4. Updates

Whenever a new update comes to Windows, you are surely going to see some errors or bugs, which will be taken out in the next few patches after the update. It is a common theme for Microsoft to push Windows users for updates when Windows receives some set of problems. Thus, when you run the update, there is a high potential that your OS will break, causing some issues for its standard processing. For Linux, the OS update happens for the smallest of the bugs, and most of these bugs won’t affect the normal working of the operating system if you have installed a Long Term Support Linux OS. As a result, with Linux, you are going to receive more constant updates for the problems you are facing in the OS than Windows.

5. Freedom

Linux is the operating system that gives its user the freedom to make changes in all the different aspects of the OS. Being open-source, you can make changes in the source code and modify it according to your needs. There is no bloatware present in Linux, and it allows you to install all the desired software programs that you need to perform a specific task. In addition to this, you can also install Linux on a USB drive and run it from there. Additionally, there are a variety of Linux flavors available. You can select from a variety of user interfaces and functionality based on the type of work you do on your system.

What GREP Command In Linux?

GREP stands for Global Regular Expression Print, and this command is used for searching out the string of characters or a particular word that is present in the specified file. The regular expression is the method of searching the text pattern in the files given. When a GREP command finds out the string which you are looking for, it will print out the location of the same as a result. The most helpful scenario where the GREP command is efficient is in searching through large log files.

Breaking Down GREP Command

Now let’s try to understand the three different parts of the GREP command. Given below is a simple example of the GREP command in Linux:

grep myname samplefile

  • In the above example, grep is the first part which is the command.
  • The second part is the string of characters myname, which we are searching for.
  • The last part is the name of the file samplefile in which we are searching the string of characters.

When you are using the above command, then Grep will show you each line that matches the string myname. One thing to keep in mind is that when you are executing this command, you are not going to get the exact match of the string you have entered. Instead, the terminal is going to print out the words which contain the mentioned string.

Examples Where GREP Command Can Be Used

Some of the user cases of the GREP command are written down below, and you can try these out in your machine to find out the preferred result.

1. Search & Find Files

With this command, you can find out which version of the software is installed in your operating systems. In addition to this, you can also find out the various versions of the same program which are present in your system.

grep -i python

2. Search & Filter Files

This is the most common usage of the GREP command; with the help of this command, you can do both search and filter within the individual and multiple files. Let’s take an example where you are having some issues with your Apache server. You are looking for a solution in an online web forum, but for that, you need to post the content of your file. Well, pasting out the whole content is not advisable as it can lead to a data breach. But what you can do is, you can remove all the commented sections of your code file. This can be done by using the following command written below.

grep –v “#” /etc/apache2/sites-available/default-ssl

3. Count The Number Of Matches

This is a built-in piping command for GREP, which will provide you with the word count of the number of times the string was present in the given file.

grep -c myname

4. Search The File By The String Present Inside

To find out the specific string in the file, you need to type in the following command.

grep “this is my name” samplefile

5. Matching Regular Expressions In Files

This is one of the most powerful features of GREP if you are working with regular expressions in Linux. With the command written below, you get to search all the patterns, which start with the “Good Morning” string of characters and end with the “day” with anything coming in between these two strings in the given file.

grep “Good Morning. *day” samplefile

6. Displaying Lines Before/After & Around The Match

GREP can also show you the lines which are before and after the given match of string. It could be beneficial when you have a large file at hand to work with. The command given below will showcase the N number of lines that are present in the file after the match “what.”

grep -A <N> “What” samplefilename

To find out the number of lines present before the string-match “what” you need to type in the following command.

grep -B <N> “What” samplefilename

7. Inverting The Match

There are so many options when it comes to matching the lines, and there are options to show the lines before the match, lines after the match, and the highlight match. So there is a command option in GREP from which you can display the lines which do not have the matched string.

grep -v “goodbye” samplefile

This command will show you all the lines which don’t have the word goodbye.

Conclusion

This is what the GREP command does in Linux, and these are some of the most common options you can use in order to find out the results for the string of characters in a file. While there are more options for the GREP command, we have showcased only the most useful ones to make it easier for you to understand the true purpose of the command. The GREP command has few alternatives, but none of them is as useful as this one. Hence, most developers and professional users prefer GREP for searching specific strings in files.

Leave a Comment