How to use grep to find files and directories

How to use grep to find files and directories

In this short article i will show you how to find files and directories using grep command, grep command is originally for filtering words, but with some tricks we can use it to find files and directories on linux.

We are not just using grep command, we need to use ls and pipe command and then filter the result using grep, the general structure would be like this:
ls [path] | grep [keyword-search]
Here's an example:
ls /home/username/Documents | grep data.txt
The command above is searching for file called data.txt in Documents folder inside home directory, here's some more examples:
ls -a | grep .jpg
ls /var/www/html/ | grep html
ls -a /etc/ | grep host

Share this

Previous
Next Post »