Linux and VI Editor Fundamentals
Printer-friendlyIndex
- Objectives
- What is Linux?
- Directory Structure in Linux
- Getting Started
- Changing Password
- Basic Linux Commands
- ls - List information about file(s)
- mkdir - Create new directory [folder]
- rmdir - Remove directory [folder]
- cd - Change directory [folder]
- more, less - Display contents of file, page by page
- cp - Copy file
- mv - Move or rename files or directories
- rm - Remove file
- file - Determine file type
- diff - Display the differences between two files
- find - Search for files that meet a desired criteria
- ps - List processes
- kill - Stop a process from running
- time - Measure program running time
- top - List processes running on the system
- which - Locate a program file in the user's path
- man - Help manual
- df - Display free disk space
- du - Display file/directory disk usage
- File Permissions
- Other Important Linux Commands
- What is vi?
- Basic vi Commands
- 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.
Back to IndexWhat 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.
|
Back to Index
Getting Started
Let's try a few simple commands- login
Use SSH (see: SSH for Windows Users) to login onto SHARCNET linux systems
- hostname: Print system name
-
[jemmyhu@nar316 ~]$ hostname
nar316 - who: Print all usernames currently logged in
-
[jemmyhu@nar316 ~]$ 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@nar316 ~]$ whoami
jemmyhu - pwd: Print working (current) directory
-
[jemmyhu@nar316 ~]$ pwd
/home/jemmyhu - date: Display the date &
time
[jemmyhu@nar316 ~]$ date
Mon Nov 19 15:59:55 EST 2007 - exit: or logout to leave the system
Changing Password
To change your Linux account password, use the command passwd
[jemmyhu@nar316 ~]$ passwd |
If you type your old password and a new password correctly, the new one becomes valid.
NOTE: you can change your SHARCNET password on the SHARCNET webportal.
Back to IndexBasic 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@nar316 ~]$ 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 workls -l shows the detailed information of the contents (permissions, owners, size, and timestamp).
[jemmyhu@nar316 ~]$ 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/jemmyhuls -a shows all subdirectories and files, even files that are hidden (these files begin with a dot.) [jemmyhu@nar316 ~]$ 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@nar317 ~]$ cd
[jemmyhu@nar317 ~]$ pwd
/home/jemmyhu
[jemmyhu@nar317 ~]$ cd ..
[jemmyhu@nar317 home]$ pwd
/home
[jemmyhu@nar317 home]$ cd jemmyhu
[jemmyhu@nar317 ~]$ pwd
/home/jemmyhu - more,
less - Display contents of File, page by page
more File less File - 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@nar316 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@nar316 ~]$ find /usr -name "lpr" -print
/usr/bin/lpr
/usr/share/terminfo/l/lpr - ps -
List processes
ps -u username List all your processes.
[jemmyhu@nar316 ~]$ 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@nar316 ~]$ time ./pi-serial
PI = 3.141593
real 0m0.002s
user 0m0.001s
sys 0m0.002s - top -
List processes running on the system
[jemmyhu@nar316 ~]$ top top - 11:46:58 up 32 days, 21:23, 18 users, load average: 1.00, 1.02, 1.20 Tasks: 314 total, 2 running, 312 sleeping, 0 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 Mem: 4037544k total, 4020680k used, 16864k free, 4132k buffers Swap: 6289408k total, 257344k used, 6032064k free, 3162308k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 17862 mjhillie 16 0 13324 1820 1196 S 0.7 0.0 4:48.16 top 15146 root 18 0 62636 3692 1476 R 0.7 0.1 0:00.02 auto.home 12647 tomek 15 0 46280 2396 1592 S 0.3 0.1 0:41.05 sshd 12920 tomek 15 0 46280 2400 1592 S 0.3 0.1 0:37.94 sshd 12921 tomek 16 0 25072 5812 1348 S 0.3 0.1 0:07.58 scp 15134 jemmyhu 16 0 13196 2044 1356 R 0.3 0.1 0:00.37 top 1 root 16 0 4748 404 372 S 0.0 0.0 0:06.60 init
- which - Locate a program file in user's
path
[jemmyhu@nar316 ~]$ which cc
/opt/sharcnet/compile/bin/cc
[jemmyhu@nar316 ~]$ which f90
/opt/sharcnet/compile/bin/f90
[jemmyhu@nar316 ~]$ which mpicc
/opt/sharcnet/compile/bin/mpicc - man -
Help manual
[jemmyhu@nar316 ~]$ 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.
- df -
Display free disk space
[jemmyhu@nar316 ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 42441844 3662780 36623144 10% / /dev/sda1 703596 31708 636148 5% /boot none 2018772 0 2018772 0% /dev/shm /dev/sda3 27585276 966940 25217068 4% /var 10.33.1.80:/hptc_cluster 13938720 7976032 5254624 61% /hptc_cluster 3712500403@gm0,3712500351@gm0:/nar-sfs-mds3/client_gm 74565613440 41282337520 29524984824 59% /nar_sfs 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 -sh DIRECTORY Display only the total disk usage of DIRECTORY (in MB).
[jemmyhu@nar316 ~]$ pwd /home/jemmyhu [jemmyhu@nar316 ~]$ 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@nar316 ~]$ du mpi 3896 mpi/mpi-2/gulper 3944 mpi/mpi-2 4160 mpi [jemmyhu@nar316 ~]$ 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 DIR 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 whom you are going to be giving permissions to.
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@nar316 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@nar316 work]$ ls -l README *View the original permissions on file '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@nar316 work]$ ls -l 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@nar316 work]$ ls -l 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@nar316 work]$ ls -l 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@nar316 work]$ ls -l 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@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
Back to Index
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.Back to Index
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.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
Keystrokes 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
Keystrokes 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.
Keystrokes 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:
Keystrokes 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
Keystrokes 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
SAMS Teach Yourself Unix in 24 Hours, third edition, Sams Publishing, 2001- 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
- Basic Unix Commands
- http://www.ss64.com/bashsyntax/vi.html
- vi Editor Commands
- http://www.cs.colostate.edu/helpdocs/vi.html
- Basic vi Commands