The vi, vim editor in linux

In this quick tutorial I will share some vim basics. Vim is a powerful text editor used in CLI (command line interface). As there are lots of configuration files in gnu/linux, which are all in clear text format, you’ll often need to edit them using a text editor. vim (in short vi) is a great tool to use.
One of the first things to know about vi is that it typically functions in three different modes:

  1. Command mode
  2. Insert mode
  3. Last line mode

vi command mode

When you first start editing a file with the vi editor you will be in vi command mode. In this mode you can issue many vi commands, including commands like insert, append, and delete, and other search and navigation commands that let you move around your file.

Possibly the most important thing to know is that when you’re in command mode you can’t insert text immediately. You first need to issue an insert, append, or open command to insert text. These commands are actually fairly simple, and I’ve documented them in this

Lets talk about other vim modes.

vi insert mode

Once you issue a vi insert, append, or open command, you will be in vi insert mode. If you’re working with a modern vi or vim implementation, your vi editor is typically configured to show the current mode of operation, so when you go into insert mode, you’ll see a text string like this on the last line of your vi editor window:

-- INSERT --

At this point you can (a) type text into your file and (b) use the arrow keys to navigate around your file just as you would do with any other text editor.

A very important concept to know is that when you’re in vi insert mode, but you want to switch back to vi command mode, you easily move back to command mode by pressing the [Esc] key. This command is so important, I’ll show it again:

[Esc]

This command is very common, and I often see expert vi users press the [Esc] key several times in a row. They usually do this (a) to be sure they hit the key and they’re really back in command mode, and (b) to hear the beep from the computer, which happens when you press the [Esc] key when you’re already in vi command mode. This seems to serve as a form of feedback which assures them that they’re in command mode.

vi last line mode

The last vi mode is known as vi last line mode. You can only get to last line mode from command mode, and you get into last line mode by pressing the colon key, like this:

:

After pressing this key, you’ll see a colon character appear at the beginning of the last line of your vi editor window, and your cursor will be moved to that position. This indicates that vi is ready for you to type in a “last line command”.

From this vi command prompt you can do all sorts of really amazing things. You can do simple things, like quitting your vi session, like this:

:q

or this:

:q!

or this:

:wq

From last line more you can also perform some amazing vi search commands or vim search and replace commands. Another cool thing is that you can issue Linux or Unix commands from within your vi editor session, like this simple ls command:

:!ls

It’s really handy sometimes to be able to stay in your vi editing session but still be able to run Unix or Linux commands.

And finally, you can also issue many vi configuration commands, such as this command that tells vi to show lines numbers in your current editor window:

:set shownumber

There is a ton of power in this vi last line mode, and I’ve tried to share pieces of this power in a variety of different vi tutorials. (Just search this blog for “vi” or “vi editor” and you’ll find a wealth of vi tutorials.)

One last note about the vi last line mode: If you’re in last line mode, and you want to switch back to command mode, there are several different ways to do this. For consistency, one way to do this is to press the [Esc] key twice, like this:

[Esc][Esc]

(This is consistent with the method of moving from insert mode back to command mode, except you have to press the [Esc] key twice.)

A second way is to press the [Backspace] key until anything you typed and the initial “:” character are gone. At this point you’ll be back in command mode.

Finally, if you haven’t typed anything at all, and you’re just looking at the “:” prompt on the last line, you can just press [Enter], and you’ll be placed back in vi command mode.

After opening a new OR existing file in vim editor with vim you can try modes listed here.

Insert mod : lets you insert text in a document. shotcurt : “i” (insert where the cursor is) or “o” (insert at the beginning of the following line).

Visual mod : permits to select the text like you would do with a mouse but using the keyboard instead of the mouse. Useful to copy several lines for example. shotcurt : V

Let’s now speak about the command mode.

A command begins with the symbol “:”.

When you are in another mod you can use the escape key (sometimes you’ll need to hit it twice) to come back to command mod at any time.

save : :w
save and exit : :wq
exit : :q
force : ! (example :w! :q!)
vertical split : open a document and then type :vsplit /path-to-document/document and this will open the specified document and split the screen so you can see both documents.
copy : y
copy a line : yy
paste : p
cut : d
cut a line : dd

I repeat these are very basic commands for vim, but they are very useful, and I hope this will help you configuring your Linux.

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:-

#date MMDDhhmmYYYY.ss

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 ?

Note:
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:

user:$1$eFd7EIOg$EeCk6XgKktWSUgi2pGUpk.:13852:0:99999:7:::

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:

user:*:13852:0:99999:7:::

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:

user:!$1$eFd7EIOg$EeCk6XgKktWSUgi2pGUpk.:13852:0:99999:7:::

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:

userdel

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 search file(s) in linux / unix?

find is very famous and regularly used command to find files in the Linux/UNIX filesystem based on various different conditions. Let us review some practical examples of find command. All system administrators love this command, sometimes a life saver..
Syntax:

find [pathnames] [conditions]

How to find files containing a particular / specific word in its name?
The following command looks for all the files under /etc directory with cron
in the filename.

# find /etc -name "*cron*"

How to find all the files greater than certain size?
The following command will list all the files in the system greater than
10MB.

# find / -type f -size +10M

How to find files that are not modified in the last x number of days?
The following command will list all the files that were modified more than 30
days ago under the current directory.

# find . -mtime +30

How to find files that are modified in the last x number of days?
The following command will list all the files that were modified in the last
five days under the current directory.

# find . –mtime -5

How to delete all the archive files with extension *.tar.gz and greater than 50MB?
Please be careful while executing the following command as you don’t want
to delete the files by mistake. The best practice is to execute the same
command with ls –l to make sure you know which files will get deleted when
you execute the command with rm.

# find / -type f -name *.tar.gz -size +50M -exec ls -l {} \;
# find / -type f -name *.tar.gz -size +50M -exec rm -f {} \;

How to archive all the files that are not modified in the last x
number of days?

The following command finds all the files not modified in the last 30 days
under /home/nilesh directory and creates an archive files under /tmp in the
format of ddmmyyyy_archive.tar.

# find /home/nilesh -type f -mtime +30 | xargs tar -cvf
/tmp/`date '+%d%m%Y'_archive.tar`

On a side note, you can perform lot of file related activities (including finding
files) us

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)

Note:
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

Output:

Stopping MySQL database server: mysqld.

Step # 2:

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

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

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
skip-grant-tables

Output:

[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

Output:

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.

mysql>

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

Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
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
mysql> RESET MASTER;

To clean up Binary Log on Slave Server

mysql -u username -p
mysql> RESET SLAVE;

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 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}

Where,

  • -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 mysql10.nixcraft.in 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.

mysql>

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

USE demo;
SHOW TABLES;

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
Bye

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

To exit type quit or \q:

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
Bye

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 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 <http://www.gnu.org/licenses/gpl.html>.
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?