<\/span><\/h2>\nSearch for lines containing ‘DB_USER’ in WordPress configuration file (wp-config.php):<\/p>\n
# grep 'DB_USER' wp-config.php\r\ndefine('DB_USER', 'wpuser');<\/pre>\n<\/span>2. Grep Command Example on how to search for specific text in all files<\/span><\/h2>\nSearch for all PHP files containing ‘str_replace’ text inside the ‘wp-admin’ directory of a WordPress installation:<\/p>\n
# # grep \"str_replace\" admin*.php\r\nadmin-ajax.php: add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );\r\nadmin-ajax.php: add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );\r\nadmin-header.php:$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );\r\nadmin-header.php:$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '\/^([.0-9]+).*\/', '$1', $wp_version ) );\r\nadmin-header.php:$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );\r\n<\/pre>\n<\/span>4. Grep Command Example on how to add\u00a0line numbers to search results<\/span><\/h2>\nIf you want to add line numbers to search results, use the following command:<\/p>\n
# grep -n \"str_replace\" admin*.php\r\nadmin-ajax.php:73: add_action( 'wp_ajax_' . $_GET['action'], 'wp_ajax_' . str_replace( '-', '_', $_GET['action'] ), 1 );\r\nadmin-ajax.php:76: add_action( 'wp_ajax_' . $_POST['action'], 'wp_ajax_' . str_replace( '-', '_', $_POST['action'] ), 1 );\r\nadmin-header.php:157:$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );\r\nadmin-header.php:158:$admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '\/^([.0-9]+).*\/', '$1', $wp_version ) );\r\nadmin-header.php:160:$admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );<\/pre>\n<\/span>5. Grep Command Example on how to search in all files for specific text\u00a0recursively<\/span><\/h2>\nSearch for all PHP files whose contents mention ‘str_replace’ text which has either capital or small letters in it, located in the ‘wp-admin’ directory recursively (i.e. read all files under each directory) and lists the file names only:<\/p>\n
# grep -ril \"str_replace\" admin*.php\r\nadmin-ajax.php\r\nadmin-header.php<\/pre>\ngrep command is often used in a Unix\/Linux pipeline with other commands. Listed below are some more advanced examples of how to use the grep command on Linux:<\/span><\/p>\n<\/span>6. Grep Command Example on how to search for lines in the current directory<\/span><\/h2>\nSearch for lines containing ‘eval’ in all PHP files located in the current directory:<\/p>\n
#find . -name '*.php' -exec grep -l 'eval' {} \\;\r\n.\/wp-admin\/includes\/image.php\r\n.\/wp-admin\/includes\/class-wp-upgrader.php\r\n.\/wp-admin\/includes\/class-pclzip.php\r\n.\/wp-admin\/includes\/media.php\r\n.\/wp-admin\/includes\/update-core.php\r\n.\/wp-admin\/includes\/ajax-actions.php\r\n.\/wp-admin\/includes\/class-wp-automatic-updater.php\r\n.\/wp-admin\/includes\/class-wp-posts-list-table.php\r\n.\/wp-admin\/user-edit.php\r\n.\/wp-admin\/edit-tag-form.php<\/pre>\n