How to change date and time in linux? date-time

Today I wanted to change date and time on one of my server quickly from command line. I am aware with command line options but every time I want to do it, I need to read man page or help to get perfect combination so i decided to write this small how to which can help me and you every time we want to change date and time in linux from command line.

Remember, CLI (command line interface) is most powerful for users / administrators like us.
To change system date in linux, type:-


For example: i want to change my system date to Dec 25 2009, 5.30pm, I will type:-
#date 122517302009.00

It’s simple, Isn’t it ?

MM – month
DD – day
YYYY – year
hh – hour is based on 24 hour
mm – minutes
ss – seconds

Hope this will help you.

How to disable users in linux/unix?

This how-to will show how to disable a user account under linux. This might be useful in the situation where you don’t want to permanently remove the user, but you just want it disabled and no longer able to use the system. The user will still receive emails for example, but he will not be able to login and check them out.

In latest linux systems /etc/shadow stores the encrypted user passwords. The quickest way to disable a user is to alter is password stored in /etc/shadow. Normally an active user account will have one line in /etc/shadow that will look like:


where the second field is the encrypted password. Note: Fields seperated by :

If we replace the password with “*” or “!” this will make the account unusable, and that means the user will not able to login on system any more:


This method has the disadvantage that the user password will be lost (unless saved somewhere, etc.) in the case we will want to re-enable it again later. From this point of view a much better method is to use the passwd command to lock the account:

passwd -l

and the output of the successful change will be “Password changed.”. This actually just changes the shadow file and adds “!” in front of the user password:


Of course we could do this manually ourselves also if we want but its good to do through commands. There is a chance of human error if you try and edit shadow file by yourself.

If you will ever need to re-enable the account just unlock it:

passwd -u

or just remove manually the “!” character from the user’s password line in /etc/shadow.

Of course if you don’t need all this stuff and you just want to permanently remove the user just run:


this will keep user’s old files (home directory, mails, etc.) or to delete all his files on the system:

userdel -r

just be careful what is the home of the user before running this command as personally I have seen someone do this and erasing all the system… the user had set as home “/” .

Hope this helps.

How to change MySQL root password? mySQL-server

How do I change MySQL root password under Linux, Debian, centos, fedora, suse, mandrake, linux-mint, Ubuntu, FreeBSD, OpenBSD and UNIX like operating system over ssh / telnet session?
Setting up mysql password is one of the essential tasks. By default root user is MySQL admin account. Please note that the Linux / UNIX login root account for your operating system and MySQL root are different. They are separate and nothing to do with each other (indeed some admin removes root account and setup admin as mysql super user).

mysqladmin command to change root password

If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To setup root password for first time, use mysqladmin command at shell prompt as follows:

$ mysqladmin -u root password NEWPASSWORD

However, if you want to change (or update) a root password, then you need to use following command

$ mysqladmin -u root -p’oldpassword’ password newpass

For example, If old password is abc, and set new password to 123456, enter:

$ mysqladmin -u root -p’abc’ password ’123456′

Change MySQL password for other user
To change a normal user password you need to type (let us assume you would like to change password for nilesh):

$ mysqladmin -u nilesh -p oldpassword password newpass

Changing MySQL root user password using MySQL sql command
This is another method. MySQL stores username and passwords in user table inside MySQL database. You can directly update password using the following method to update or change password for user nilesh:

1) Login to mysql server, type following command at shell prompt:

$ mysql -u root -p

2) Use mysql database (type command at mysql> prompt):

mysql> use mysql;

3) Change password for user nilesh:

mysql> update user set password=PASSWORD("NEWPASSWORD") where User='nilesh';

4) Reload privileges:

mysql> flush privileges;
mysql> quit

This method you need to use while using PHP or Perl scripting.

How to connect to MySQL Server From Shell Prompt? (CLI – Command Line Interface)

How do I access (connect to) MySQL server from the shell prompt (command line)?

MySQL software includes mysql client. It is a text-based client for mysqld, a SQL-based relational database server. It works interactive and non-interactive mode.

mysql Client Syntax:

mysql -u {mysql-user} -p {mysql-password} -h {mysql-server}


  • -u {mysql-user} : Specify MySQL user name. Use root only when connecting to local system.
  • -p {mysql-password}: Specify password, Employ the specified password when connecting to the database server. If a password is not supplied, it will be requested interactively.
  • -h {mysql-server}: Connect to the specified host (remote or local)

For example remote connect to MySQL server called and user vivek:

$ mysql -u nilesh -h mysql101 -p

Sample outputs:

Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


You can type an sql statement at mysql> prompt. In this example, you will list tables from the demo database, run;

USE demo;

Sample Session:

mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31855130
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use linux;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
| Tables_in_linux |
| linux1          |
| linux1_meta     |
2 rows in set (0.00 sec)

mysql> \q

After typing an SQL statement, end it with ” ; ” (semicolon) and press [Enter] key.

To exit type quit or \q:




Sample session:

mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31853999
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> quit

Batch Mode:

You can execute SQL statements in a script file (batch file) as follows:

mysql database_name < input.script.sql > output.file
mysql -u user -p'password' database_name < input.script.sql > output.file

Recommended Reading:

Type the following command to mysql command man page:

man mysql

what is connection string in MySQL?
How to connect to mysql-server using username and password?
free opensoure RDBMS MySQL?
how to get help on mysql database administration?
connect to mysql-server using mysql-client remotely?

How to Increase max_connections in mysql without restarting mysqld service? global-variable

By default in mysql database server max_connections is set to 100. This value indicates how many maximum concurrent connections mysql server can handle. If mysql reaches to it maximum (max) limit then you can see errors like “too many connections“. I assume that you have enough hardware resources (Mainly RAM )to handle more connections, here with this article I will share a TIP to increase max_connections in mysql.

As we know my.cnf is default configuration file for mysqld service and by default it is located in /etc directory unless and until you have changed it.

To find out how many max_connections are allowed currently on your mysql server use following command from mysql prompt.

mysql> select @@max_connections;
| @@max_connections |
| 100                |
1 row in set (0.00 sec)

max_connections is a GLOBAL variable. we can increase it on the fly without restarting mysqld service.
To do so use following command.

mysql> set global max_connections = 200;
Query OK, 0 rows affected (0.00 sec)

Now, If you check again you will see that limit of max_connections is increased.

mysql> select @@max_connections;
| @@max_connections |
| 200               |
1 row in set (0.00 sec)

It is important that you edit your /etc/my.cnf to add max_connections = 200 otherwise when you restart mysqld service in future, It will complain again after it hits the old max_connections limit.
Mysql server is throwing “too many connections” error ?
How to increase number of concurrent mysql connections without restarting mysqld service?
errrr… mysql-server not allowing any more connections ?
Quick how to on increasing max_connections in mysql. restart not required?


How to recover mysql root password?

Don’t worry if you have forgotten / lost your mysql-server root (administrator) password.
You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.
Step # 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password. you can add this option in /etc/my.cnf.
Step # 3: Connect to mysql server as the root user.
Step # 4: Setup new root password.Step # 5: Exit and restart MySQL server.

Here are commands you need to type for each step (login as the root user):

Step # 1 :

Stop mysql service
# /etc/init.d/mysql stop


Stopping MySQL database server: mysqld.

Step # 2:

Start to MySQL server w/o password:
# mysqld_safe --skip-grant-tables &

Edit /etc/my.cnf and add skip-grant-tables

# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).


[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step # 3: Connect to mysql server using mysql client:

# mysql -u root


Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;

mysql> quit

Step # 5:

Stop MySQL Server:
# /etc/init.d/mysql stop


Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/
mysqld_safe[6186]: ended

[1]+ Done mysqld_safe --skip-grant-tables

Step # 6: Start MySQL server. Remove skip-grant-tables from /etc/my.cnf if you have added and test it

# /etc/init.d/mysql start
# mysql -u root -p

Errr. Help me, I forgot root password for mysql-server.
Quick how-to to recover / reset mysql-server root password.
Easy steps to recover / reset mysql root password.
Damn, Forgot mysql root / administrator password. can you help ?
How to reset mysql root password ?
How do I reset-recover MySQL root password under Linux, Debian, centos, fedora, suse, mandrake, linux-mint, Ubuntu, FreeBSD, OpenBSD and UNIX like operating system over ssh / telnet session?

How to remove/purge binary logs in mysql? binlogs

By default, MySQL 5.x and above enables MySQL Binary Log. Keeping MySQL Binary Log will take up a lot of disk space for long run. Older MySQL Binary log can be removed in order to keep your hard disk space free.

MySQL Binary Log stores query event such as add, delete and update in a very details way. The Binary Log is used for two main purposes;

* 1. Replication between master and slave server, statement that has been made on Master server will later send it to slave server.
* 2. Recovery, certain recovery job required data stored in MySQL Binary Log.

MySQL Binary Log is located at the database’s root folder with the naming convention of mysql-bin.. Sample of the binary files as shown below;

Can I Remove MySQL Binary Log ?

Yes, as long as the data is replicated to Slave server, it’s safe to remove the file. It’s recommend only remove MySQL Binary Log older than 1 month.
Besides, if Recovery of data is the main concern, it’s recommend to archive MySQL Binary Log.
There are several ways to remove or clean up MySQL Binary Log, it’s not recommend to clean up the file manually, manually means running the remove command.
Remove MySQL Binary Log with RESET MASTER Statement
Reset Master statement is used for new database start up during replication for Master and Slave server. This statement can be used to remove all Binary Log.
To clean up Binary Log on Master Server

shell> mysql -u username -p

To clean up Binary Log on Slave Server

mysql -u username -p

Remove MySQL Binary Log with PURGE BINARY LOGS Statement
PURGE BINARY LOGS statement can remove Binary Log based on date or up to a Binary Log sequence number
Base on the binary logs example shown above, I would like to remove binary up to mysql-bin.000015

shell> mysql -u username -p
mysql>PURGE BINARY LOGS TO 'mysql-bin.000015';

Alternatively, you can remove the binary older than a specific date.

shell> mysql -u username -p
mysql> PURGE BINARY LOGS BEFORE '2009-11-01 00:00:00';

Remove MySQL Binary Log with mysqladmin flush-logs Command
Another method is running mysqladmin flush-logs command, it will remove binary logs more than 3 days old.

shell> mysqladmin -u username -p flush-logs

Keep MySQL Binary Log for X Days

All of the methods above required monitoring on disk usage, to “rotate” and keep the binary logs for x number of day. The option below can be configured on MySQL’s config file, my.cnf

expire_logs_days = 7

Consider turning off MySQL Binary Log if MySQL Replication is not deploy on the database server and recovery is not the main concern.

I have additional 15GB if disk space now
Free disk space by purging mysql binary logs.
Are old mysql binary logs important ?
Can I remove old binary logs on mysql server?
Why my mysql server is creating these many binary logs ? How to remove them ?

How to find File-Directory creation date ?

$stat <file name> command basically displays file or file-system status. stat command is used to find Access , Modify, Change date and time for any file or directory in Unix like operating systems. e.g

$stat /root/install.log
File: `/root/install.log'
Size: 17154           Blocks: 48         IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 1177346     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-09-30 16:24:39.000000000 +0100
Modify: 2011-09-30 16:28:59.000000000 +0100
Change: 2011-09-30 16:29:04.000000000 +0100

As show in above example, It also displays owner and group associated with particular file. permissions on particular file. etc
There is no creation date available as far as I know unless you log the file creation date in a script. You can use the stat command as mentioned above to get the time stamp for last access, last change and last modification time.

$stat --version
stat (GNU coreutils) 5.97
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <>.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.

In linux, centos, fedora, redhat, suse, ubuntu, mint, debian, mandrake, caldera, yolinux, gnulinux

How to find directory last change / modification date and time?
How to find file / filesystem last change / modification date and time?
How to find last “access” date and time for file / directory?

As we are talking about time stamp (access, change, modify) for files / directories in linux, we much consider talking about “touch” command.
“touch” [options] <file name(s)>  :
Mainly touch command is used to change time stamp for files. If file does not exist then touch command creates file with current time stamp. e.g

$touch file1 file2 file3

will create 3 files in your present working directory with current time stamp.
Several of touch’s options are specifically designed to allow the user to change the timestamps for files. For example, the -a option changes only the access time, while the -m option changes only the modification time. The use of both of these options together changes both the access and modification times to the current time, for example:

$touch -am file11

The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file’s time stamps instead of current time. For example, the following would tell it to use the times of file4 for file5:

$touch -r myfile yourfile

The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the following command would make file7 30 seconds older than file6.

$touch -r myfile -B 30 yourfile

Importance of touch command.
Ah, beware before you use touch command on production system.
How to change times tamp using touch command?

What is PHP? server-side-scripting

What is PHP? <?php … ?>

PHP (Hypertext Preprocessor) is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a scripting language designed to fill the gap between SSI (Server Side Includes) and Perl, intended largely for the web environment. PHP has gained quite a following in recent times, and it is one of the forerunners in the Open Source software movement. Its popularity derives from its C-like syntax, and its simplicity. PHP is currently divided into two major versions: PHP 4 and PHP 5, although PHP 4 is deprecated and is no longer developed or supplied with critical bug fixes. PHP 6 is currently under development.

PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.

  • PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
  • It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
  • PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time.
  • PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.

Common uses of PHP:

  • Access control on websites / web applications using php. you can restrict users to access some pages of your website.
  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • It can encrypt data.

“Hello Web World” Script in PHP:

To get a taste of PHP, first start with simple PHP scripts. Since “Hello, Web World!” is an essential example, first we will create a friendly little “Hello, World!” script.

As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal HTML (or XHTML if you’re cutting-edge) you’ll have PHP statements like this:

<title>Hello Web World</title>
    <?php echo "Hello, Web World!";?>

It will produce following result:

Hello, Web World!

If you examine the HTML output of the above example, you’ll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.

All PHP code must be included inside one of the three special markup tags ate are recognised by the PHP Parser.

<?php PHP code goes here ?>

<?    PHP code goes here ?>

<script language="php"> PHP code goes here </script>

Most common tag is the <?php…?> and we will also use same tag in our tutorial.

From the next chapter we will start with PHP Environment Setup on your machine and then we will dig out almost all concepts related to PHP to make you comfortable with the PHP language.
Is PHP a buzz word ?
Why php is so popular ?
PHP uh.. never heard of it ? what is php?
php a hypertext preprocessor?
A powerful server-side-scripting language, PHP.

What is sed? linux/unix

sed is stream editor for filtering and transforming text.
Sed  is a stream editor.  A stream editor is used to perform basic text transformations on an input stream (a file or input from  a  pipeline). While  in  some  ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s),  and is consequently more efficient.  But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other  types  of  editors.

sed is very useful when you are making same changes on multiple files or multiples times in same file. Imagine using regular text editor for such scenario. you will feel rest-less when you do such changes manually. once you start using sed, those changes will be done easily.. quickly..How to find version of sed on your system ?

$sed --version
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
$man sed
--- Read this man page for more details.

Example 1 :

$sed 's/wrong pattern/correct pattern/' original.file

substitutes in EVERY line of a file called wrong.file the first and only the first occurrence of “wrong pattern” by “good pattern” and displays the corrected text at standard output. Check the output until everything is ok, then redirect the output (you can not overwrite the old file)

$sed 's/wrong pattern/correct pattern/' original.file > updated.file

Another way to redirect the output:

$cat wrong_file.txt | sed 's/wrong pattern/good pattern/g' > correct_file.txt

If you want to replace the “wrong pattern” globally (whole file), e.g. several times a line, then enter

$sed 's/wrong pattern/good pattern/g' wrong.file > correct.file

To replace the “wrong pattern” only once at the very first occurrence we need to adress the substitution. Either we know at which line the “wrong pattern” is located, i.e. line 16, then type

$sed '16s/wrong pattern/good pattern/' original.file > correct_new.file

If we don’t know the position of the “wrong pattern”, we need to give an address range where sed should do the substitution, i.e.

$sed '1,16s/wrong pattern/correct pattern/' original.file > correct_new.file

but in an general case we won’t know if the “wrong pattern” occurs exactly once in the first 20 lines… But we can define the range with the pattern itself, so we expect

$sed '1,/wrong pattern/s/wrong pattern/correct pattern/' correct_new.file

to do the job: start at line 1 and substitute until the first occurrence is found. Unfortunately this doesn’t work IF the “wrong pattern” appears in the first AND second line! Because the 1 activates the first line the second address of the range is at least the second line. In this case the “wrong pattern” will be substituted in both lines. What we need is

$sed '0,/wrong pattern/s/wrong pattern/good pattern/' wrong.file

to prevent the activation of the second line where the “wrong pattern”
could appear again.
How to perform different append operations using sed?
Append content of file year.txt at line 3 of data.txt.

$sed '3 r file1.txt' file2.txt > file1_2.txt

Append after each line with the string “pattern” a line with text “new”.

$sed '/pattern/anew' original.file

Append after each line a new line with text “new”.

$sed 'a/new' original.file

 Why “sed” is system administrators friend ?
How do is edit multiple files in 1 go ?
Make your edit/update/delete/replace easier with sed?
Sed “The King” of stream / patter edditing / formating and transforming