DEV Community

Nayden Gochev
Nayden Gochev

Posted on

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.

Top comments (2)

lbonanomi profile image

Please consider handling whitespace:

find . -name "*.properties" -print | while read file
    grep default.session.timeout=600 "$file"
gochev profile image
Nayden Gochev

that can be handy for someone thanks :D:D:D btw I have never had to search for something with spaces, but I can imagine for translations and other stuff that this can be handy :)