Linux: find a string recursively within files


Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/3/d98111892/htdocs/insanelabs/htdocs/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524

Well… there are several ways of doing this in Linux and Unix:
find /<path> 'filename.extension> (wildcard allowed)
find /<path> *.* | xargs grep <string>
find . -type f -exec grep "<string>" {} \; -print
grep -r "<string>" /<path>/<filename>

You can also add a ‘>> <filename>’ (no quotes) to the end to write the output to a file so you can take a look at it later.

To recursively find and replace a string within files:
find . -type f | xargs perl -pi~ -e 's/<current string>/<new string>/g;'
sed -i '<current string>/from/to/<new string>' `find . -name \*.ext`

Put `find . -name \*.ext` in a double quote and it should work for filenames with space as well.

If you want to find and delete files or directories recursively, look here.

One thought on “Linux: find a string recursively within files

  1. Pingback:
    Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /homepages/3/d98111892/htdocs/insanelabs/htdocs/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
    Linux: Find and delete files/directories recursively in Linux | Ali Aboosaidi

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.