I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified.

This is how I am finding and then using sed:

find . -type f -a \( -name "*.txt" -o -name "*.git"\) -a -exec sed -i -e "s/"str1"/"str2"/g" {} +

How do I print the file name of the changed files? I would like to print it in a sorted order so it's easier to read.

When using only sed we can do this:

sed -i 's/$pattern/$new_pattern/w changelog.txt' $filename
if [ -s changelog.txt ]; then

But how do I do this when using find and sed together? I checked the man page and tried a bunch of stuff but nothing worked.

  • I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see. – EightBitTony Jan 21 '16 at 14:49
  • @EightBitTony I have added examples, please take a look – KLMM Jan 21 '16 at 17:05
  • 1
    You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f \( -name \*.git -o -name \*.txt \) -exec sed -i 'blah_blah' {} \; -print. Sure, you'll have to sort the output then. – don_crissti Jan 21 '16 at 18:11
  • @don_crissti using print giving an error "-print: command not found". – KLMM Jan 21 '16 at 18:34
  • 1
    Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 {} \; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here) – don_crissti Jan 23 '16 at 2:45

sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.

Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:

find . -type f \( -name "*.txt" -o -name "*.git" \) -size +3c \
  -exec grep -lZ str1 {} + |
  while IFS= read -rd '' file; do
    sed -i 's/str1/str2/g' "$file" &&
      printf '%s\n' "$file"

That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).

Or you can run one grep and sed per file:

find . -type f \( -name "*.txt" -o -name "*.git" \) \( -size +3c \
  -exec grep -q str1 {} \; \
  -exec sed -i 's/str1/str2/g' {} \; \
  -printf '"%p" was modified\n' \
    -o -printf '"%p" was not modified\n"' \)
| improve this answer | |

It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form

sed -i 's/$pattern/$new_pattern/' $1
echo $1 >> changelog

Let us call this script ed_notify. Now, you can run it on selected files by

cat changelog >> changelog.old
rm changelog
find . -type f -a \( -name "*.txt" -o -name "*.git"\) -a -exec ed_notify {} \;
| improve this answer | |
  • 2
    Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion. – terdon Jan 21 '16 at 15:06
  • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks – KLMM Jan 21 '16 at 17:06
  • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order? – KLMM Jan 21 '16 at 20:31
  • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command. – unxnut Jan 21 '16 at 21:11

Your sed command (with proper quoting):

sed 's/str1/str2/g'

This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':

find . -type f \( -name '*.txt' -o -name '*.git' \) \
    -exec grep -l 'str1' {} \; \
    -exec sed -i 's/str1/str2/g' {} + >changelist.txt

Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).

Alternatively, let find print the pathnames of the files that contain the string:

find . -type f \( -name '*.txt' -o -name '*.git' \) \
    -exec grep -q 'str1' {} \; \
    -print \
    -exec sed -i 's/str1/str2/g' {} + >changelist.txt


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.