You can translate the content of this page by selecting a language in the select box.
How to pipe grep on command line on Windows and Linux?
Let’s find how to pipe grep or find a specific string after running a command using shell, batch and powershell (windows and Linux)
On Linux via shell
ls -al | grep filename
On Windows via powershell
GetChildItem | Select-Object “filename”
or
GetChildItem | where-Object {$_ -match “filename”}On Windows via batch
Dir | findstr “filename”
On both Windows and Linux, you can use the grep
command in combination with the |
(pipe) operator to filter the output of another command. The |
operator takes the output of the command on the left and passes it as input to the command on the right.
Here is an example of how to use the grep
command with the |
operator on both Windows and Linux:
On Linux:
# List all the files in the current directory and filter the output to show only the files that contain the word "example"
ls | grep example
On Windows:
# List all the files in the current directory and filter the output to show only the files that contain the word "example"
dir | findstr example
In this example, the ls
(Linux) or dir
(Windows) command lists all the files in the current directory, and the grep
(Linux) or findstr
(Windows) command filters the output to show only the lines that contain the word “example”.
You can use the grep
command with the |
operator in combination with other command-line utilities to perform various tasks. For example, you can use the grep
command to filter the output of the ps
command to show only the processes that contain a particular string in their command line arguments.
# Show all the processes that contain the string "python" in their command line arguments
ps -aux | grep python
I hope this helps! Let me know if you have any questions.