Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Saturday, November 12, 2022

How do we kill the processes in linux/unix with a process name?

Below is the command to kill all the processes running based on a process name: ps -ef | grep {process_name} | grep -v grep | awk '{print $2}' | xargs kill


Eg: Kill all the processes that have chrome as part of the process name: ps -ef | grep chrome | grep -v grep | awk '{print $2}' | xargs kill

Friday, October 14, 2016

Listing files that were changed in a certain range of time

Here is the simple thing, that you can do to crack the list!

touch -t 201610110000 startTime 
touch -t 201611110000 stopTime 
find . -newer startTime \! -newer stopTime

Wednesday, June 19, 2013

Script to calculate the logIn time in linux

This is could be very useful when you need to calculate the logIn time for attendance/swipes. Here it goes.


#!/bin/bash

. ~/.bash_functions

function updateLogInTime()
{
    file=$1
    fullDate=`date +"%d-%m-%Y %H:%M:%S"`
    today=`date +"%d-%m-%Y"`;
    todayEntry=`grep $today $1`;
    now=`date +"%H:%M:%S"`;
    nowInSecs=`tounixtime`
    if [ -z "$todayEntry" ]; then
       echo "UpdatedAt: $fullDate, In: $now/$nowInSecs, Out: $now/$nowInSecs, Total: `date -u -d @"0" +'%-H:%-M:%-S'`" >> $file;
    else
       tokens=( `getTokens "$todayEntry" ,` )
       # use for loop read all fields
       for (( i=0; i<${#tokens[@]}; i++ ))
       do
           token=${tokens[i]}
           case $token in
                UpdatedAt:)
                        updatedTodayEntry="$updatedTodayEntry $token $fullDate";;
                In:)
                        inTimeToken=${tokens[i+1]};
                        inTimeTokens=( `getTokens "$inTimeToken" /` );
                        inTime=${inTimeTokens[1]};
                        updatedTodayEntry="$updatedTodayEntry, $token $inTimeToken";;
                Out:)
                        updatedTodayEntry="$updatedTodayEntry, $token $now/$nowInSecs";;
                Total:)
                        diff=`calc $nowInSecs-$inTime`;
                        updatedTodayEntry="$updatedTodayEntry, $token `date -u -d @"$diff" +'%-H:%-M:%-S'`";;
           esac
       done
       updatedTodayEntry=`trim $updatedTodayEntry`
       sed -i s%"${todayEntry}"%"${updatedTodayEntry}"%g $file
    fi
}

function main()
{
     screenLocked=`isScreenLocked`;
     if [ $screenLocked -eq 0 ]; then
        updateLogInTime /krishna/Misc/Notifications/data/logInTime.db
     fi
}

main

Happy Coding!

Thursday, October 11, 2012

Shell script Tips - Using functions and Executing a java process as background one

It is small shell script which teach you about the things, that we use in our routine shell scripting life.
They are:
1) Functions.
2) Resolving the current path ro absolute path.
3) Blinking Text.
4) Proper usage of log.
5) Executing a java process in back ground.
6) Usage of ps and grep commands.
7) Proper code formation.

#!/bin/bash

########
# This is to log the events to a file in events directory, monitors and manages the java process which
# actually reads  the files and sends the events to third party for processing. 
#
# It also demonstrates the usage and handling of certain features defined in *ix kernal.
#
# @author Rajakrishna V. Reddy
# @version 1.0
#
########

### CONSTANTS may be changed over a time ###
jarName=arcsight-integration-1.0.jar
javaLogFileName=javaProcess.log
scriptLogFileName=script.log
javaFileName=com.mt.arcsight.ArcsightManager
NOW=`date +"%y-%m-%d %H:%M:%S"`


### Variables to be defined or automatically get defined by script as it is flowing ###
myName=$(readlink -fn $(type -p $0))
currentDir=$(dirname $myName)
eventDir=$currentDir/../events
logDir=$currentDir/../log
confDir=$currentDir/../conf
binDir=$currentDir
eventFileName=
event=
keyValuePairLength=

### Logging the text in blinking mode, and set it back to normal mode after display ###
function blinkingLog
{
        echo -e "[$NOW] [ArcsightServer] [INFO]: $* \033[0m"
}

### Creates all the required dirs, if doesn't exists ###
function makeAllDris
{
makeDir $eventDir
makeDir $logDir
makeDir $confDir

eventDir=$(cd $currentDir/../events && pwd)
logDir=$(cd $currentDir/../log && pwd)
confDir=$(cd $currentDir/../conf && pwd)
}

### Creates a dir, if doesn't exists ###
function makeDir
{
if [ ! -d $1 ]; then
#echo "Events dir is not exists, going to create: $eventDir";
mkdir $1
#echo "Created the event dir successfully: $eventDir";
return 1;
fi
}
### Just check and create the event dir ###
function checkAndCreateEventDir
{
if [ ! -d $eventDir ]; then
#echo "Events dir is not exists, going to create: $eventDir";
mkdir $eventDir
#echo "Created the event dir successfully: $eventDir";
return 1;
fi
}

### Just check and create the log dir ###
function checkAndCreateLogDir
{
        if [ ! -d $logDir ]; then
blinkingLog "\033[31m Log dir does not exists, \033[32mgoing to create it: $logDir"
                mkdir $logDir
                blinkingLog " Created the event dir successfully: $logDir";
        fi
if [ ! -f $logDir/$javaLogFileName ]; then
blinkingLog "\033[31m Log dir does not exists, \033[32mgoing to create it: $logDir/$javaLogFileName"
touch $logDir/$javaLogFileName;
chmod +w $logDir/$javaLogFileName;
       blinkingLog " Log file created successfully: $logDir/$javaLogFileName";
fi
}

### Get the present events count and get the desired filename by using the next event number ###
function checkAndCreateNextEventFile
{
fileCount=`ls -ltr $eventDir | wc -l`;

#Contains total number of files + "total 0", hence it gets the number of files + 1, which will be our next file number;
#fileCount=`expr $fileCount - 1`;

eventFileName=$fileCount.'event';
#echo "The next event file name is: $eventFileName";
touch $eventDir/$eventFileName;
chmod +w $eventDir/$eventFileName;
#echo "Event file is created successfully: $eventFileName."
}

### Write the content into the specified file, decided by checkAndCreateNextEventFile function. ###
function writeAnEvent
{
#blinkingLog "\033[05m Writing the given event into $eventDir/$eventFileName";
#blinkingLog "\033[05m Writing the event: $event"
checkAndCreateEventDir
checkAndCreateNextEventFile
echo "$event" > $eventDir/$eventFileName;
}

### Manages the java process by checking and starting it if not already running ###
function manageJavaProcess
{
        checkAndCreateLogDir
        pid=`ps -ef | grep -i $javaFileName | grep -v grep | awk '{print $2}'`
        if [ -z "$pid" -o "$pid" == " " ]; then
                blinkingLog " Checking the status of java app: \033[31m Process: $javaFileName is down..!!! \033[32mstarting it.!"
                java -cp $PATH:$currentDir/arcsight-integration-1.0.jar com.mt.arcsight.ArcsightManager $confDir/arcsight.properties > $logDir/$javaLogFileName 2>&1 &
sleep 0.1
        pid=`ps -ef | grep -i $javaFileName | grep -v grep | awk '{print $2}'`
blinkingLog "\033[32m Process $javaFileName is started with pid: $pid";
        else
                blinkingLog " Checking the status of java app: \033[32m Process $javaFileName is running with pid: $pid";
        fi
}

### Starting method of your process ###
function main
{
event=$*
keyValuePairLength=$#

makeAllDris
blinkingLog "\033[35m **** Sending the event to MWatch. ****";
manageJavaProcess
writeAnEvent
blinkingLog "\033[35m **** Sent the event to MWatch successfully. ****";
}

### Starting point of your process ###
main $*

Tuesday, October 9, 2012

Unix in a nut shell-1


1) Getting the Absolute Path in the shell script:

scriptFile=$(readlink -fn $(type -p $0))
echo "Absolute path: $scriptFile"

1) Reading ALL the command-line params in the shell script:

Shell script accepts parameters in following format... 
$1 : first 
$2 : second....so on upto  9th param,
$9 : 9th param 

whereas $0 : gives script/function name 

If your script has more than 9 params then !???? :( :( 

No worries ... just access them in following way... 
${12} : 12th param 
${18} : 18th param

Mastering in Linux "history" command

Tips to get mastered in using history in *ix systems!!
When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.

1. Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.
# export HISTTIMEFORMAT='%F %T '
# history | more
1  2008-08-05 19:02:39 service network restart
2  2008-08-05 19:02:39 exit
3  2008-08-05 19:02:39 id
4  2008-08-05 19:02:39 cat /etc/redhat-release

2. Search the history using Control+R

I strongly believe, this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.
# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red': cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)
Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.
# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd': service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start

3. Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.
  1. Use the up arrow to view the previous command and press enter to execute it.
  2. Type !! and press enter from the command line
  3. Type !-1 and press enter from the command line.
  4. Press Control+P will display the previous command, press enter to execute it

4. Execute a specific command from history

In the following example, If you want to repeat the command #4, you can do !4 as shown below.
# history | more
1  service network restart
2  exit
3  id
4  cat /etc/redhat-release

# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)

5. Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.
# !ps
ps aux | grep yp
root     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

6. Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

7. Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.

8. Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.
# pwd
# pwd
# pwd
# history | tail -4
44  pwd
45  pwd
46  pwd [Note that there are three pwd commands in history, after
executing pwd 3 times as shown above]
47  history | tail -4

# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd [Note that there is only one pwd command in the history, even after
executing pwd 3 times as shown above]
58  history | tail -4

9. Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38  pwd
39  service httpd stop
40  history | tail -3

# ls -ltr
# service httpd stop
# history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -ltr
39  service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40  history | tail -6

10. Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
#  service httpd stop [Note that there is a space at the beginning of service,
to ignore this command from history]
# history | tail -3
67  ls -ltr
68  pwd
69  history | tail -3

11. Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.
# history -c

12. Subtitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.
In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi  !^
vi anaconda-ks.cfg

13. Substitute a specific argument for a specific command.

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.
# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

14. Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.
# export HISTSIZE=0
# history
# [Note that history did not display anything]

15. Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.
# export HISTIGNORE="pwd:ls:ls -ltr:"
# pwd
# ls
# ls -ltr
# service httpd stop

# history | tail -3
79  export HISTIGNORE="pwd:ls:ls -ltr:"
80  service httpd stop
81  history
[Note that history did not record pwd, ls and ls -ltr]


Monday, September 10, 2012

SSH Problem: ssh_exchange_identification: Connection closed by remote host


Are you tired of getting the exception ssh_exchange_identification: Connection closed by remote host even if the key is correct !?????? 

Then here are few tips to rectify that ...!!

1) Check the permission of /var/log/btmp, it must be 600 
    chmod 600 /var/log/btmp

now
ll /var/log/btmp
-rw------- 1 root utmp 73026817 Jan 11 11:13 btmp

Check now....!!! Still not solved !!?????

2) /var/empty/sshd must be owned by root and not group or world-writable
chown -R root:root /var/empty/sshd

Now  
ll  /var/empty/sshd 
-rw------- 1 root utmp 73026817 Jan 11 11:13 sshd

Then Try, U must be logged-in !!!

Have FUN!!!

Sunday, September 2, 2012

FTP Server config on linux

FTP Server

File Transfer Protocol (FTP) is a TCP protocol for uploading and downloading files between computers. FTP works on a client/server model. The server component is called an FTP daemon. It continuously listens for FTP requests from remote clients. When a request is received, it manages the login and sets up the connection. For the duration of the session it executes any of commands sent by the FTP client.
Access to an FTP server can be managed in two ways:
  • Anonymous
  • Authenticated
In the Anonymous mode, remote clients can access the FTP server by using the default user account called "anonymous" or "ftp" and sending an email address as the password. In the Authenticated mode a user must have an account and a password. User access to the FTP server directories and files is dependent on the permissions defined for the account used at login. As a general rule, the FTP daemon will hide the root directory of the FTP server and change it to the FTP Home directory. This hides the rest of the file system from remote sessions.

vsftpd - FTP Server Installation

vsftpd is an FTP daemon available in Ubuntu. It is easy to install, set up, and maintain. To install vsftpd you can run the following command:
sudo apt-get install vsftpd

Anonymous FTP Configuration

By default vsftpd is configured to only allow anonymous download. During installation a ftp user is created with a home directory of /home/ftp. This is the default FTP directory.

If you wish to change this location, to /srv/ftp for example, simply create a directory in another location and change the ftp user's home directory:
 
sudo mkdir /srv/ftp
sudo usermod -d /srv/ftp ftp 

After making the change restart vsftpd:
sudo /etc/init.d/vsftpd restart

Finally, copy any files and directories you would like to make available through anonymous FTP to /srv/ftp.

User Authenticated FTP Configuration

To configure vsftpd to authenticate system users and allow them to upload files
edit /etc/vsftpd.conf:
local_enable=YES
write_enable=YES

Now restart vsftpd:
sudo /etc/init.d/vsftpd restart

Now when system users login to FTP they will start in their home directories where they can download, upload, create directories, etc.
Similarly, by default, the anonymous users are not allowed to upload files to FTP server. To change this setting, you should uncomment the following line, and restart vsftpd:
 
anon_upload_enable=YES
[Warning]
Enabling anonymous FTP upload can be an extreme security risk. It is best to not enable anonymous upload on servers accessed directly from the Internet.
The configuration file consists of many configuration parameters. The information about each parameter is available in the configuration file. Alternatively, you can refer to the man page, man 5 vsftpd.conf for details of each parameter.

Securing FTP

There are options in /etc/vsftpd.conf to help make vsftpd more secure. For example users can be limited to their home directories by uncommenting:
 
chroot_local_user=YES
 
You can also limit a specific list of users to just their home directories:
 
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

After uncommenting the above options, create a /etc/vsftpd.chroot_list containing a list of users one per line. Then restart vsftpd:
 
sudo /etc/init.d/vsftpd restart

Also, the /etc/ftpusers file is a list of users that are disallowed FTP access. The default list includes root, daemon, nobody, etc. To disable FTP access for additional users simply add them to the list.

FTP can also be encrypted using FTPS. Different from SFTP, FTPS is FTP over Secure Socket Layer (SSL). SFTP is a FTP like session over an encrypted SSH connection. A major difference is that users of SFTP need to have a shell account on the system, instead of a nologin shell. Providing all users with a shell may not be ideal for some environments, such as a shared web host.

To configure FTPS, edit /etc/vsftpd.conf and at the bottom add:
ssl_enable=Yes

Also, notice the certificate and key related options:
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

By default these options are set the certificate and key provided by the ssl-cert package. In a production environment these should be replaced with a certificate and key generated for the specific host. For more information on certificates see the section called “Certificates”.

Now restart vsftpd, and non-anonymous users will be forced to use FTPS:
sudo /etc/init.d/vsftpd restart
To allow users with a shell of /usr/sbin/nologin access to FTP, but have no shell access, edit /etc/shells adding the nologin shell:
 
# /etc/shells: valid login shells
/bin/csh
/bin/sh
/usr/bin/es
/usr/bin/ksh
/bin/ksh
/usr/bin/rc
/usr/bin/tcsh
/bin/tcsh
/usr/bin/esh
/bin/dash
/bin/bash
/bin/rbash
/usr/bin/screen
/usr/sbin/nologin

 This is necessary because, by default vsftpd uses PAM for authentication, and the /etc/pam.d/vsftpd configuration file contains:
auth    required        pam_shells.so

The shells PAM module restricts access to shells listed in the /etc/shells file.
Most popular FTP clients can be configured connect using FTPS. The lftp command line FTP client has the ability to use FTPS as well.

References

Monday, November 14, 2011

Advanced Commands in Vi





Click here for the Basic VI Commands

General Notes:

1. Before doing anything to a document, type the following command followed by a carriage return: :set showmode

2. VI is CaSe SEnsItiVe!!! So make sure Caps Lock is OFF.


Starting and Ending VI


Starting VI
vi filenameEdits filename
vi -r filenameEdits last save version of filename after a crash
vi + n filenameEdits filename and places curser at line n
vi + filenameEdits filename and places curser on last line
vi +/string filenameEdits filename and places curser on first occurance of string
vi filename file2 ...Edits filename, then edits file2 ... After the save, use :n


Ending VI
ZZ or :wq or :xSaves and exits VI
:wSaves current file but doesn't exit
:w!Saves current file overriding normal checks but doesn't exit
:w fileSaves current as file but doesn't exit
:w! fileSaves to file overriding normal checks but doesn't exit
:n,mw fileSaves lines n through m to file
:n,mw >>fileSaves lines n through m to the end of file
:qQuits VI and may prompt if you need to save
:q!Quits VI and without saving
:e!Edits file discarding any unsaved changes (starts over)
:we!Saves and continues to edit current file



Status


:.=Shows current line number
:=Shows number of lines in file
Control-GShows filename, current line number, total lines in file, and % of file location
lDisplays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) formfeed (^L^) of current line



Modes


Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.



Inserting Text


iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters
CTRL-v charWhile inserting, ignores special meaning of char (e.g., for inserting characters like ESC and CTRL) until ESC is used
:r fileReads file and inserts it after current line
:nr fileReads file and inserts it after line n
CTRL-i or TABWhile inserting, inserts one shift width

Things to do while in Insert Mode:
CTRL-h or BackspaceWhile inserting, deletes previous character
CTRL-wWhile inserting, deletes previous word
CTRL-xWhile inserting, deletes to start of inserted text
CTRL-vTake the next character literally. (i.e. To insert a Control-H, type Control-v Control-h)



Motion


hMove left
jMove down
kMove up
lMove right
Arrow KeysThese do work, but they may be too slow on big files. Also may have unpredictable results when arrow keys are not mapped correctly in client.
wMove to next word
WMove to next blank delimited word
bMove to the beginning of the word
BMove to the beginning of blank delimted word
^Moves to the first non-blank character in the current line
+ or Moves to the first character in the next line
-Moves to the first non-blank character in the previous line
eMove to the end of the word
EMove to the end of Blank delimited word
(Move a sentence back
)Move a sentence forward
{Move a paragraph back
}Move a paragraph forward
[[Move a section back
]]Move a section forward
0 or |Move to the begining of the line
n|Moves to the column n in the current line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
nHMoves to nth line from the top of the screen
MMove to middle of screen
LMove to botton of screen
nLMoves to nth line from the bottom of the screen
Control-dMove forward ½ screen
Control-fMove forward one full screen
Control-uMove backward ½ screen
Control-bMove backward one full screen
CTRL-eMoves screen up one line
CTRL-yMoves screen down one line
CTRL-uMoves screen up ½ page
CTRL-dMoves screen down ½ page
CTRL-bMoves screen up one page
CTRL-fMoves screen down one page
CTRL-IRedraws screen
z z-carriage return makes the current line the top line on the page
nz Makes the line n the top line on the page
z.Makes the current line the middle line on the page
nz.Makes the line n the middle line on the page
z-Makes the current line the bottom line on the page
nz-Makes the line n the bottom line on the page
%Move to associated ( ), { }, [ ]



Deleting Text


Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are:
xDelete character to the right of cursor
nxDeletes n characters starting with current; omitting n deletes current character only
XDelete character to the left of cursor
nXDeletes previous n characters; omitting n deletes previous character only
DDelete to the end of the line
d$Deletes from the cursor to the end of the line
dd or :dDelete current line
ndwDeletes the next n words starting with current
ndbDeletes the previous n words starting with current
nddDeletes n lines beginning with the current line
:n,mdDeletes lines n through m
dMotion_cmdDeletes everything included in the Motion Command (e.g., dG would delete from current position to the end of the file, and d4 would delete to the end of the fourth sentence).
"npRetrieves the last nth delete (last 9 deletes are kept in a buffer)
"1pu.u.Scrolls through the delete buffer until the desired delete is retrieved (repeat u.)



Yanking Text


Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are:
yyYank the current line
:yYank the current line
nyy or nYPlaces n lines in the buffer-copies
yMotion_cmdCopies everything from the curser to the Motion Command (e.g., yG would copy from current position to the end of the file, and y4 would copy to the end of the fourth sentence)
"(a-z)nyy or "(a-z)nddCopies or cuts (deletes) n lines into a named buffer a through z; omitting n works on current line




Changing text


The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For example cw changes a word. A few other change commands are:
CChange to the end of the line
cc or SChange the whole line until ESC is pressed
xpSwitches character at cursor with following character
stextSubstitutes text for the current character until ESC is used
cwtextChanges current word to text until ESC is used
CtextChanges rest of the current line to text until ESC is used
cMotion_cmdChanges to text from current position to Motion Command until ESC is used
<< or >>Shifts the line left or right (respectively) by one shift width (a tab)
n<< or n>>Shifts n lines left or right (respectively) by one shift width (a tab)
<Motion_cmd or >Motion_cmdUse with Motion Command to shift multiple lines left or right



Putting text


pPut after the position or after the line
PPut before the poition or before the line
"(a-z)p or "(a-z)PPastes text from a named buffer a through z after or before the current line



Buffers


Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.



Markers


Named markers may be set on any line in a file. Any lower case letter may be a marker name. Markers may also be used as limits for ranges.
mcSet marker c on this line
`cGo to beginning of marker c line.
'cGo to first non-blank character of marker c line.



Search for strings


/stringSearch forward for string
?stringSearch back for string
nSearch for next instance of string
NSearch for previous instance of string
%Searches to beginning of balancing ( ) [ ] or { }
fcSearches forward in current line to char
FcSearches backward in current line to char
tcSearches forward in current line to character before char
TcharSearches backward in current line to character before char
?strFinds in reverse for str
:set icIgnores case when searching
:set noicPays attention to case when searching
:n,ms/str1/str2/optSearches from n to m for str1; replaces str1 to str2; using opt-opt can be g for global change, c to confirm change (y to acknowledge, to suppress), and p to print changed lines
&Repeats last :s command
:g/str/cmdRuns cmd on all lines that contain str
:g/str1/s/str2/str3/Finds the line containing str1, replaces str2 with str3
:v/str/cmdExecutes cmd on all lines that do not match str
,Repeats, in reverse direction, last / or ? search command



Replace


The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flagsReplace pattern with string according to flags.
gFlag - Replace all occurences of pattern
cFlag - Confirm replaces.
&Repeat last :s command



Regular Expressions


. (dot)Any single character except newline
*zero or more occurances of any character
[...]Any single character specified in the set
[^...]Any single character not specified in the set
\<Matches beginning of word
\>Matches end of word
^Anchor - beginning of the line
$Anchor - end of line
\<Anchor - begining of word
\>Anchor - end of word
\(...\)Grouping - usually used to group conditions
\nContents of nth grouping
\Escapes the meaning of the next character (e.g., \$ allows you to search for $)
\\Escapes the \ character

[...] - Set Examples
[A-Z]The SET from Capital A to Capital Z
[a-z]The SET from lowercase a to lowercase z
[0-9]The SET from 0 to 9 (All numerals)
[./=+]The SET containing . (dot), / (slash), =, and +
[-A-F]The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z]The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z]In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters
[a-z]{m}Look for m occurances of the SET from lowercase a to lowercase z
[a-z]{m,n}Look for at least m occurances, but no more than n occurances of the SET from lowercase a to lowercase z

Regular Expression Examples
/Hello/Matches if the line contains the value Hello
/^TEST$/Matches if the line contains TEST by itself
/^[a-zA-Z]/Matches if the line starts with any letter
/^[a-z].*/Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/Matches if line ends with 2134
/\(21|35\)/Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/Matches if there are zero or more numbers in the line
/^[^#]/Matches if the first character is not a # in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specified


Counts


Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with this method, say to insert the same line 100 times.



Ranges


Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:n,mRange - Lines n-m
:.Range - Current line
:$Range - Last line
:'cRange - Marker c
:%Range - All lines in file
:g/pattern/Range - All lines that contain pattern



Shell Functions


:! cmdExecutes shell command cmd; you can add these special characters to indicate:% name of current file# name of last file edited
!! cmdExecutes shell command cmd, places output in file starting at current line
:!!Executes last shell command
:r! cmdReads and inserts output from cmd
:f fileRenames current file to file
:w !cmdSends currently edited file to cmd as standard input and execute cmd
:cd dirChanges current working directory to dir
:shStarts a sub-shell (CTRL-d returns to editor)
:so fileReads and executes commands in file (file is a shell script)
!Motion_cmdSends text from current position to Motion Command to shell command cmd
!}sortSorts from current position to end of paragraph and replaces text with sorted text



Files


:w fileWrite to file
:r fileRead file in after line
:nGo to next file
:pGo to previous file
:e fileEdit file
!!programReplace line with output from program



VI Settings


--noto
Note: Options given are default. To change them, enter type :set option to turn them on or :set nooptioni to turn them off.To make them execute every time you open VI, create a file in your HOME directory called .exrc and type the options without the colon (:) preceding the option
SetDefaultDescription
:set ainoaiTurns on auto indentation
:set all--Prints all options to the screen
:set apawPrints line after d c J m :s t u commands
:set awnoawAutomatic write on :n ! e# ^^ :rew ^} :tag
:set bfnobfDiscards control characters from input
:set dir=tmpdir = /tmpSets tmp to directory or buffer file
:set ebnoedPrecedes error messages with a bell
:set ednoedPrecedes error messages with a bell
:set ht=ht = 8Sets terminal hardware tabs
:set icnoicIgnores case when searching
:set lispnolispModifies brackets for Lisp compatibility.
:set listnolistShows tabs (^l) and end of line ($)
:set magicmagicAllows pattern matching with special characters
:set mesg mesgAllows others to send messages
:set nooption
Turns off option
:set nunonuShows line numbers
:set optoptSpeeds output; eliminates automatic RETURN
:set para=para = LIlPLPPPQPbpPmacro names that start paragraphs for { and } operators
:set promptpromptPrompts for command input with :
:set renoreSimulates smart terminal on dumb terminal
:set remapremapAccept macros within macros
:set reportnoreportIndicates largest size of changes reported on status line
:set ronoroChanges file type to "read only"
:set scroll=nscroll = 11set n lines for CTRL-d and z
:set sh=shell_pathsh = /bin/shset shell escape (default is /bin/sh) to shell_path
:set showmodenosmIndicates input or replace mode at bottom
:set slowslowPospone display updates during inserts
:set smnosmShow matching { or ( as ) or } is typed
:set sw=nsw = 8Sets shift width to n characters
:set tags=xtags = /usr/lib/tagsPath for files checked for tags (current directory included in default)
:set term$TERMPrints terminal type
:set tersenoterseShorten messages with terse
:set timeout
Eliminates one-second time limit for macros
:set tl=ntl = 0Sets significance of tags beyond n characters (0 means all)
:set ts=nts = 8Sets tab stops to n for text input
:set wanowaInhibits normal checks before write commands
:set warnwarn
Warns "no write since last change"
:set window=nwindow = nSets number of lines in a text window to n
:set wm=nwm = 0Sets automatic wraparound n spaces from right margin.
:set wswsSets automatic wraparound n spaces from right margin.



Key Mapping


NOTE: Map allows you to define strings of VI commands. If you create a file called ".exrc" in your home directory, any map or set command you place inside this file will be executed every time you run VI. To imbed control characters like ESC in the macro, you need to precede them with CTRL-v. If you need to include quotes ("), precede them with a \ (backslash). Unused keys in vi are: K V g q v * = and the function keys. Example (The actual VI commands are in blue): :map v /I CTRL-v ESC dwiYou CTRL-v ESC ESC Description: When v is pressed, search for "I" (/I ESC), delete word (dw), and insert "You" (iYou ESC). CTRL-v allows ESC to be inserted
:map key cmd_seqDefines key to run cmd_seq when pressed
:mapDisplays all created macros on status line
:unmap keyRemoves macro definition for key
:ab str stringWhen str is input, replaces it with string
:abDisplays all abbreviations
:una strUnabbreviates str



Other


~Toggle upper and lower case
JJoin lines
nJJoins the next n lines together; omitting n joins the beginning of the next line to the end of the current line
.Repeat last text-changing command
uUndo last change (Note: u in combination with . can allow multiple levels of undo in some versions)
UUndo all changes to line
;Repeats last f F t or T search command
:N or :EYou can open up a new split-screen window in (n)vi and then use ^w to switch between the two.



Return to the top

Basic Commands in VI





Click here for the Advanced VI commands

Modes


Vi has two modes insertion mode and command mode. The editor begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns the editor to command mode (where you can quit, for example by typing :q!). Most commands execute as soon as you type them except for "colon" commands which execute when you press the ruturn key.



Quitting


:xExit, saving changes
:qExit as long as there have been no changes
ZZExit and save changes if any have been made
:q!Exit and ignore any changes



Inserting Text


iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters



Motion


hMove left
jMove down
kMove up
lMove right
wMove to next word
WMove to next blank delimited word
bMove to the beginning of the word
BMove to the beginning of blank delimted word
eMove to the end of the word
EMove to the end of Blank delimited word
(Move a sentence back
)Move a sentence forward
{Move a paragraph back
}Move a paragraph forward
0Move to the begining of the line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
MMove to middle of screen
LMove to botton of screen
%Move to associated ( ), { }, [ ]



Deleting Text


Almost all deletion commands are performed by typing d followed by a motion. For example, dw deletes a word. A few other deletes are:
xDelete character to the right of cursor
XDelete character to the left of cursor
DDelete to the end of the line
ddDelete current line
:dDelete current line



Yanking Text


Like deletion, almost all yank commands are performed by typing y followed by a motion. For example, y$ yanks to the end of the line. Two other yank commands are:
yyYank the current line
:yYank the current line



Changing text


The change command is a deletion command that leaves the editor in insert mode. It is performed by typing c followed by a motion. For wxample cw changes a word. A few other change commands are:
CChange to the end of the line
ccChange the whole line



Putting text


pPut after the position or after the line
PPut before the poition or before the line



Buffers


Named buffers may be specified before any deletion, change, yank or put command. The general prefix has the form "c where c is any lowercase character. for example, "adw deletes a word into buffer a. It may thereafter be put back into text with an appropriate "ap.



Markers


Named markers may be set on any line in a file. Any lower case letter may be a marker name. Markers may also be used as limits for ranges.
mcSet marker c on this line
`cGo to beginning of marker c line.
'cGo to first non-blank character of marker c line.



Search for strings


/stringSearch forward for string
?stringSearch back for string
nSearch for next instance of string
NSearch for previous instance of string



Replace


The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flagsReplace pattern with string according to flags.
gFlag - Replace all occurences of pattern
cFlag - Confirm replaces.
&Repeat last :s command



Regular Expressions


. (dot)Any single character except newline
*zero or more occurances of any character
[...]Any single character specified in the set
[^...]Any single character not specified in the set
^Anchor - beginning of the line
$Anchor - end of line
\<Anchor - begining of word
\>Anchor - end of word
\(...\)Grouping - usually used to group conditions
\nContents of nth grouping

[...] - Set Examples
[A-Z]The SET from Capital A to Capital Z
[a-z]The SET from lowercase a to lowercase z
[0-9]The SET from 0 to 9 (All numerals)
[./=+]The SET containing . (dot), / (slash), =, and +
[-A-F]The SET from Capital A to Capital F and the dash (dashes must be specified first)
[0-9 A-Z]The SET containing all capital letters and digits and a space
[A-Z][a-zA-Z]In the first position, the SET from Capital A to Capital Z
In the second character position, the SET containing all letters

Regular Expression Examples
/Hello/Matches if the line contains the value Hello
/^TEST$/Matches if the line contains TEST by itself
/^[a-zA-Z]/Matches if the line starts with any letter
/^[a-z].*/Matches if the first character of the line is a-z and there is at least one more of any character following it
/2134$/Matches if line ends with 2134
/\(21|35\)/Matches is the line contains 21 or 35
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/Matches if there are zero or more numbers in the line
/^[^#]/Matches if the first character is not a # in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specified


Counts


Nearly every command may be preceded by a number that specifies how many times it is to be performed. For example, 5dw will delete 5 words and 3fe will move the cursor forward to the 3rd occurence of the letter e. Even insertions may be repeated conveniently with thismethod, say to insert the same line 100 times.



Ranges


Ranges may precede most "colon" commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:n,mRange - Lines n-m
:.Range - Current line
:$Range - Last line
:'cRange - Marker c
:%Range - All lines in file
:g/pattern/Range - All lines that contain pattern



Files


:w fileWrite to file
:r fileRead file in after line
:nGo to next file
:pGo to previos file
:e fileEdit file
!!programReplace line with output from program



Other


~Toggle upp and lower case
JJoin lines
.Repeat last text-changing command
uUndo last change
UUndo all changes to line



Return to the top