I have a I need to replace all instances of the word mysite.com to maysite.net within the public_html web folder. How can I accomplish this.
dannyd, try this:
for FileName in `ls -1`
do
sed 's/mysite.com/mysite.net/g' $FileName > $$TempFile
mv $$TempFile $FileName
done
Would this replace the keyword mysite.com to mysite.net in all subfolders as well ?
No. You didn't specify it in your request.
To list all files in your directory and sub-directories, replace
`ls -1` by `find .`
Sorry I dont think i was clear in my question. I would like to replace all instances of the word mysite.com to mysite.net inside every file in the entire website.
I think your solution searches for files with the name mysite.com and renames them to mysite.net.
No dannyd,
My solution with the "find" loops thru every single file in the current directory
and sub-directories and for each file found, it searches and replaces the string
"mysite.com" to "mysite.net".
Sorry im not too experienced in unix. 
Can this command be done on the command line or do i need to put this in a shell script ?
No. You didn't specify it in your request.
Danny,
I dont think we may require `ls -1` in the for loop in case we are listing the files/directories in the Current working directory just an * will do.
Also note `find .` or `ls -1` or simple * will list all files & directories,so we may to have use,
Please correct me if am wrong.
Thanks
Nagarajan Ganesan
So I guess this would be the final solution ?
for file in $(find . -name '*' -type f -print ) ; do
sed 's/mysite.com/mysite.net/g' $FileName > $$TempFile
mv $$TempFile $FileName
done
If i want to run this command do I type this out on the command line or do i have to make a shell script and run the shell script ?
Thank you ennstate,
You are right, the "find ." will list everything, including the directories.
Also, there is no need for "-name '*'" nor "-print".
The "find . -type -f" is sufficient to list all regular files and not directories.
Thanks.
Hi,
Do I run this on the command line or do I have to run this command in a script ?
dannyd,
The best way to run this is:
1) Create a file with the above commands.
2) Change its permissions to executable (ie 755, etc.).
3) Type the file name in the command line.
no need the for loop
find . -size +1024c -type f -name "*.txt" -print0 | xargs -0 sed -i 's/term/replace/g'
NB. -i option only for gnu sed.
I can run this on the command line, but...
Wouldnt this search only files that end with .txt and would it search subfolders ?
Do you think this would work:
find . -size +1024c -type f -name "*" -print0 | xargs -0 sed -i 's/mysite.com/mysite.net/g'
get rid of the -name and the "*", you don't need either.
find . -size +1024c -type f -print0 | xargs -0 sed -i 's/mysite.com/mysite.net/g'
so this is the final product of trying to replace all instances of the keyword mysite.com to mysite.net in every file in the website ?
Why is it necessary to use the -size ?
no, it's not required.
If you remove it it will make the replaement in very file, if not only files > 1k in size.
its just an example i tried because i don't have 5M file, so i just used some size that i have.. you can fine tune your 'find' expression as you like.