-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_replace_in_files
executable file
·44 lines (39 loc) · 1.86 KB
/
find_replace_in_files
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
#!/bin/sh
# ***************************************************************************
# find_replace_in_files.sh
# Performs a recursive, case-sensitive directory find and replace of files
# For case-insensitive, use the -i switch in the grep call
# Uses a "startdirectory" parameter so that you can run it outside of the
# specified directory - else this script will modify itself!
# Changes the Internal Field Separator (IFS) to carriage return to accomodate
# file paths that contain whitespace
# ***************************************************************************
# ********************** Change Variables Here ******************************
startdirectory="/afs/econ.duke.edu/data/grad/tmr17/CollegeMaj/ACS/TylerAnalysis"
searchterm="\/afs\/econ.duke.edu\/data\/grad\/tmr17\/CollegeMaj\/ACS\/Data"
replaceterm="\/afs\/econ.duke.edu\/data\/grad\/tmr17\/CollegeMaj\/ACS\/TylerData"
# ***************************************************************************
# IFS (Internal Field Separator) default is whitespace, which is a
# problem if there are spaces in the file path. Change IFS to carriage
# return instead
IFS_backup=$IFS
IFS=$'\n'
echo "*****************************************"
echo "* Find and Replace in Files Version 0.6 *"
echo "*****************************************"
# get file names and store as loop variable $file
for file in $(grep -l -R $searchterm $startdirectory)
do
# extract modification date and time of file
MODTIME=`stat -c %Y "$file"`
HMODTIME=`date -d @"$MODTIME"`
# make the replacement with sed and update file
sed -i "s/$searchterm/$replaceterm/ig" "$file"
# change the modification date of the file back to the original date
touch -d @$MODTIME "$file"
echo "Modified: " "$file"
echo "Modification date/time: " $HMODTIME "(sec since epoch: "$MODTIME")"
done
# change IFS back to whitespace
IFS=IFS_backup
echo " *** Yay! All Done! *** "