-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplrecr
executable file
·50 lines (40 loc) · 1.12 KB
/
replrecr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
#
# File:
# replrecr
#
# Description:
# Replace all occurrences of a string recursively.
#
# Supplemental feature:
# This solution allows a backup directory of the directories in which it is
# run to be created.
#
# Usage:
# replrecr <text_to_replace> <replacement_text>
#
# Notes:
# All characters in both arguments are accepted as literals.
#
# ======= CONFIGURATIONS ==============
# Backup the directory in which this script is run.
readonly BACKUP=true
# Directory where backup directories are created
readonly BACKUPS_DIR="${HOME}/.unixfoundation/shell/replrecr-backups/"
# ======= ! CONFIGURATIONS ==============
if [ "$#" -ne 2 ]; then
echo 'replrecr: invalid number of arguments' 1>&2
exit 1
elif [ -z "${1}" ]; then
echo 'replrecr: first argument empty' 1>&2
exit 1
fi
if [ "${BACKUP}" != 'false' ]; then
if [ ! -d "${BACKUPS_DIR}" ]; then
mkdir -p "${BACKUPS_DIR}"
fi
cp -R "$PWD" "${BACKUPS_DIR}"
fi
ARGS[0]="$(echo "${1}" | sed 's/[]\/$*.^|[]/\\&/g')"
ARGS[1]="$(echo "${2}" | sed 's/[\/&]/\\&/g')"
find . -type f -print0 | xargs -0 sed -i "s/${ARGS[0]}/${ARGS[1]}/g"