Is there any way to search for a text string e.g Hello and replace it with e.g Hi in all the text files in a directory structure with MANY sub directories? I'm running Ubuntu 17.04 x64 server.
Asked
Active
Viewed 3,832 times
-1
Zanna
- 72,312
W.Scott7182
- 1
- 1
- 3
2 Answers
3
You have to ssh into the remote machine and run the command described below.
The following command will replace all occurrences of "Hello" in all files inside a specific location (as per your choice) with "Hi".
find /path/to/main/parent/directory -type f -exec sed -i 's/hello/hi/gI' {} \;
Note
The above is case insensitive, if you want case sensitive replacement you can try with removing I, ie. 's/Hello/Hi/g'
/path/to/main/parent/directory : You must specify the parent directory from which your file containing string "Hello" starts.
for URLs
From the comment I came to know that you want to replace a URL with other which contains :// So please use the following method to replace strings which contains URL.
find /path/to/main/parent/directory -type f -exec sed -i 's,/URL1/,/URL2/,gI' {} \;
1
Just another option using grep -Rl instead of find:
grep -Rl 'hello' /path/to/main/parent/directory | xargs -n1 sed -i 's|hello|hi|g'
pLumo
- 27,991