(Update SSH link) |
|||
(9 intermediate revisions by 6 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. 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 | + | | work directory, with a quota of 1TB on some large '''SHARCNET systems''' |
|- | |- | ||
| '''/scratch''' | | '''/scratch''' | ||
− | | scratch directory on '''SHARCNET systems''' with an expiry time of 2 | + | | scratch directory on '''SHARCNET systems''' with an expiry time of 2 months |
|} | |} | ||
− | |||
== Getting Started == | == Getting Started == | ||
Line 67: | Line 65: | ||
Let's try a few simple commands | Let's try a few simple commands | ||
* <font color='blue'>'''login'''</font> | * <font color='blue'>'''login'''</font> | ||
− | ** Use SSH (see our [[ | + | ** 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@ | + | [jemmyhu@saw-login2: ~]$ hostname |
− | + | 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@ | + | [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 91: | 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@ | + | [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@ | + | [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@ | + | [jemmyhu@saw-login2: ~]$ date |
− | Mon | + | 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 113: | 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@ | + | [jemmyhu@saw-login2: ~]$ passwd |
Changing password for user jemmyhu. | Changing password for user jemmyhu. | ||
Changing password for jemmyhu | Changing password for jemmyhu | ||
Line 127: | 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 138: | 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@ | + | [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 147: | 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@ | + | [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 172: | 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@ | + | [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 184: | 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 191: | 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 199: | Line 187: | ||
<tt>'''rm -r my_dir'''</tt> | <tt>'''rm -r my_dir'''</tt> | ||
− | |||
=== ''cd'' - change directory === | === ''cd'' - change directory === | ||
Line 214: | Line 201: | ||
|} | |} | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ cd <br> |
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ pwd <br> |
/home/jemmyhu <br> | /home/jemmyhu <br> | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ cd .. <br> |
− | [jemmyhu@ | + | [jemmyhu@saw-login2:/home]$ pwd <br> |
/home <br> | /home <br> | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2:/home]$ cd jemmyhu <br> |
− | [jemmyhu@ | + | [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 234: | 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 248: | 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 262: | Line 246: | ||
| Rename ''DIR'' to ''NEWDIRNAME'' | | Rename ''DIR'' to ''NEWDIRNAME'' | ||
|} | |} | ||
− | |||
=== ''rm'' - remove file === | === ''rm'' - remove file === | ||
Line 282: | Line 265: | ||
| Remove ''DIR'' and its entire contents. | | Remove ''DIR'' and its entire contents. | ||
|} | |} | ||
− | |||
=== ''file'' - determine file type === | === ''file'' - determine file type === | ||
− | [jemmyhu@ | + | [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 296: | 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 308: | Line 288: | ||
|} | |} | ||
− | [jemmyhu@ | + | [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 320: | Line 299: | ||
|} | |} | ||
− | [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 333: | 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 342: | Line 319: | ||
|} | |} | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ time ./pi-serial |
PI = 3.141593 | PI = 3.141593 | ||
Line 348: | 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@ | + | [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: | + | 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 | 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@ | + | [jemmyhu@saw-login2: ~]$ which cc |
− | /opt/sharcnet/ | + | /opt/sharcnet/intel/12.1.3/snbin/cc |
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ which f90 |
− | /opt/sharcnet/ | + | /opt/sharcnet/intel/12.1.3/snbin/f90 |
− | [jemmyhu@ | + | [jemmyhu@saw-login2:~] which mpicc |
− | /opt/sharcnet/ | + | /opt/sharcnet/openmpi/1.6.2/intel/bin/mpicc |
− | + | ||
=== ''man'' - help manual === | === ''man'' - help manual === | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ man sqjobs |
SQJOBS(1) User Contributed Perl Documentation SQJOBS(1) | SQJOBS(1) User Contributed Perl Documentation SQJOBS(1) | ||
Line 409: | 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@ | + | [jemmyhu@saw-login2: ~]$ df |
Filesystem 1K-blocks Used Available Use% Mounted on | 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'' - display file/directory disk usage (in KB) === | ||
Line 449: | Line 406: | ||
|} | |} | ||
− | [jemmyhu@ | + | [jemmyhu@saw-login2: ~]$ pwd |
/home/jemmyhu | /home/jemmyhu | ||
− | [jemmyhu@ | + | [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 458: | Line 415: | ||
Courses Matlab scratch work | Courses Matlab scratch work | ||
− | [jemmyhu@ | + | [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@ | + | [jemmyhu@saw-login2: ~]$ du -sh mpi |
4.1M mpi | 4.1M mpi | ||
− | |||
== File Permissions == | == File Permissions == | ||
Line 478: | 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 501: | 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 514: | 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@ | + | [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 527: | 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@ | + | [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@ | + | [jemmyhu@saw-login2:/work]$ chmod ugo=rw README #Give read and write permissions to all |
− | [jemmyhu@ | + | [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@ | + | [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@ | + | [jemmyhu@saw-login2:/work]$ chmod ug-x README #Take away execute permission from user and group |
− | [jemmyhu@ | + | [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@ | + | [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@ | + | [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 568: | 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 595: | Line 545: | ||
The screen looks like: | The screen looks like: | ||
~ | ~ | ||
− | |||
* <font color='blue'>'''Exit vi'''</font> | * <font color='blue'>'''Exit vi'''</font> | ||
Line 616: | 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 634: | 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 654: | 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 667: | 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 683: | 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 699: | 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 719: | 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 739: | 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 755: | Line 696: | ||
| provides the line number for the entire file | | provides the line number for the entire file | ||
|} | |} | ||
− | |||
== Further Reading == | == Further Reading == | ||
− | |||
− | |||
− | |||
− | |||
* [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] | ||
− | |||
* [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] | ||
− | |||
* [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] | ||
− | + | ||
+ | |||
+ | [[Category:Tutorials]] |
Latest revision as of 12:45, 16 November 2017
Contents
- 1 Objectives
- 2 What is Linux?
- 3 Directory Structure in Linux
- 4 Getting Started
- 5 Changing Password
- 6 Basic Linux Commands
- 6.1 ls - list the contents of a directory
- 6.2 mkdir - create directory (i.e., a folder)
- 6.3 rmdir - remove empty directory
- 6.4 cd - change directory
- 6.5 more, less - display contents of file, page by page
- 6.6 cp - copy file
- 6.7 mv - move or rename file or directory
- 6.8 rm - remove file
- 6.9 file - determine file type
- 6.10 diff - display the differences between two files
- 6.11 find - search for files that meet a desired criteria
- 6.12 ps - list processes
- 6.13 kill - stop a process from running
- 6.14 time - measure program running time
- 6.15 top - list processes running on the system
- 6.16 which - locate a program file in user's path
- 6.17 man - help manual
- 6.18 df - display free disk space
- 6.19 du - display file/directory disk usage (in KB)
- 7 File Permissions
- 8 Other Important Linux Commands
- 9 What is vi?
- 10 Basic vi Commands
- 11 Further Reading
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:
- Command mode commands which cause action to be taken on the file, and
- 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 |