Skip to content

Instantly share code, notes, and snippets.

@flangofas
Last active August 29, 2015 14:02
Show Gist options
  • Save flangofas/dcbb0cdc3bc4d795c1c1 to your computer and use it in GitHub Desktop.
Save flangofas/dcbb0cdc3bc4d795c1c1 to your computer and use it in GitHub Desktop.
Find replace bash executable PHP script
#!/usr/bin/php
<?php
/**
* Find replace tool
* Place it in the executable folder "/usr/local/sbin/"
* Also, add an alias in the ~/.bashrc
* Option should be -e for execute
* Otherwise view the files only that contain the search term
* Author: Antonis Flangofas
* Date: 01/06/2014
*/
if ($argc < 5) {
die("Not enough arguments\n");
}
$cmd = '';
$flag = $argv[1];
$find = addslashes($argv[2]);
$replace = addslashes($argv[3]);
$dir = $argv[4];
if (!file_exists($dir)) {
die("Wrong directory\n");
}
if ($flag === '-e') {
$cmd = "grep -rl $find $dir | xargs sed -i 's/$find/$replace/'";
} else {
$cmd = "grep -rl $find $dir";
}
exec($cmd, $output, $return_var);
if ($return_var != 0) {
die("Error with your command $cmd\n");
}
if (!empty($output)) {
array_walk($output, function($line) { echo "$line \n"; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment