find & sed
The find and sed commands are powerful tools in Linux for searching and manipulating text.
Here’s an overview related to how I used them both to rename specific words in multiple files. Basically, I wanted to change all instances of “loose” to “relaxed” in text files within a project.
find + sed Pattern
Section titled “find + sed Pattern”find [path] [filters] -exec sed -i '' 's/old/new/g' {} +Components
Section titled “Components”find: Locate files matching criteria
-name "*.ext": Filter by filename pattern-type f: Only regular files (not directories)-exec cmd {} +: Run command on all found files
sed: Stream editor for text replacement
-i '': Edit in-place (macOS syntax)- On Linux, use
-iwithout quotes
- On Linux, use
s/pattern/replacement/g: substitute globally\b: Word boundary (prevents partial matches)
{}: Placeholder for filenames found by find
+: Batch multiple files (efficient) vs ; (one at a time)
Patterns
Section titled “Patterns”Word Boundaries
Section titled “Word Boundaries”# WITHOUT \b: "loosely" → "relaxedly" (wrong!)sed 's/loose/relaxed/g'
# WITH \b: Only "loose" → "relaxed" (correct!)sed 's/\bloose\b/relaxed/g'Case Sensitivity
Section titled “Case Sensitivity”# Separate patterns for different casessed 's/\bLoose mode\b/Relaxed mode/g' # Capital Lsed 's/\bloose mode\b/relaxed mode/g' # Lowercase lDelimiters
Section titled “Delimiters”# When pattern contains /, use different delimitersed 's|path/to/loose|path/to/relaxed|g' # Using | instead of /