verbose ·

verbose · work · email ·

That time 30 seconds, StackOverflow, and sed saved me 30 minutes

Sat May 06, 2017 · 309 words

This week, I launched my new website and changed my Twitter username to match. I was about to spend time going through all my old blog posts to find and update the URLs when this very future blog post popped up on my screen like Clippy and shook its pixelated head disapprovingly.

Here’s a worthwhile new habit for you: anytime you find yourself going “Ughhh I have to do that? It’ll take forever!” head on over to

Sparkly StackOverflow, Savior of Devs

and search for “terminal command [the thing you’re trying to do]”.

Voila! I’ve just saved you x amount of time. (You’ll just spend it browsing gifs on Twitter - why do I bother?!)

Update dozens of previous blog posts in milliseconds with sed

Sed is your friend. This amazingly powerful tool lives in your terminal and is available to be totally underused for things like finding and replacing strings in files. (I seem to have a habit of suggesting ways to totally underuse powerful tools - I previously outlined how to use cron to create desktop notifications, but I digress.)

Run this command to search all the files in your current directory and replace a given string.

// to replace 'foo' with 'bar'
$ sed -i -- 's/foo/bar/g' *

The above is non-recursive, meaning it won’t look in subdirectories. There are ways to do this recursively, as well as limit the search to specific file extensions: see the full answer on StackOverflow.

Break it down:

-i will change the original, and stands for “in-place.”
s is for substitute, so we can find and replace.
foo is the string we’ll be taking away,
bar is the string we’ll use instead today.
g as in “global” means “all occurrences, please.”
* denotes all file types. (No more rhymes. What a tease.)

Here are some more examples for using sed that you may find handy.

Like this post? Tweet me about it!