Knowledgebase

need a linux shell command for find and replace

Posted by takserver, 06-15-2015, 07:26 AM
I need a linux shell command for search all files included in /home/ directory for "X.X.X.X" value and change to "Y.Y.Y.Y"

Posted by Andei, 06-15-2015, 07:35 AM
sed -i -- 's/X.X.X.X/Y.Y.Y.Y/g' /home/* OR a simple script: Last edited by Andei; 06-15-2015 at 07:38 AM.

Posted by takserver, 06-15-2015, 07:44 AM
Thank you very much. i have following error: sed: couldn't edit /home/2: not a regular file

Posted by diman, 06-15-2015, 10:38 AM
My advice - find /home/* -type f | head -n1 | xargs sed -i 's/X.X.X.X/Y.Y.Y.Y/g'; Seems to be an error was called when sed had tried to "fix" directory name.

Posted by Srv24x7, 06-16-2015, 10:43 AM
Hi, you can use this simple one to change the word to new one. find /home -type f -exec sed -i 's/x.x.x.x/y.y.y.y/g' {} \;

Posted by SneakySysadmin, 06-16-2015, 07:16 PM
Call me paranoid but executing shell commands on '/home/*' just makes me cringe. Use 'find', and make sure that 'find' is finding what you want to change. Let's say you just want to change all the PHP files: Repeat as needed for other file types like .htm, html etc.

Posted by kevincheri, 06-16-2015, 08:33 PM
Recommend this..

Posted by JustinAY, 06-17-2015, 12:06 PM
The solutions suggested above will work. One thing another poster said -- you may want to ensure you want to replace *everything*. If you run the above find command, you can remove the -exec bit and you will get a list of all files first. You can skim through these and once ensured you want to run the sed on everything, then add back the -exec part. If you find a file or three you don't want to change, you can `chattr +i filename`, and then run the above find command. Then, you can `chattr -i filename` the file(s) you didn't want changed. This way you can still use find to do a mass change without effecting the files you want to remain the same.

Posted by servermates, 06-23-2015, 05:13 AM
Try this find /home -type f -exec sed -i 's/x.x.x.x/y.y.y.y/g' {} \;



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
TOFSWebHosting.com (Views: 547)


Language:

Client Login

Email

Password

Remember Me

Search