From Documentation
Jump to: navigation, search
(Directory Structure in Linux)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
 
== Objectives ==
 
== Objectives ==
  
Line 4: Line 5:
 
to SHARCNET Linux systems.  Examples shown in the lightgrey areas were run on 'narwhal', a SHARCNET cluster.   
 
to SHARCNET Linux systems.  Examples shown in the lightgrey areas were run on 'narwhal', a SHARCNET cluster.   
 
We hope that this tutorial would be a good starting point for newcomers to Linux.  
 
We hope that this tutorial would be a good starting point for newcomers to Linux.  
 
  
 
== What is Linux? ==
 
== What is Linux? ==
Line 15: Line 15:
 
* '''multiusers''': several users at the same machine at once
 
* '''multiusers''': several users at the same machine at once
 
* '''multiplatforms''': it runs on many different CPU
 
* '''multiplatforms''': it runs on many different CPU
 
  
 
== Directory Structure in Linux ==
 
== Directory Structure in Linux ==
Line 53: Line 52:
 
|-
 
|-
 
| '''/home'''
 
| '''/home'''
| User's home directory (quota of 1GB). User has a common home directory on '''SHARCNET systems'''
+
| User's home directory (quota of 10GB). User has a common home directory on '''SHARCNET systems'''
 
|-
 
|-
 
| '''/work'''
 
| '''/work'''
| work directory, with a quota of 200GB on some large '''SHARCNET systems'''
+
| work directory, with a quota of 1TB on some large '''SHARCNET systems'''
 
|-
 
|-
 
| '''/scratch'''
 
| '''/scratch'''
Line 67: Line 66:
 
*  <font color='blue'>'''login'''</font>  
 
*  <font color='blue'>'''login'''</font>  
 
** Use SSH (see our [[SSH|SSH page]]) to login onto SHARCNET linux systems
 
** Use SSH (see our [[SSH|SSH page]]) to login onto SHARCNET linux systems
 
  
 
* <font color='blue'>'''hostname'''</font>: Print system name
 
* <font color='blue'>'''hostname'''</font>: Print system name
  [jemmyhu@nar316 ~]$ hostname
+
  [jemmyhu@saw-login2: ~]$ hostname
  nar316
+
  saw-login2
 
+
  
 
* <font color='blue'>'''who'''</font>: Print all usernames currently logged in
 
* <font color='blue'>'''who'''</font>: Print all usernames currently logged in
  [jemmyhu@nar316 ~]$ who
+
  [jemmyhu@saw-login2: ~]$ who
 
  inta    pts/0        Nov 29 12:10 (pharmtech4.pharm.helsinki.fi)
 
  inta    pts/0        Nov 29 12:10 (pharmtech4.pharm.helsinki.fi)
 
  jemmyhu  pts/1        Nov 29 09:33 (jemmyhu.uwaterloo.ca)
 
  jemmyhu  pts/1        Nov 29 09:33 (jemmyhu.uwaterloo.ca)
Line 90: Line 87:
 
  zzheng  pts/14      Nov 27 11:49 (meg34.megaladon.sharcnet)
 
  zzheng  pts/14      Nov 27 11:49 (meg34.megaladon.sharcnet)
  
 
 
* <font color='blue'>'''whoami'''</font>: Print the current username  
 
* <font color='blue'>'''whoami'''</font>: Print the current username  
  [jemmyhu@nar316 ~]$ whoami
+
  [jemmyhu@saw-login2: ~]$ whoami
 
  jemmyhu
 
  jemmyhu
 
  
 
* <font color='blue'>'''pwd'''</font>: Print working (current) directory
 
* <font color='blue'>'''pwd'''</font>: Print working (current) directory
  [jemmyhu@nar316 ~]$ pwd
+
  [jemmyhu@saw-login2: ~]$ pwd
 
  /home/jemmyhu
 
  /home/jemmyhu
 
  
 
* <font color='blue'>'''date'''</font>: Display the date & time
 
* <font color='blue'>'''date'''</font>: Display the date & time
  [jemmyhu@nar316 ~]$ date
+
  [jemmyhu@saw-login2: ~]$ date
  Mon Nov 19 15:59:55 EST 2007
+
  Mon Feb  3 10:06:09 EST 2014
 
+
  
 
* <font color='blue'>'''exit'''</font>: or <font color='blue'>'''logout'''</font> to leave the system
 
* <font color='blue'>'''exit'''</font>: or <font color='blue'>'''logout'''</font> to leave the system
Line 112: Line 105:
 
To change your Linux account password, use the command <font color='blue'>'''passwd'''</font>  
 
To change your Linux account password, use the command <font color='blue'>'''passwd'''</font>  
  
  [jemmyhu@nar316 ~]$ passwd
+
  [jemmyhu@saw-login2: ~]$ passwd
 
  Changing password for user jemmyhu.
 
  Changing password for user jemmyhu.
 
  Changing password for jemmyhu
 
  Changing password for jemmyhu
Line 126: Line 119:
  
 
'''NOTE: you can change your SHARCNET password on SHARCNET webportal.'''
 
'''NOTE: you can change your SHARCNET password on SHARCNET webportal.'''
 
  
 
== Basic Linux Commands ==
 
== Basic Linux Commands ==
  
 
The basic format of a command is  <font color='blue'>'''''command [option] ...'''''</font>, please check the command's manpage (man command) for other options.
 
The basic format of a command is  <font color='blue'>'''''command [option] ...'''''</font>, please check the command's manpage (man command) for other options.
 
  
 
=== ''ls'' - list the contents of a directory ===
 
=== ''ls'' - list the contents of a directory ===
Line 137: Line 128:
 
<tt>'''ls'''</tt> displays the names of all files and subdirectories in the working (current) directory.
 
<tt>'''ls'''</tt> displays the names of all files and subdirectories in the working (current) directory.
  
  [jemmyhu@nar316 ~]$ ls
+
  [jemmyhu@saw-login2: ~]$ ls
 
  adf2007.01                          Dakota    mpi              script         
 
  adf2007.01                          Dakota    mpi              script         
 
  adf2007.01.pc64_linux.hpmpi.bin.tar  debugger  Octave_matlab    test_g03
 
  adf2007.01.pc64_linux.hpmpi.bin.tar  debugger  Octave_matlab    test_g03
Line 146: Line 137:
 
<tt>'''ls -l'''</tt> shows the detailed information of the contents (permissions, owners, size, and timestamp).
 
<tt>'''ls -l'''</tt> shows the detailed information of the contents (permissions, owners, size, and timestamp).
  
  [jemmyhu@nar316 ~]$ ls -l
+
  [jemmyhu@saw-login2: ~]$ ls -l
 
  total 352192
 
  total 352192
 
  drwxr-xr-x  8 jemmyhu jemmyhu      4096 Nov 23 09:47 adf2007.01
 
  drwxr-xr-x  8 jemmyhu jemmyhu      4096 Nov 23 09:47 adf2007.01
Line 171: Line 162:
 
<tt>'''ls -a'''</tt> shows all subdirectories and files, even files that are hidden (these files begin with a dot.)
 
<tt>'''ls -a'''</tt> shows all subdirectories and files, even files that are hidden (these files begin with a dot.)
  
  [jemmyhu@nar316 ~]$ ls -a
+
  [jemmyhu@saw-login2: ~]$ ls -a
 
  .                                    Courses          .octave_hist    test_g03             
 
  .                                    Courses          .octave_hist    test_g03             
 
  ..                                  Dakota          Octave_matlab    test_mpiPWSCF  
 
  ..                                  Dakota          Octave_matlab    test_mpiPWSCF  
Line 183: Line 174:
 
  .bashrc                              mpi              test_g03
 
  .bashrc                              mpi              test_g03
 
  bin                                  .nodelist        test_mpiPWSCF
 
  bin                                  .nodelist        test_mpiPWSCF
 
  
 
=== ''mkdir'' - create directory (i.e., a folder) ===
 
=== ''mkdir'' - create directory (i.e., a folder) ===
Line 190: Line 180:
 
   
 
   
 
<tt>my_dir</tt> is the name of a newly created directory, a subdirectory of the current directory.
 
<tt>my_dir</tt> is the name of a newly created directory, a subdirectory of the current directory.
 
  
 
=== ''rmdir'' - remove empty directory ===
 
=== ''rmdir'' - remove empty directory ===
Line 198: Line 187:
  
 
<tt>'''rm -r my_dir'''</tt>
 
<tt>'''rm -r my_dir'''</tt>
 
  
 
=== ''cd'' - change directory ===
 
=== ''cd'' - change directory ===
Line 213: Line 201:
 
|}
 
|}
  
  [jemmyhu@nar317 ~]$ cd <br>  
+
  [jemmyhu@saw-login2: ~]$ cd <br>  
  [jemmyhu@nar317 ~]$ pwd <br>
+
  [jemmyhu@saw-login2: ~]$ pwd <br>
 
  /home/jemmyhu <br>  
 
  /home/jemmyhu <br>  
  [jemmyhu@nar317 ~]$ cd .. <br>
+
  [jemmyhu@saw-login2: ~]$ cd .. <br>
  [jemmyhu@nar317 home]$ pwd <br>
+
  [jemmyhu@saw-login2:/home]$ pwd <br>
 
  /home <br>
 
  /home <br>
  [jemmyhu@nar317 home]$ cd jemmyhu <br>
+
  [jemmyhu@saw-login2:/home]$ cd jemmyhu <br>
  [jemmyhu@nar317 ~]$ pwd <br>
+
  [jemmyhu@saw-login2: ~]$ pwd <br>
 
  /home/jemmyhu
 
  /home/jemmyhu
  
 
 
=== ''more, less'' - display contents of file, page by page ===
 
=== ''more, less'' - display contents of file, page by page ===
  
Line 233: Line 220:
 
* '''press enter''' the next row is displayed
 
* '''press enter''' the next row is displayed
 
* '''press q'''  the command is finished
 
* '''press q'''  the command is finished
 
  
 
=== ''cp'' - copy file ===
 
=== ''cp'' - copy file ===
Line 247: Line 233:
 
| Copy ''DIR'' and all its contents to ''NEWDIR''.
 
| Copy ''DIR'' and all its contents to ''NEWDIR''.
 
|}
 
|}
 
  
 
=== ''mv'' - move or rename file or directory ===
 
=== ''mv'' - move or rename file or directory ===
Line 261: Line 246:
 
| Rename ''DIR'' to ''NEWDIRNAME''
 
| Rename ''DIR'' to ''NEWDIRNAME''
 
|}
 
|}
 
  
 
=== ''rm'' - remove file ===
 
=== ''rm'' - remove file ===
Line 281: Line 265:
 
| Remove ''DIR'' and its entire contents.
 
| Remove ''DIR'' and its entire contents.
 
|}
 
|}
 
  
 
=== ''file'' - determine file type ===
 
=== ''file'' - determine file type ===
 
   
 
   
  [jemmyhu@nar316 work]$ file README
+
  [jemmyhu@saw-login2:/work]$ file README
 
  README: ASCII English text
 
  README: ASCII English text
 
  
 
=== ''diff'' - display the differences between two files ===
 
=== ''diff'' - display the differences between two files ===
Line 295: Line 277:
 
| Show differences between two versions of a file.
 
| Show differences between two versions of a file.
 
|}
 
|}
 
  
 
=== ''find'' - search for files that meet a desired criteria ===
 
=== ''find'' - search for files that meet a desired criteria ===
Line 307: Line 288:
 
|}
 
|}
  
  [jemmyhu@nar316 ~]$ find /usr -name "lpr" -print
+
  [jemmyhu@saw-login2: ~]$ find /usr -name "lpr" -print
 
  /usr/bin/lpr
 
  /usr/bin/lpr
 
  /usr/share/terminfo/l/lpr
 
  /usr/share/terminfo/l/lpr
 
  
 
=== ''ps'' - list processes ===
 
=== ''ps'' - list processes ===
Line 319: Line 299:
 
|}
 
|}
  
  [jemmyhu@nar316 ~]$ ps -u jemmyhu
+
  [jemmyhu@saw-login2: ~]$ ps -u jemmyhu
 
  PID  TTY      TIME    CMD
 
  PID  TTY      TIME    CMD
 
  11437 ?        00:00:00 sshd
 
  11437 ?        00:00:00 sshd
 
  11438 pts/5    00:00:00 bash
 
  11438 pts/5    00:00:00 bash
 
  13861 pts/5    00:00:00 ps
 
  13861 pts/5    00:00:00 ps
 
  
 
=== ''kill'' - stop a process from running ===  
 
=== ''kill'' - stop a process from running ===  
Line 332: Line 311:
 
| Stop a process with process NO.  ''PID'' before it has been finished normally.
 
| Stop a process with process NO.  ''PID'' before it has been finished normally.
 
|}  
 
|}  
 
  
 
=== ''time'' - measure program running time ===
 
=== ''time'' - measure program running time ===
Line 341: Line 319:
 
|}
 
|}
  
  [jemmyhu@nar316 ~]$ time ./pi-serial
+
  [jemmyhu@saw-login2: ~]$ time ./pi-serial
 
  PI = 3.141593
 
  PI = 3.141593
 
   
 
   
Line 347: Line 325:
 
  user 0m0.001s
 
  user 0m0.001s
 
  sys 0m0.002s
 
  sys 0m0.002s
 
  
 
=== ''top'' - list processes running on the system ===
 
=== ''top'' - list processes running on the system ===
  
  [jemmyhu@nar316 ~]$ top
+
  [jemmyhu@saw-login2: ~]$ top
top - 11:46:58 up 32 days, 21:23, 18 users,  load average: 1.00, 1.02, 1.20
+
top - 10:09:50 up 8 days, 15:27, 16 users,  load average: 0.01, 0.06, 0.36
Tasks: 314 total,  2 running, 312 sleeping,  0 stopped,  0 zombie
+
Tasks: 472 total,  1 running, 469 sleeping,  2 stopped,  0 zombie
Cpu(s):  1.0% us,  0.7% sy,  0.0% ni, 98.2% id,  0.0% wa,  0.0% hi,  0.2% si
+
Cpu(s):  0.2%us,  0.2%sy,  0.0%ni, 99.5%id,  0.0%wa,  0.0%hi,  0.0%si, 0.0%st
  Mem:   4037544k total, 4020680k used,   16864k free,    4132k buffers
+
Mem: 16463412k total, 14773748k used, 1689664k free,    6864k buffers
Swap:  6289408k total,   257344k used,  6032064k free, 3162308k cached
+
Swap:  1999988k total,       0k used,  1999988k free, 11396408k cached
+
 
PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                
+
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                
  17862 mjhillie  16   0 13324 1820 1196 0.7 0.0  4:48.16 top                     
+
24801 viwanj14 20   0 119m 4720 1172 1.3 0.0  1:34.14 sshd                   
15146 root      18   0 62636 3692 1476 R  0.7  0.1   0:00.02 auto.home               
+
21032 jemmyhu  20   0 26180 1844 1204 R  0.7  0.0   0:00.14 top                   
  12647 tomek    15   0 46280 2396 1592 S  0.3 0.0:41.05 sshd                    
+
22298 viwanj14 20   0 119m 4620 1172 S  0.7 0.0   2:44.65 sshd                  
  12920 tomek    15   0 46280 2400 1592 S  0.3  0.0:37.94 sshd                   
+
  1779 root      20   0     0    0    0 S  0.3  0.0 55:32.72 ptlrpcd               
  12921 tomek    16   0 25072 5812 1348 S  0.3  0.1  0:07.58 scp                     
+
  2158 nobody    20   0 332m 192m 1156 S  0.3  1.2  88:07.71 gmond                 
  15134 jemmyhu  16   0 13196 2044 1356 R 0.3  0.1   0:00.37 top
+
24806 viwanj14 20   0 38180 3424 1040 S 0.3  0.0   0:11.72 scp                   
    1 root      16   0  4748 404  372 S  0.0  0.0  0:06.60 init
+
    1 root      20   0 21436 1516 1196 S 0.0 0.0  0:01.51 init                   
 +
    2 root      20  0    0    0    0 S  0.0  0.0  0:00.10 kthreadd
  
 
Enter '''q'' to stop 'top' command
 
Enter '''q'' to stop 'top' command
 
  
 
=== ''which'' - locate a program file in user's path ===
 
=== ''which'' - locate a program file in user's path ===
  [jemmyhu@nar316 ~]$ which cc
+
  [jemmyhu@saw-login2: ~]$ which cc
  /opt/sharcnet/compile/bin/cc
+
  /opt/sharcnet/intel/12.1.3/snbin/cc
  [jemmyhu@nar316 ~]$ which f90
+
  [jemmyhu@saw-login2: ~]$ which f90
  /opt/sharcnet/compile/bin/f90
+
  /opt/sharcnet/intel/12.1.3/snbin/f90
  [jemmyhu@nar316 ~]$ which mpicc
+
  [jemmyhu@saw-login2:~] which mpicc
  /opt/sharcnet/compile/bin/mpicc
+
  /opt/sharcnet/openmpi/1.6.2/intel/bin/mpicc
 
+
  
 
=== ''man'' - help manual ===
 
=== ''man'' - help manual ===
  
  [jemmyhu@nar316 ~]$ man sqjobs
+
  [jemmyhu@saw-login2: ~]$ man sqjobs
 
  SQJOBS(1)          User Contributed Perl Documentation            SQJOBS(1)
 
  SQJOBS(1)          User Contributed Perl Documentation            SQJOBS(1)
 
   
 
   
Line 408: Line 384:
  
 
Enter '''q''' to stop 'man' command
 
Enter '''q''' to stop 'man' command
 
  
 
=== ''df'' - display free disk space ===
 
=== ''df'' - display free disk space ===
  
  [jemmyhu@nar316 ~]$ df
+
  [jemmyhu@saw-login2: ~]$ df
 
  Filesystem          1K-blocks      Used Available Use% Mounted on
 
  Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/sda2            42441844  3662780  36623144  10% /
+
rootfs               23441665792 6615963896 16825701896 29% /
/dev/sda1               703596    31708    636148  5% /boot
+
10.28.1.136:/var/lib/oneSIS/image
  none                  2018772        0  2018772  0% /dev/shm
+
                    23441665792 6615963896 16825701896 29% /
/dev/sda3            27585276    966940  25217068  4% /var
+
/dev/ram                102400    57424    44976 57% /ram
10.33.1.80:/hptc_cluster
+
/dev/cciss/c0d0p2    139053828    165624 131824668   1% /local
                      13938720  7976032  5254624  61% /hptc_cluster
+
none                  8228360      168   8228192  1% /dev
3712500403@gm0,3712500351@gm0:/nar-sfs-mds3/client_gm
+
tmpfs                  8231704        0  8231704  0% /dev/shm
                      74565613440 41282337520 29524984824 59% /nar_sfs
+
saw-admin:/saw_local 54558912 48159232  3583488 94% /saw_local
10.33.1.64:/nar_home 1441960384 77531840 1291178848  6% /nar_home
+
......
10.33.1.64:/nar_data 480668576  10531008 445720992   3% /nar_data
+
nar_home.narwhal.sharcnet:/nar_home/mjhillie
+
                      1441960360  77531832 1291178832   6% /home/mjhillie
+
bull_home.bull.sharcnet:/bull_home/zbice
+
                      1441960360 250717016 1117993648  19% /home/zbice
+
bull_home.bull.sharcnet:/bull_home/mhashimo
+
                      1441960360 250717016 1117993648 19% /home/mhashimo
+
  10.20.0.126:/bull_home/jemmyhu
+
                      1441960360 250717016 1117993648 19% /home/jemmyhu
+
10.20.0.126:/bull_home/kowalskm
+
                      1441960360 250717016 1117993648  19% /home/kowalskm
+
bull_home.bull.sharcnet:/bull_home/tomek
+
                      1441960360 250717016 1117993648  19% /home/tomek
+
wha_home.whale.sharcnet:/wha_home/wangh29
+
                      1441960360  99059832 1269650832  8% /home/wangh29
+
 
+
 
+
  
 
=== ''du'' - display file/directory disk usage (in KB) ===
 
=== ''du'' - display file/directory disk usage (in KB) ===
Line 448: Line 406:
 
|}
 
|}
  
  [jemmyhu@nar316 ~]$ pwd
+
  [jemmyhu@saw-login2: ~]$ pwd
 
  /home/jemmyhu
 
  /home/jemmyhu
  [jemmyhu@nar316 ~]$ ls
+
  [jemmyhu@saw-login2: ~]$ ls
 
  adf2007.01                          Dakota    mpi              script         
 
  adf2007.01                          Dakota    mpi              script         
 
  adf2007.01.pc64_linux.hpmpi.bin.tar  debugger  Octave_matlab    test_g03
 
  adf2007.01.pc64_linux.hpmpi.bin.tar  debugger  Octave_matlab    test_g03
Line 457: Line 415:
 
  Courses                              Matlab    scratch          work
 
  Courses                              Matlab    scratch          work
 
   
 
   
  [jemmyhu@nar316 ~]$ du mpi
+
  [jemmyhu@saw-login2: ~]$ du mpi
 
  3896    mpi/mpi-2/gulper
 
  3896    mpi/mpi-2/gulper
 
  3944    mpi/mpi-2
 
  3944    mpi/mpi-2
 
  4160    mpi
 
  4160    mpi
 
   
 
   
  [jemmyhu@nar316 ~]$ du -sh mpi
+
  [jemmyhu@saw-login2: ~]$ du -sh mpi
 
  4.1M    mpi
 
  4.1M    mpi
 
  
 
== File Permissions ==
 
== File Permissions ==
Line 477: Line 434:
 
|}
 
|}
 
   
 
   
 
 
* Operator <font color='blue'>'''='''</font> is used to set up permissions
 
* Operator <font color='blue'>'''='''</font> is used to set up permissions
 
* To add permissions, use <font color='blue'>'''+'''</font>
 
* To add permissions, use <font color='blue'>'''+'''</font>
 
* To take away permissions, use <font color='blue'>'''-'''</font>  
 
* To take away permissions, use <font color='blue'>'''-'''</font>  
 
  
 
'''''who''''' is a list of letters that specifies to whom you are going to be giving permissions.  These may be specified in any order.
 
'''''who''''' is a list of letters that specifies to whom you are going to be giving permissions.  These may be specified in any order.
Line 500: Line 455:
 
| <font color='blue'>'''a'''</font>ll of the above (an abbreviation for ugo)
 
| <font color='blue'>'''a'''</font>ll of the above (an abbreviation for ugo)
 
|}
 
|}
 
  
 
'''''permissions''''' are the same letters that you see in the directory listing (<font color='blue'>'''ls -l'''</font>).  
 
'''''permissions''''' are the same letters that you see in the directory listing (<font color='blue'>'''ls -l'''</font>).  
Line 513: Line 467:
 
| permission to e<font color='blue'>'''x'''</font>ecute the file
 
| permission to e<font color='blue'>'''x'''</font>ecute the file
 
|}
 
|}
 
  
 
<font color='blue'>'''EXAMPLES:'''</font>
 
<font color='blue'>'''EXAMPLES:'''</font>
  
  [jemmyhu@nar316 work]$ ls -l README
+
  [jemmyhu@saw-login2:/work]$ ls -l README
 
  -rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
  
Line 526: Line 479:
 
Here are some examples to change the permissions on file 'README':
 
Here are some examples to change the permissions on file 'README':
  
  [jemmyhu@nar316 work]$ ls -l README        #View the original permissions on file 'README'
+
  [jemmyhu@saw-login2:/work]$ ls -l README        #View the original permissions on file 'README'
 
  -rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
    
 
    
  [jemmyhu@nar316 work]$ chmod ugo=rw README  #Give read and write permissions to all
+
  [jemmyhu@saw-login2:/work]$ chmod ugo=rw README  #Give read and write permissions to all
 
   
 
   
  [jemmyhu@nar316 work]$ ls -l README
+
  [jemmyhu@saw-login2:/work]$ ls -l README
 
  -rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
   
 
   
  [jemmyhu@nar316 work]$ chmod ug+x README    #Add execute permission to user and group
+
  [jemmyhu@saw-login2:/work]$ chmod ug+x README    #Add execute permission to user and group
 
  [jemmyhu@nar316 work]$ ls -l README
 
  [jemmyhu@nar316 work]$ ls -l README
 
  -rwxrwxrw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rwxrwxrw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
   
 
   
  [jemmyhu@nar316 work]$ chmod ug-x README    #Take away execute permission from user and group
+
  [jemmyhu@saw-login2:/work]$ chmod ug-x README    #Take away execute permission from user and group
  [jemmyhu@nar316 work]$ ls -l README
+
  [jemmyhu@saw-login2:/work]$ ls -l README
 
  -rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
   
 
   
  [jemmyhu@nar316 work]$ chmod a-w README    #Take away write permission for all
+
  [jemmyhu@saw-login2:/work]$ chmod a-w README    #Take away write permission for all
 
  [jemmyhu@nar316 work]$ ls -l README
 
  [jemmyhu@nar316 work]$ ls -l README
 
  -r--r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -r--r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
   
 
   
  [jemmyhu@nar316 work]$ chmod u+w README    #Add write permission to user
+
  [jemmyhu@saw-login2:/work]$ chmod u+w README    #Add write permission to user
 
  [jemmyhu@nar316 work]$ ls -l README
 
  [jemmyhu@nar316 work]$ ls -l README
 
  -rw-r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  -rw-r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
  
 
== Other Important Linux Commands ==
 
== Other Important Linux Commands ==
Line 567: Line 519:
  
 
For a list of Linux commands, please see [http://www.ss64.com/bash/ Linux Bash Commands].
 
For a list of Linux commands, please see [http://www.ss64.com/bash/ Linux Bash Commands].
 
  
 
== What is vi? ==
 
== What is vi? ==
Line 594: Line 545:
 
The screen looks like:
 
The screen looks like:
 
  ~
 
  ~
 
  
 
* <font color='blue'>'''Exit vi'''</font>
 
* <font color='blue'>'''Exit vi'''</font>
Line 615: Line 565:
 
| exit vi, without saving the modified file
 
| exit vi, without saving the modified file
 
|}
 
|}
 
  
 
* <font color='blue'>'''Insert or Add Text'''</font>
 
* <font color='blue'>'''Insert or Add Text'''</font>
Line 633: Line 582:
 
| open a new line below current line, until '''Esc''' key is pressed
 
| open a new line below current line, until '''Esc''' key is pressed
 
|}
 
|}
 
  
 
* <font color='blue'>'''Cursor Movement Commands'''</font>
 
* <font color='blue'>'''Cursor Movement Commands'''</font>
Line 653: Line 601:
 
| Cursor is moved one line up  
 
| Cursor is moved one line up  
 
|}
 
|}
 
  
 
* <font color='blue'>'''Change Text'''</font>
 
* <font color='blue'>'''Change Text'''</font>
Line 666: Line 613:
 
| Overwrite text until the keystroke of Esc
 
| Overwrite text until the keystroke of Esc
 
|}
 
|}
 
  
 
* <font color='blue'>'''Delete Text'''</font>
 
* <font color='blue'>'''Delete Text'''</font>
Line 682: Line 628:
 
| delete N lines, beginning with the current line
 
| delete N lines, beginning with the current line
 
|}
 
|}
 
  
 
* <font color='blue'>'''Cut and Paste Text'''</font>
 
* <font color='blue'>'''Cut and Paste Text'''</font>
Line 698: Line 643:
 
| paste the copied line(s) into the text after the current line
 
| paste the copied line(s) into the text after the current line
 
|}
 
|}
 
  
 
* <font color='blue'>'''Screen Manipulation'''</font>
 
* <font color='blue'>'''Screen Manipulation'''</font>
Line 718: Line 662:
 
| move up (back) one half screen  
 
| move up (back) one half screen  
 
|}
 
|}
 
  
 
* <font color='blue'>'''Search and Replace Text'''</font>
 
* <font color='blue'>'''Search and Replace Text'''</font>
Line 738: Line 681:
 
| search 'oldstring' and replace it with 'newstring' in the entire file  
 
| search 'oldstring' and replace it with 'newstring' in the entire file  
 
|}
 
|}
 
  
 
* <font color='blue'>'''Determine Line Number''' </font>
 
* <font color='blue'>'''Determine Line Number''' </font>
Line 754: Line 696:
 
| provides the line number for the entire file
 
| provides the line number for the entire file
 
|}
 
|}
 
  
 
== Further Reading ==
 
== Further Reading ==
 
* '''''SAMS Teach Yourself Unix in 24 Hours (3e)''.  Sams Publishing, 2001.'''
 
* [http://www.fsid.cvut.cz/cz/U201/LINUX.HTML http://www.fsid.cvut.cz/cz/U201/LINUX.HTML]
 
** Quick Linux Tutorial, by Jiri Vogel
 
 
* [http://faculty.plattsburgh.edu/jan.plaza/computing/help/commands.html http://faculty.plattsburgh.edu/jan.plaza/computing/help/commands.html]
 
* [http://faculty.plattsburgh.edu/jan.plaza/computing/help/commands.html http://faculty.plattsburgh.edu/jan.plaza/computing/help/commands.html]
** Basic Unix Commands
 
 
* [http://www.ss64.com/bashsyntax/vi.html http://www.ss64.com/bashsyntax/vi.html]
 
* [http://www.ss64.com/bashsyntax/vi.html http://www.ss64.com/bashsyntax/vi.html]
** vi Editor Commands
 
 
* [http://www.cs.colostate.edu/helpdocs/vi.html http://www.cs.colostate.edu/helpdocs/vi.html]
 
* [http://www.cs.colostate.edu/helpdocs/vi.html http://www.cs.colostate.edu/helpdocs/vi.html]
** Basic vi Commands
+
 
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 13:45, 16 November 2017

Objectives

This self-directed tutorial is mainly for people who have none or very little experience in Linux system, particularly for Windows users who want to migrate to SHARCNET Linux systems. Examples shown in the lightgrey areas were run on 'narwhal', a SHARCNET cluster. We hope that this tutorial would be a good starting point for newcomers to Linux.

What is Linux?

Linux is a free Unix-type operating system originally created by Linus Torvalds with the assistance of developers around the world in the 90's. Linux is becoming popular as a powerful, low-cost operating system for cluster servers.

Linux Features

  • multitasking: several programs running at once
  • multiusers: several users at the same machine at once
  • multiplatforms: it runs on many different CPU

Directory Structure in Linux

This is not a complete list, but the most general directories on Linux system.

Directory Meaning
/bin Directory for system command
/dev Directory with special files which enable to work with pheripheral devices
/etc System programs and data
/lib Libraries that are needed to execute the binaries in /bin/ and /sbin/
/mnt Directory for temporarily mounted file systems
/tmp Directory for temporary data sets
/usr Other system programs
/var Files which are being updated during system running
/opt Directory for large, static application software packages
/home User's home directory (quota of 10GB). User has a common home directory on SHARCNET systems
/work work directory, with a quota of 1TB on some large SHARCNET systems
/scratch scratch directory on SHARCNET systems with an expiry time of 2 months

Getting Started

Let's try a few simple commands

  • login
    • Use SSH (see our SSH page) to login onto SHARCNET linux systems
  • hostname: Print system name
[jemmyhu@saw-login2: ~]$ hostname
saw-login2
  • who: Print all usernames currently logged in
[jemmyhu@saw-login2: ~]$ who
inta     pts/0        Nov 29 12:10 (pharmtech4.pharm.helsinki.fi)
jemmyhu  pts/1        Nov 29 09:33 (jemmyhu.uwaterloo.ca)
jemmyhu  pts/2        Nov 29 09:34 (jemmyhu.uwaterloo.ca)
tomek    pts/3        Nov 19 10:00 (lola.hut.fi)
merz     pts/5        Nov 29 12:52 (nar317)
michaelg pts/4        Nov  2 12:47 (pc-mat1.eng.mcmaster.ca)
viv      pts/6        Nov 26 19:12 (dirac.physics.uoguelph.ca)
mhashimo pts/7        Nov 26 17:21 (lab2.apmaths.uwo.ca)
msammalk pts/8        Nov 29 11:13 (mae-msammalk.princeton.edu)
sclee    pts/9        Nov 29 14:37 (sclee.uwaterloo.ca)
merz     pts/10       Nov 29 14:41 (scinaa61.uwaterloo.ca)
mjhillie pts/12       Nov 27 10:56 (hillier.uwaterloo.ca)
zzheng   pts/14       Nov 27 11:49 (meg34.megaladon.sharcnet)
  • whoami: Print the current username
[jemmyhu@saw-login2: ~]$ whoami
jemmyhu
  • pwd: Print working (current) directory
[jemmyhu@saw-login2: ~]$ pwd
/home/jemmyhu
  • date: Display the date & time
[jemmyhu@saw-login2: ~]$ date
Mon Feb  3 10:06:09 EST 2014
  • exit: or logout to leave the system

Changing Password

To change your Linux account password, use the command passwd

[jemmyhu@saw-login2: ~]$ passwd
Changing password for user jemmyhu.
Changing password for jemmyhu
(current) UNIX password:
(You will write your actual password after colon.)
New password:
(You will write your new password after colon.)
Re-type new password:
(You will repeat your new password after colon.)
Password changed

if you type your old password and a new password correctly, the new one becomes valid.

NOTE: you can change your SHARCNET password on SHARCNET webportal.

Basic Linux Commands

The basic format of a command is command [option] ..., please check the command's manpage (man command) for other options.

ls - list the contents of a directory

ls displays the names of all files and subdirectories in the working (current) directory.

[jemmyhu@saw-login2: ~]$ ls
adf2007.01                           Dakota     mpi              script         
adf2007.01.pc64_linux.hpmpi.bin.tar  debugger   Octave_matlab    test_g03
awk_example                          fftw_test  OpenMP_Summer07  test_mpiPWSCF
bin                                  HU         overLord         util
Courses                              Matlab     scratch          work

ls -l shows the detailed information of the contents (permissions, owners, size, and timestamp).

[jemmyhu@saw-login2: ~]$ ls -l
total 352192
drwxr-xr-x   8 jemmyhu jemmyhu      4096 Nov 23 09:47 adf2007.01
-rw-rw-r--   1 jemmyhu jemmyhu 360212480 Nov 12 13:10 adf2007.01.pc64_linux.hpmpi.bin.tar
drwxr-x---   2 jemmyhu jemmyhu      4096 Apr 30  2007 awk_example
drwxr-xr-x   2 jemmyhu jemmyhu      4096 Nov  9 12:14 bin
drwx------   3 jemmyhu jemmyhu      4096 Apr 12  2007 Courses
drwxr-xr-x   4 jemmyhu jemmyhu      4096 May 29  2007 Dakota
drwx------   3 jemmyhu jemmyhu      4096 Aug 22 15:39 debugger
drwxr-x---   3 jemmyhu jemmyhu      4096 May  7  2007 fftw_test
drwx------  13 jemmyhu jemmyhu      4096 Oct 31 12:08 HU
drwxrwxr-x   2 jemmyhu jemmyhu      4096 Oct 31 11:52 Matlab
drwx------   3 jemmyhu jemmyhu      4096 Oct 30 10:57 mpi
drwx------   2 jemmyhu jemmyhu      4096 Aug 24 15:31 Octave_matlab
drwx------   6 jemmyhu jemmyhu      4096 Jun 13 11:06 OpenMP_Summer07
drwx------   2 jemmyhu jemmyhu      4096 Oct 31 11:54 overLord
lrwxrwxrwx   1 jemmyhu jemmyhu        16 Aug  1 11:22 scratch -> /scratch/jemmyhu
drwx------   2 jemmyhu jemmyhu      4096 Nov 21 12:14 script
drwx------   2 jemmyhu jemmyhu      4096 Nov  6 15:29 test_g03
drwx------   7 jemmyhu jemmyhu      4096 Jun  7 21:34 test_mpiPWSCF
drwx------   2 jemmyhu jemmyhu      4096 Oct 31 11:58 util
lrwxrwxrwx   1 jemmyhu jemmyhu        13 Aug  1 11:21 work -> /work/jemmyhu

ls -a shows all subdirectories and files, even files that are hidden (these files begin with a dot.)

[jemmyhu@saw-login2: ~]$ ls -a
.                                    Courses          .octave_hist     test_g03             
..                                   Dakota           Octave_matlab    test_mpiPWSCF 
adf2007.01                           .ddt             OpenMP_Summer07  util             
adf2007.01.pc64_linux.hpmpi.bin.tar  .ddt_session     .pathscale       .viminfz.tmp 
.assistant                           debugger         .rpmmacros       work
awk_example                          fftw_test        scratch          .Xauthority
.bash_history                        HU               script    
.bash_logout                         .lsbatch         .sq            
.bash_profile                        Matlab           .ssh         
.bashrc                              mpi              test_g03
bin                                  .nodelist        test_mpiPWSCF

mkdir - create directory (i.e., a folder)

mkdir my_dir

my_dir is the name of a newly created directory, a subdirectory of the current directory.

rmdir - remove empty directory

rmdir my_dir

remove my_dir if it is empty. If my_dir is not empty, use

rm -r my_dir

cd - change directory

cd change to your home directory
cd .. change to the parent directory
cd directory change to directory
[jemmyhu@saw-login2: ~]$ cd 
[jemmyhu@saw-login2: ~]$ pwd
/home/jemmyhu
[jemmyhu@saw-login2: ~]$ cd ..
[jemmyhu@saw-login2:/home]$ pwd
/home
[jemmyhu@saw-login2:/home]$ cd jemmyhu
[jemmyhu@saw-login2: ~]$ pwd
/home/jemmyhu

more, less - display contents of file, page by page

more file less file

User can controle the output:

  • press space the next screen is displayed
  • press enter the next row is displayed
  • press q the command is finished

cp - copy file

cp file NEWFILE Copy file to NEWFILE in the same directory.
cp /home/jemmyhu/file . Copy file from /home/jemmyhu to the current directory.
cp -r DIR NEWDIR Copy DIR and all its contents to NEWDIR.

mv - move or rename file or directory

mv File NEWNAME Rename FILE to NEWNAME in the same directory.
mv File DIR Move FILE to existing directory DIR.
mv DIR NEWDIRNAME Rename DIR to NEWDIRNAME

rm - remove file

rm FILE Remove FILE in the current directory.
rm b*p Remove all files beginning with b and ending with p in the current directory.
rm * Remove all files in the current directory
rm -i * the system will ask for permission before removing each file.
rm -r DIR Remove DIR and its entire contents.

file - determine file type

[jemmyhu@saw-login2:/work]$ file README
README: ASCII English text

diff - display the differences between two files

diff File_1 File_2 Show differences between two versions of a file.

find - search for files that meet a desired criteria

find FILE(S) Search current dir and its subdirs for FILE(S).
find /usr -name "lpr" -print Find a file lpr in directory /usr and its subdirectories.
[jemmyhu@saw-login2: ~]$ find /usr -name "lpr" -print
/usr/bin/lpr
/usr/share/terminfo/l/lpr

ps - list processes

ps -u username List all your processes.
[jemmyhu@saw-login2: ~]$ ps -u jemmyhu
PID   TTY       TIME    CMD
11437 ?        00:00:00 sshd
11438 pts/5    00:00:00 bash
13861 pts/5    00:00:00 ps

kill - stop a process from running

kill -9 PID Stop a process with process NO. PID before it has been finished normally.

time - measure program running time

time executablecode Display the time used to run executablecode.
[jemmyhu@saw-login2: ~]$ time ./pi-serial
PI = 3.141593

real 0m0.002s
user 0m0.001s
sys 0m0.002s

top - list processes running on the system

[jemmyhu@saw-login2: ~]$ top

top - 10:09:50 up 8 days, 15:27, 16 users, load average: 0.01, 0.06, 0.36 Tasks: 472 total, 1 running, 469 sleeping, 2 stopped, 0 zombie Cpu(s): 0.2%us, 0.2%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 16463412k total, 14773748k used, 1689664k free, 6864k buffers Swap: 1999988k total, 0k used, 1999988k free, 11396408k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                 

24801 viwanj14 20 0 119m 4720 1172 S 1.3 0.0 1:34.14 sshd 21032 jemmyhu 20 0 26180 1844 1204 R 0.7 0.0 0:00.14 top 22298 viwanj14 20 0 119m 4620 1172 S 0.7 0.0 2:44.65 sshd

1779 root      20   0     0    0    0 S  0.3  0.0  55:32.72 ptlrpcd                 
2158 nobody    20   0  332m 192m 1156 S  0.3  1.2  88:07.71 gmond                   

24806 viwanj14 20 0 38180 3424 1040 S 0.3 0.0 0:11.72 scp

   1 root      20   0 21436 1516 1196 S  0.0  0.0   0:01.51 init                    
   2 root      20   0     0    0    0 S  0.0  0.0   0:00.10 kthreadd

Enter 'q to stop 'top' command

which - locate a program file in user's path

[jemmyhu@saw-login2: ~]$ which cc
/opt/sharcnet/intel/12.1.3/snbin/cc
[jemmyhu@saw-login2: ~]$ which f90
/opt/sharcnet/intel/12.1.3/snbin/f90
[jemmyhu@saw-login2:~] which mpicc
/opt/sharcnet/openmpi/1.6.2/intel/bin/mpicc

man - help manual

[jemmyhu@saw-login2: ~]$ man sqjobs
SQJOBS(1)          User Contributed Perl Documentation             SQJOBS(1)

NAME
           sqjobs - show SQ jobs

SYNOPSIS
           sqjobs [-r][-q][-z][-v][-u user][-n][--summary][jobid...]

         options:
           -a or --all show all jobs: all users and all states
           -r          show running jobs
           -q          show queued jobs
           -z          show suspended/preempted jobs
           -l          show more detail on jobs
           -L          show more detail on jobs
           -u user     show jobs for the given user
           -n, --none  show one-line summary of cluster
           -s or --summary   show a line-per-user summary of all jobs
           -h or --help        show usage
           --man               show man page
           jobid...    one or more jobids to examine

DESCRIPTION
       sqjobs displays a succinct tabular listing of jobs in the SQ system.

Enter q to stop 'man' command

df - display free disk space

[jemmyhu@saw-login2: ~]$ df
Filesystem           1K-blocks      Used Available Use% Mounted on

rootfs 23441665792 6615963896 16825701896 29% / 10.28.1.136:/var/lib/oneSIS/image

                    23441665792 6615963896 16825701896  29% /

/dev/ram 102400 57424 44976 57% /ram /dev/cciss/c0d0p2 139053828 165624 131824668 1% /local none 8228360 168 8228192 1% /dev tmpfs 8231704 0 8231704 0% /dev/shm saw-admin:/saw_local 54558912 48159232 3583488 94% /saw_local ......

du - display file/directory disk usage (in KB)

du -sh DIRECTORY Display only the total disk usage of DIRECTORY (in MB).
[jemmyhu@saw-login2: ~]$ pwd
/home/jemmyhu
[jemmyhu@saw-login2: ~]$ ls
adf2007.01                           Dakota     mpi              script        
adf2007.01.pc64_linux.hpmpi.bin.tar  debugger   Octave_matlab    test_g03
awk_example                          fftw_test  OpenMP_Summer07  test_mpiPWSCF
bin                                  HU         overLord         util 
Courses                              Matlab     scratch          work

[jemmyhu@saw-login2: ~]$ du mpi
3896    mpi/mpi-2/gulper
3944    mpi/mpi-2
4160    mpi

[jemmyhu@saw-login2: ~]$ du -sh mpi
4.1M    mpi

File Permissions

  • chmod - change file/directory access permissions
chmod who=permissions Filename Gives who the specified permissions for a given Filename.
chmod who=permissions DIRECTORY Gives who the specified permissions for a given DIRECTORY.
  • Operator = is used to set up permissions
  • To add permissions, use +
  • To take away permissions, use -

who is a list of letters that specifies to whom you are going to be giving permissions. These may be specified in any order.

Letter Meaning
u user who owns the file
g group the file belongs to
o other users
a all of the above (an abbreviation for ugo)

permissions are the same letters that you see in the directory listing (ls -l).

r permission to read the file
w permission to write (or delete) the file
x permission to execute the file

EXAMPLES:

[jemmyhu@saw-login2:/work]$ ls -l README
-rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

The letters written from the 2nd to the 10th column are for file permissions. The first three columns are permissions for the user, the columns 5, 6, 7 for the user's group, and the last three for other users.

In the above example, user (jemmyhu) has read and write permissions on file 'README', but no permissions for the group and others.

Here are some examples to change the permissions on file 'README':

[jemmyhu@saw-login2:/work]$ ls -l README         #View the original permissions on file 'README'
-rw-------  1 jemmyhu jemmyhu 342 Nov  7 14:17 README
 
[jemmyhu@saw-login2:/work]$ chmod ugo=rw README  #Give read and write permissions to all

[jemmyhu@saw-login2:/work]$ ls -l README
-rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

[jemmyhu@saw-login2:/work]$ chmod ug+x README    #Add execute permission to user and group
[jemmyhu@nar316 work]$ ls -l README
-rwxrwxrw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

[jemmyhu@saw-login2:/work]$ chmod ug-x README    #Take away execute permission from user and group
[jemmyhu@saw-login2:/work]$ ls -l README
-rw-rw-rw-  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

[jemmyhu@saw-login2:/work]$ chmod a-w README     #Take away write permission for all
[jemmyhu@nar316 work]$ ls -l README
-r--r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

[jemmyhu@saw-login2:/work]$ chmod u+w README     #Add write permission to user
[jemmyhu@nar316 work]$ ls -l README
-rw-r--r--  1 jemmyhu jemmyhu 342 Nov  7 14:17 README

Other Important Linux Commands

  • alias - create an alias
  • awk - find and replace text
  • cat - display a short file, or used to append files
  • echo - display message on screen
  • env - environment variables
  • grep - search file(s) for lines that match a given pattern
  • gzip/gunzip - compress or decompress named file(s)
  • make - recompile a group of programs
  • quota - display disk usage and limits
  • tar - Tape ARchiver
  • touch - create an empty file or change file timestamps
  • wc - print byte, word, and line counts

For a list of Linux commands, please see Linux Bash Commands.

What is vi?

vi (visual editor) is the default editor that comes with the UNIX/Linux operating system. Alternate editors for UNIX environments include nano (formerly named pico) and emacs (for basic emacs commands, see: http://www.cs.colostate.edu/helpdocs/emacs.html).

The vi editor is a full screen editor and has two modes of operation:

  1. Command mode commands which cause action to be taken on the file, and
  2. Insert mode in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the Esc key turns off the Insert mode. While there are a number of vi commands, just a handful of these are usually sufficient for beginning vi users.

Both UNIX/Linux and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.

Basic vi Commands

  • Start vi
vi Filename - Open Filename in command mode

If Filename exists, the file will be displayed on the screen. If Filename does not exist, then an empty file is created for entering text.

The screen looks like:

~
  • Exit vi

In the following, [Enter] means press Enter key after the keystrokes.

Keystrokes Meaning
:x [Enter] exit vi, save the modified file to file named
:wq [Enter] exit vi, save the modified file to file named
:q [Enter] exit vi
:q! [Enter] exit vi, without saving the modified file
  • Insert or Add Text

The following keystroke will lead to insert mode for inserting and adding text. Esc key must be pressed to terminate the insert mode.

Keystroke Meaning
i insert text before cursor, until Esc key is pressed
a append text after cursor, until Esc key is pressed
o open a new line below current line, until Esc key is pressed
  • Cursor Movement Commands

In the command mode, move along the cursor on the screen.

Keystroke Meaning
h Cursor is moved one space to the left
l Cursor is moved one space to the right
j Cursor is moved one line down
k Cursor is moved one line up
  • Change Text
Keystroke Meaning
r Replace one character over the cursor
R Overwrite text until the keystroke of Esc
  • Delete Text
Keystroke Meaning
x delete single character under cursor
dd delete the current line
Ndd delete N lines, beginning with the current line
  • Cut and Paste Text
Keystroke Meaning
yy copy (yank, cut) the current line into the buffer
Nyy copy (yank, cut) the next N lines into the buffer
p paste the copied line(s) into the text after the current line
  • Screen Manipulation

In the table below, the symbol ^ before a letter means that the Ctrl key should be held down while the letter key is pressed.

Keystroke Meaning
^f move down (forward) one screen
^b move up (backward) one screen
^d move down (forward) one half screen
^u move up (back) one half screen
  • Search and Replace Text

A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands:

Keystroke(s) Meaning
/string [Enter] search forward for occurrence of 'string' in text
?string [Enter] search backward for occurrence of 'string' in text
n [Enter] move to next occurrence of search string
:%s/oldstring/newstring/cg [Enter] search 'oldstring' and replace it with 'newstring' in the entire file
  • Determine Line Number
Keystroke(s) Meaning
:.= [Enter] returns line number of current line at bottom of screen
:= [Enter] returns the total number of lines at bottom of screen
:set number [Enter] provides the line number for the entire file

Further Reading