back       next

Search and Edit Files with Certain Text

Here's a quick way to edit all files whose contents contain a given string:

files=""; for i in `find . -type f -print`; do files="$files `grep -l <text> $i`"; done; vi $files
  1. For all file names in the current directory, including those in subdirectories, assign each in turn to the variable i.
    for i in `find . -type f -print`;
  2. For each of the files we found, run the grep command to search the file provided by the $i variable.
    do files="$files `grep -l <string> $i`";
  3. Edit the found files with the vi editor.

    vi $files

    Move back and forth between files using:

    :n    -- move to the next file
    :rew  -- move back 
    :e #3 -- edit the third file in buffer
    :ar   -- list files status
    

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.