Unix: Replace text in multiple files and subfolders

I have  thousands of HTML pages that need to be updated – I just have to swap a couple of lines. I was wondering how to do it all at once and in subdirectories as well.

Here’s what worked:

for mFile in `find . ! -type d -name ‘*.html’` do
sed -i ‘s/text1/replacement1/g’  $mFile

sed -i ‘s/text2/replacement2/g’  $mFile

sed -i ‘s/text3/replacement3/g’  $mFile
done

Advertisements

Batch update custom fields in WordPress

If most of the pages have the same custom field values and need to be updated, going through each page manually would take long time.

This can be done by running an update query directly to the database. This should be done carefully, of course.

Steps:

1) Make a backup of the database.

2) Custom fields are stored in the “wp_postmeta” table and marked with the meta keys. We need the field called “facts”, so we’ll be looking for that key.

3) Select pages that will need to have facts changed. Query:

SELECT *
FROM `wp_postmeta`
WHERE meta_key = ‘facts’
AND post_id NOT
IN ( 10, 643, 316, 532)

Note that “post_id not in” clause will exclude those posts/pages, and the custom fields won’t be updated for those pages.

4) Update query:

update `wp_postmeta` set meta_value = ‘new_meta_value_goes_here’ WHERE meta_key = ‘facts’
and post_id not in (10,643,316,532)

5) Check that everything worked fine.