WP CLI: search and replace specific directory name of URL

Instead of using " to wrap your expressions, use '.

Further experimenting shows me that this is incorrect—both " and ' should work, as long as they’re matched (ie, you don’t accidentally try something like wp search-replace "domain.com/wp-content/uploads/(\d)/(\d)/(\d*)/' "domain.com/wp-content/uploads/\$1/\$2/" --precise --all-tables --skip-columns=guid --regex. (Note that the regex in that example starts with " and ends with '.)

I’m not deleting this answer because the info below, about the regex itself, still stands.

The regex

Your regex looks wrong to me. \d will match a single digit, so it’d match, eg, 1, 5, etc, but not 2020 or 04.

I’d recommend something like this:

wp search-replace
--dry-run
'domain.com/wp-content/uploads/(\d+)/(\d+)/[^/]+/'
'domain.com/wp-content/uploads/\$1/\$2/'
--precise --all-tables --skip-columns=guid --regex

(line breaks added for readability; all this should be on a single line)

The [^/]+ will match everything but a / character in that last segment of the regex search. I also took out the () wrapping it since you’re discarding it.

I also strongly recommend using the --dry-run argument with wp search-replace first, to see what you’ll be replacing.

Edited again to add: I’ve removed the text about my deeper dive into using ' vs. " in bash, because it seems it was incorrect.