Back

/ 1 min read

find vs grep, whats the difference ?

find and grep, whats the difference ?

Check it out as a Video

find - Search for files

find lets you search for files inside a folder. for example, let’s find all the files that have the letter A in their name:

Terminal window
find . -name "*a*"

grep - Search for content

grep on the other hand, lets you search for words or pattern inside a file. so let’s search for the word long inside our tasks:

Terminal window
grep "long" tasks

I can just ls | grep ‘a’ right ?

That correct! but only in some cases.

ls lists a folders content, and with -R, it can list them recursively in subfolders of subfolders, piping that into grep, we can filter the result.

Terminal window
ls -R | grep 'a'
--- result ---
tasks.txt

But then why use find ?

Because find, searches recursively by default, and can apply multiple filters like: name, size, type, and many more. And it actually shows the path to the file.

Terminal window
find . -type f -size -10M -name "*a*"
--- result ---
./folder1/tasks.txt

So, ls | grep 'a' filters the result of listing a directory, while find, searches for files recursively by applying conditions and can even performs actions on them.

Hope this helps, See ya!