sonesay 7 Report post Posted August 28, 2009 I have a problem that I would like to put forth. Suppose you have a large collection of source files for example if you did PHP and your source files had passwords saved in a db.php file. Obviously you would want to protect them from being viewed by others you do not trust. Heres where the problem lies, you could have multiple sites and they all contain their very own db.php files with the passwords in there but you need to share a copy with others for what ever reason. It would be very tedious to copy a folder containing all the files and go through echo one and removing or renaming the password text. Is there a way to do this with a shell script so that you can recursively scan through all files within a folder and replace the password text with a dummy text so that you can safely give to others for viewing purposes? I'm guessing there is a way and I am wondering if anyone has done something like this before or can point it out to me. I could of course go hunting on the web but I want to save time and be guided if possible by experienced shell script writers.Looking forward to any help cheers. Share this post Link to post Share on other sites
rvalkass 5 Report post Posted August 28, 2009 You can get this done with sed. If you want to replace password with dummytext in the file db.php just run the following command: CONSOLE rob@rob-laptop:~$ sed -i 's/password/dummytext/g' db.php To repeat that for all files in a directory: CONSOLE rob@rob-laptop:~$ sed -i 's/password/dummytext/g' /path/to/folder/* If the files are nested in many directories then that makes things slightly trickier, but something like this should work: #!/bin/bashcd /path/to/directory/containing/filesfor i in $( find . -type f )do sed -i 's/password/dummytext/g' $idone Share this post Link to post Share on other sites
sonesay 7 Report post Posted August 28, 2009 Thanks for the response rvalkass, The first example appears to work it echos out: BP:lsms_cp sone$ sed -l 's/password/dummytext/g' db.php<?php/* * Author: Sone Inthavong * Created: 2007 beginning * Purpose: Store database connection * * Updated: * 13/10/2008 - reformated code and updated * **/$p = $_POST;$g = $_GET;$s = $_SESSION;// set database variables$db_host = '127.0.0.1';$db_user = 'sonesayi_admin';$db_pass = 'dummytext';$db_name = 'sonesayi_lsms';// setup link connection$link = mysql_connect($db_host, $db_user, $db_pass) or die("Could not connect to Database > ". $db_name);// select Databasemysql_select_db($db_name,$link) or die("Could not find Database > ". $db_name);?>MBP:lsms_cp sone$ It clearly echos out the dummytext in place of password but when i go and open the file or cat it still shows the old original password unchanged. Is there something else missing?The second example I have yet to try. Share this post Link to post Share on other sites
rvalkass 5 Report post Posted August 29, 2009 It should be sed -i not sed -l The i represents in-line replacement, so will replace the text in the file and save over the top of the old version. Otherwise it echos out the result so you can save it as a new file, and preserve the original. Share this post Link to post Share on other sites