Repository navigation
Pattern Matching
Mikhail Deynekin edited this page Oct 12, 2026
·
2 revisions
Historical page (sr 6.x). It documents the Bash implementation, which sr 7 replaced with a Python rewrite and a different command line. Nothing below applies to 7.x; the page is kept for reference. Current documentation: Home, Installation, Quick-Start and
sr help(27 topics). The text below is unchanged.
Comprehensive guide to file pattern matching in sr.
sr uses shell glob patterns to match files for processing. Understanding these patterns is essential for effective use.
| Pattern | Description | Example |
|---|---|---|
* |
Matches any characters in filename |
*.txt matches all .txt files |
** |
Matches any files recursively |
**/*.js matches all .js files in any subdirectory |
? |
Matches any single character |
file?.txt matches file1.txt, fileA.txt |
{a,b} |
Matches either a or b |
*.{js,ts} matches .js and .ts files |
[abc] |
Matches any character in set |
file[123].txt matches file1.txt, file2.txt, file3.txt |
# Single extension
sr "*.txt" "search" "replace"
# Multiple extensions (brace expansion)
sr "*.{js,ts,jsx,tsx}" "search" "replace"
# Specific prefix and extension
sr "config*.yml" "search" "replace"# All .js files recursively
sr "**/*.js" "search" "replace"
# All files in src directory
sr "src/**/*" "search" "replace"
# All config files recursively
sr "**/config/*.{yml,yaml,json}" "search" "replace"# Only current directory (use -nr flag)
sr -nr "*.txt" "search" "replace"
# Specific directory depth
sr -md 2 "**/*.conf" "search" "replace"sr supports three distinct argument processing modes:
sr "PATTERN" "SEARCH" "REPLACE"
# Example:
sr "*.js" "function old" "function new"# Uses default pattern: *.*
sr "SEARCH" "REPLACE"
# Example:
sr "localhost" "production-host"# Process specific files (not patterns)
sr file1.txt file2.txt "SEARCH" "REPLACE"
# Example:
sr index.html about.html "Copyright 2020" "Copyright 2025"# Exclude specific directories
sr -xd node_modules,dist,vendor "**/*.js" "search" "replace"
# Exclude specific file patterns
sr -xp "*.min.js" "**/*.js" "search" "replace"
# Exclude hidden files
sr -xh "**/*" "search" "replace"
# Limit file size (skip large files)
sr -xs 10 "*.log" "search" "replace" # Skip files > 10MB# Limit recursion depth
sr -md 3 "**/*.js" "search" "replace" # Max 3 levels deep
# Non-recursive (current directory only)
sr -nr "*.txt" "search" "replace"-
Always quote patterns: Use
sr "*.txt"notsr *.txtto prevent shell expansion - Patterns are processed by sr using
find, not the shell - Backup directories (
sr.backup.*) are automatically excluded - Hidden directories (
.git,.svn) are excluded by default
- Regular Expressions - Advanced pattern matching with regex
- Command Reference - Complete option listing
- Basic Examples - Practical examples