Skip to main content
GrN.dk

Main navigation

  • Contact
  • Your Digital Project Manager
  • About Greg Nowak
  • Services
  • Portfolio
  • Container
    • Excel Freelancer
    • Kubuntu - tips and tricks
    • Linux Apache MySQL and PHP
    • News
    • Image Gallery
User account menu
  • Log in

Breadcrumb

  1. Home

Find and replace string in files recursively

This is the best option I have found so far: find . -type f -exec sed -i 's/foo/bar/g' {} +
foo is replaced with bar.

Source: https://linuxize.com/post/how-to-use-sed-to-find-and-replace-string-in-…

Also quite handy to be able to search and replace filenames.

find . -type f -name 'foo*' | while read FILE ; do
    newfile="$(echo ${FILE} |sed -e 's/foo/bar/')" ;
    mv "${FILE}" "${newfile}" ;
done 

Source: https://unix.stackexchange.com/questions/175135/how-to-rename-multiple-…

 

Also a useful thing is to search for strings within files: grep -rn '/path/to/somewhere/' -e "pattern"

Here for searching for strings in PDF-files: find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' \;

sed Examples

List folders in folder and show each folder on one line. The software sed is piped and will replace all the slash characters with an empty character: ls -p | grep / | sed "s/\///g"

Last modified
2021-09-20

Tags

  • Script
  • Log in to post comments

Review Greg on Google

Greg Nowak Google Reviews

 

  • Portfolio
  • About Greg Nowak
  • Gregs IT and Logistics Optimization
  • Shrink PDF In Linux
  • Running Linux on T450s T495s T14, Thinkpads and Others
RSS feed

GrN.dk web platforms, web optimization, data analysis, data handling and logistics.