How to search string in all files in a folder and sub folder recursivelly

How to use “grep” command in linux or macos to find text inside all files in a folder and including subdirectories

Command for the same is
grep -rl "string to search" /absolute-path/to/folder/

-r (or –recursive) option is used to traverse also all sub-directories of /path
-l (or –files-with-matches) option is used to only print filenames of matching files
-i ( for ignore case sensitive)

If you’re looking for lines matching in files, then use the below command

grep -Hrn "string to search" /absolute-path/to/folder/

-H causes the filename to be printed (implied when multiple files are searched)
-r does a recursive search
-n causes the line number to be printed
/absolute-path/to/folder/ can be . (dot) to search in the current directory

How to create date wise apache error log and access log

By default apache write the access log and error log in the single file. Later it grow in sizes over days and then when you have to debug you find the issue from the apache log, it become difficult to open and read the file. And over a time, the log size bigger so big that, many of the file editor fail to load the file.

Default Logformat
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

So apache come with a inbuilt program called rotatelogs.
First you need to check if rotatelogs is present on your server or not.

To check if rotatelogs is present type the below command
> whereis rotatelogs

The output of the command will be as below
rotatelogs: /usr/bin/rotatelogs /usr/share/man/man8/rotatelogs.8.gz

This “/usr/bin/rotatelogs” indicate that rotatelogs program is present

Now to split your errorlog and accesslog by date, you need to comment the default log format
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined

and write down the below format.

CustomLog “| /usr/bin/rotatelogs -l /var/log/apache2/access.%Y.%m.%d.log 86400” common
ErrorLog “| /usr/bin/rotatelogs -l /var/log/apache2/error.%Y.%m.%d.log 86400”

What this does is…everyday a new error log and access log file will be created like error.2020.04.03.log and access.2020.04.03.log respectively.

To explore more option for the custom logging you can refer the apache project page http://httpd.apache.org/docs/current/programs/rotatelogs.html

export mysql database – mysql dump

When you have access to the command line and you have the export large database, you will not be able to export from phpmyadmin.

Hence mysql provide you handy method to export the database from the command line. Below are various options, you can use while exporting the database.

——databases – This allows you to specify the databases that you want to backup. You can also specify certain tables that you want to backup. If you want to do a full backup of all of the databases, then leave out this option
——add-drop-database – This will insert a DROP DATABASE statement before each CREATE DATABASE statement. This is useful if you need to import the data to an existing MySQL instance where you want to overwrite the existing data. You can also use this to import your backup onto a new MySQL instance, and it will create the databases and tables for you.
——triggers – this will include the triggers for each dumped table
——routines – this will include the stored routines (procedures and functions) from the dumped databases
——events – this will include any events from the dumped databases
——set-gtid-purged=OFF – since I am using replication on this database (it is the master), I like to include this in case I want to create a new slave using the data that I have dumped. This option enables control over global transaction identifiers (GTID) information written to the dump file, by indicating whether to add a SET @@global.gtid_purged statement to the output.
——user – The MySQL user name you want to use
——password – Again, you can add the actual value of the password (ex. ——password=mypassword), but it is less secure than typing in the password manually. This is useful for when you want to put the backup in a script, in cron or in Windows Task Scheduler.
——single-transaction – Since I am using InnoDB tables, I will want to use this option.
——no-create-db – don’t create database
——no-create-info – don’t create table structure
——extended-insert=FALSE => Each row as seperate insert querry

Syntax : mysqldump ——triggers ——routines ——databases databasename ——user=root ——password > database_export_file_name.sql
Example : mysqldump ——no-create-db ——triggers ——extended-insert=FALSE ——routines ——databases mydatabase ——user=root ——password  > mydatabase.sql

Sometime you have to type the absolute path for mysqldump executable path to execute the above command for example.

Example : /var/mysqlFolder/bin/mysqldump ——no-create-db ——triggers ——extended-insert=FALSE ——routines ——databases mydatabase ——user=root ——password  > mydatabase.sql

More option you can explore on Maria DB website

How to change the php version for the console

Many a times when you view the php version in the browser by displaying the phpinfo() information and the php version that is displayed when you see by typing the command “php -v” in the console or command prompt, You will notice that there is difference in the two.

So how  to set the php version same as the version that you see in your phpinfo( ) function in the browser.

Step 1: Find the path to the executable of the php from the phpinfo() displayed in the browser. For example, if you have ampps installed you will find the path as “/Applications/AMPPS/php-7.1/bin/php”. 

Incase you have multiple version of php configured then choose the binary/executable path to the required version of php.

For example for php5, the path would be “/Applications/AMPPS/php5/bin/php”.

Step 2: Setting the php version for the console

Once you have decided upon the required php version for your console application execute the below command.

alias php=’/Applications/AMPPS/php-7.1/bin/php’ => This will set your php version to 7.1 for you console command line instruction.

Similarly for php5

alias php=’/Applications/AMPPS/php5/bin/php’

To make this work,You need to double check the location of the required php executable path, then only it will work. If you don’t have the required version installed. you could installed it as per your need and the operating system you are using.

 

Yii2 how to set custom value for checkbox in gridview widget

In Yii2, while you use gridview widget to display your table data, for performing any operation on the record by selecting the checkbox, gridview assign the value of the first column retrieved from your the result set.

But you need to set some different value to the checkbox, so to do that you can use the below code

GridView::widget([
	'dataProvider' => $dataProvider,
	'filterModel' => $searchModel,
	'columns' => [
		['class' => 'yii\grid\SerialColumn'],
		[
			'class' => '\kartik\grid\CheckboxColumn',
			'checkboxOptions' => function($model, $key, $index, $widget) {
				return ["value" => $model['id']]; // this can be substituted with any column value of your result data
			},
		],
		'name',
		['class' => 'yii\grid\ActionColumn'],
	],
]);

instead of the default one

GridView::widget([
	'dataProvider' => $dataProvider,
	'filterModel' => $searchModel,
	'columns' => [
		['class' => 'yii\grid\SerialColumn'],
		[
			'class' => '\kartik\grid\CheckboxColumn',
			'mergeHeader'=>false
		],
		'name',
		['class' => 'yii\grid\ActionColumn'],
	],
]);

How to create tar archive file in linux

The below command will create a tar archive file whatevername_yougive.tar for a directory /path/of/directory/to/archive in current working directory.

See the example below.

tar cvf whatevername_yougive.tar /path/of/directory/to/archive

The options detail are explained as below
c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – File name type of the archive file.

Similarly to create tar.gz file

tar cvzf whatevername_yougive.tar.gz /path/of/directory/to/archive

To untar or uncompress a tar file check here