How to find a specific String in file content with specific file name in specific folder ?

gochev

Nayden Gochev

Posted on November 5, 2019

How to find a specific String in file content with specific file name in specific folder ?

It sounds trivial task - you want to “find a specific string in a file content of specific file type starting from the current folder“ and yes it sounds easy right ?

However 99% of the developers use their IDE find in path because they simply cannot, even if they can use the terminal it is just not that easy. The command works on any linux/macos so it should be handy for 99% of the developers out there.

For me personally very often I have to find some texts in some properties files, sometimes this happens on different environments which doesn't have any UI so I have to use the terminal.

In order to be clear how this is used lets say I want to search for the string default.session.timeout=600 which is somewhere in some properties file inside this folder or subfolders.

How to do that ?

find . -name "*.properties" -print | xargs grep default.session.timeout=600
Enter fullscreen mode Exit fullscreen mode

It is a long command but it do the job, it searches everything from current folder "." filtering files by name and/or extension "*.properties" and it prints the content and uses xargs grep to grep the line that contains this string.

I hope this is handy for you, because it was handy for me over the years like 100 times.

Also if you are one of this poor, sad souls that uses Windows for some reason because it works in the Enterprise maybe and the Enterprises just love to work slow I have, a bonus for you - the command for PowerShell.

Get-ChildItem -Recurse -Include *.properties | Select-String "text to search for"
Enter fullscreen mode Exit fullscreen mode

If you ask me this is crazy, someone just told me the command :D I am lacking the unlimited mind of windows engineers to came up with this crazy commands.

💖 💪 🙅 🚩
gochev
Nayden Gochev

Posted on November 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related