Mastering grep
Integration with the Pipe Command in Linux
As you begin attending to know the pipe command, one of many instruments you’ll rapidly turn out to be acquainted with is the grep
command.
You recognize these moments if you find yourself caught in log file hell, recognizing errors or navigating by means of directories? Cue the pipe (|
) and grep
instructions! Collectively, they make duties faster and extra exact.
To see how the pipe (|
) command works with grep
, comply with these steps:
1. Open a terminal and execute the next instructions to carry out the next:
- Create three recordsdata (
contact
) (i.e.,/report.txt
,/abstract.txt
, and/notes.pdf
) within the~/Paperwork
listing. - Change the working listing (
cd
) to the~/Paperwork
listing.
These instructions don’t present any output, however the recordsdata will probably be created, which you’ll confirm in step two.
contact ~/Paperwork/report.txt ~/Paperwork/abstract.txt ~/Paperwork/notes.pdf
cd ~/Paperwork
2. Subsequent, run the command beneath, chaining ls
with the grep
command to show recordsdata that finish with .txt
solely.
This command works as follows:
ls
– Lists all recordsdata within the listing.|
– Passes the output ofls
togrep.
grep '.txt$'
– Filters and shows solely these filenames ending with.txt
.
If profitable, the command returns the recordsdata report.txt and abstract.txt, the place the search time period .txt coloured in crimson, as proven beneath.
The pipe (|
) command isn’t a standalone command that can be utilized independently however helps you to chain instructions. This performance permits you to direct one command’s output into one other’s enter, performing complicated duties with minimal effort.
3. Now, repeat the identical command in step two, however this time, change the search time period to report
no matter its file extension.
Discover the command returned the report.txt file with the filename in crimson.