sed command in dry run

How it is possible to make a dry run with sed?

I have this command:

find ./ -type f | xargs sed -i 's/string1/string2/g'

But before I really substitute in all the files, i want to check what it WOULD substitute. Copying the whole directory structure to check is no option!


Remove the -i and pipe it to less to paginate though the results. Alternatively, you can redirect the whole thing to one large file by removing the -i and appending > dryrun.out

I should note that this script of yours will fail miserably with files that contain spaces in their name or other nefarious characters like newlines or whatnot. A better way to do it would be:

while IFS= read -r -d $'\0' file; do
  sed -i 's/string1/string2/g' "$file"
done < <(find ./ -type f -print0)

I would prefer to use the p-option:

find ./ -type f | xargs sed 's/string1/string2/gp'

Could be combined with the --quiet parameter for less verbose output:

find ./ -type f | xargs sed --quiet 's/string1/string2/gp'

From man sed:

p:

Print the current pattern space.

--quiet:

suppress automatic printing of pattern space