Introduction To : Linu Linu Linu Linux X X X
Introduction To : Linu Linu Linu Linux X X X
LINUX
LINUX Bob Booth
Mike Griffiths
Deniz Savas
March 2011
AP-LINUX3
Contents
1 INTRODUCTION....................................................................................................................................... 3
1.1 THE SHELL ........................................................................................................................................... 3
1.2 FORMAT OF COMMANDS ...................................................................................................................... 4
1.3 ENTERING COMMANDS ......................................................................................................................... 4
2 ACCESSING LINUX MACHINES........................................................................................................... 5
2.1 SUN GLOBAL DESKTOP......................................................................................................................... 5
2.2 OTHER METHODS OF ACCESS ............................................................................................................... 5
2.3 LOGGING IN .......................................................................................................................................... 6
2.4 LOGGING OUT ...................................................................................................................................... 6
2.5 CHANGING YOUR PASSWORD ............................................................................................................... 6
3 BASIC LINUX COMMANDS ................................................................................................................... 7
3.1 FILENAMES ........................................................................................................................................... 7
3.2 LISTING YOUR FILES............................................................................................................................. 8
3.3 DISPLAYING THE CONTENTS OF A FILE ................................................................................................. 8
3.4 COPY, RENAME AND REMOVE .............................................................................................................. 9
3.5 SEARCHING FILES ................................................................................................................................. 9
4 REDIRECTION AND PIPING................................................................................................................ 10
1 Introduction
The UNIX operating system was developed in the early 1970s by a group of
enthusiasts at Bell Laboratories in the USA. Since then it was modified by a
number of different groups and evolved into many similar but not identical
flavours. Linus Torvalds, at the time a computer science student at the
university of Helsinki, started a freely available academic version of UNIX
‘LINUX’ that was to become the standard for all Linux implementations that
followed. Now-a-days Unix and Linux have become almost synonymous with
each other. Linux is structured so that the user works within a ‘shell’ that can
be configured by the system administrators, working behind the scenes.
See : http://wrgrid.group.shef.ac.uk/ubuntu/index.html .
Of these BASH is by far the most popular one and is choosen to be the default
shell to use on the Sheffield University HPC computers (iceberg) .
March 2011 3
Bob Booth, D. Savas Introduction to LINUX
1.2 Format of Commands
For historical reasons, most important Linux commands are only two letters
long. This brevity can sometimes make them difficult to remember, and it is
not always easy to tell from a sequence of commands exactly what is
happening. Also Linux distinguishes upper case letters from lower case, and
insists that many commands are written in lower case. Typing a command in
upper case will probably generate the response
If you make a mistake when typing a command you can correct it using the
Backspace or Del key. On the command prompt, you can also use the ← →
arrow keys to move around the command line or Use the ↓ ↑ keys to get back
the previously entered commands. If you want to clear the entire command line
you can hold down the Ctrl key and press u (Ctrl u).
So to summarise:
March 2011 4
Bob Booth, D. Savas Introduction to LINUX
Also some terminal emulation software provide X11 capabilities while others
do not. The meaning of this jargon word is that if your terminal emulator
supports X11 you can run applications on the remote host that either presents a
user friendly interface via Graphical User Interfaces (GUI) or displays its
results in a graphical format. Exceed and XMing fall into this category.
March 2011 5
Bob Booth, D. Savas Introduction to LINUX
Currently Sheffield University have a site licence for Exceed. Exceed DVDs
can be obtained from the IT centre at the cost of the media. Exceed is also
installed on the Managed XP workstations via the Applications Menu.
Please refer to the URL below for further information on this subject.
http://www.sheffield.ac.uk/wrgrid/using/access
2.3 Logging In
Whichever terminal emulation method you use, you will have been asked to
enter the hostname “which is iceberg.shef.ac.uk for the iceberg cluster” and
your usual username and password. This document uses machine iceberg in its
examples, but the procedure is similar for other Linux hosts.
Soon after logging into iceberg qsh or qrsh commands can be used to log
into one of the workers for interactive work. Submitting batch jobs involve the
use of the qsub command which is beyond the scope of this document but see
below link.
exit or logout
command. Logout command will only work when working under a login shell.
A safe rule to follow is to use the exit command if logout command fails.
Never leave your machine logged into LINUX after you have completed your
work. On some hosts CTRL+D on the command prompt can also initiate the
logging out process.
2.5 Changing Your Password
To change your password, use the web form at
http://www.shef.ac.uk/cics/password
March 2011 6
Bob Booth, D. Savas Introduction to LINUX
ls list directory.
ls -l list directory in long format.
ls -al list directory in long format, listing all entries.
3.1 Filenames
When you work on a computer you create files. Files hold information such as
programs, data sets and text passages. Each file is distinguished by a unique
name.
Filenames in LINUX may include any number of letters and digits. You can
use full stops, hyphens, and underscores (.-_) to divide the name into
sections for clarity. There must be no spaces within a filename. For example a
name of a report for 11 March 1991 might be.
Rep_11-3-91
By convention the symbol ‘.’ is usually reserved for separating the file-type
from the filename with the template < filename.filetype > . This is taken into
account by various software components. For example a Fortran program may
be stored in a file named myprog.f77 or myprog.f or myprog.F or myprog.f90
each variation signifying compliance with a particular standard that signals to
the compiler what to do with that file.
LINUX distinguishes between upper and lower case, so the two filenames
MYFILE and myfile are different.
March 2011 7
Bob Booth, D. Savas Introduction to LINUX
3.2 Listing Your Files
The first thing you need to be able to do is produce a list of your files. To do
this you use the ls command with the options -l (long listing) and -a (all
entries). So you would type in
ls -al
The contents of the file will be displayed and, at the bottom of the screen will
be a prompt :
To continue when using more or less you can press:
• Spacebar next screenful
• Enter next line
• q quit listing
• ? list commands
• b go back one screenfull
March 2011 8
Bob Booth, D. Savas Introduction to LINUX
The less command works in exactly the same way as the more command but
has many other extra features. The best one being the ability to use the cursor
up/down and page up/down keys to scroll through the file. You can find out
more about these two commands by consulting the on-line help described in
section 5.
cp oldfile newfile
cp tuesday.dat today.dat
Accidental copying over an existing file will destroy the contents of that file.
For example, in the above example if the file today.dat already existed its
contents will be lost after the copy operation.
To rename and move files we use the mv (move) command. For example, to
rename the file called copyfile to newfile we would use
mv copyfile newfile
As wildcards are also acceptable, be careful when using this command not to
delete your entire filestore.
cp, mv and rm command can also be applied to directories “with care” see
section (6) for further information.
Where string is the word or phrase you want to find, and file is the file or
files to be searched. Option –i is very useful if you want to catch a string
irrespective of its case. The following example will list all the lines which
contain the string LINUX or Linux or LiNuX etc. in a file called
tutorial:
grep –i LINUX tutorial
March 2011 9
Bob Booth, D. Savas Introduction to LINUX
For example, to save a directory listing in a file called dir_list you would
use:
ls -l > dir_list
The output of one command can become the input to another command.
Commands strung together in this fashion are called a pipeline. The symbol for
this kind of redirection is a vertical bar | called a pipe.
who | wc -l
The command wc with the -l option counts lines. So the pipeline is counting
the number of users who are logged in.
Another example is to use the more command to scan through a long directory
listing
ls -al | more
Using the pipe symbol with the grep command, which searches for strings,
we can set up a filter. A filter allows you to select only the useful information
from the output of a command. To use grep as a filter you must pipe the
output of the command through grep.
March 2011 10
Bob Booth, D. Savas Introduction to LINUX
5 Getting Help
Comprehensive on-line help is available for all LINUX commands. The
information is stored in files called reference manual pages and these can be
viewed using the man command. To get a description of any command, type
man command
So to view the manual pages on, for example, the ls command you would type
man ls
The pages would then be displayed via the more command, so you can use the
Spacebar to advance the manual page and the q key to quit the listing. It is well
worth looking at the manual pages on the more command simply to help you
view the manual pages! In addition you can get a full description of the man
help system by typing
man man
Linux has a rich variety of commands for doing many things. The manual
pages give you full information on each command and, usefully, the last few
lines of each entry give cross references to other related commands.
If you are having trouble locating information on a particular topic you can
type
and this will list possible sources. To get a list of Linux commands type
grep --help
Also while working on iceberg typing news on the command line will display the
latest news related to iceberg, usually announcing changes to software.
March 2011 11
Bob Booth, D. Savas Introduction to LINUX
To do this you can create a directory structure. Directories are like divisions
that can contain files. By using a directory structure you can keep related files
together, but separate from other files. In addition any directory can contain
further divisions called subdirectories in case you need to sub-divide your
groups of files further.
pwd
This stands for ‘print working directory’, and is useful as you navigate the
directory structure. You will get a response similar to:
/home/me1xyz
Note that the character that divides the directories and filename in a path is a
forward slash ‘/’, whereas on a PC, a backslash ‘\’ is used.
There are many other ways to use this command, but these four will get you
wherever you’re going.
March 2011 12
Bob Booth, D. Savas Introduction to LINUX
6.3 Making and Deleting Directories
You can create and remove your own subdirectories using the command:
Note that rmdir can not remove directories that are not empty.
In order to keep track of your filestore you can use the command
quota
March 2011 13
Bob Booth, D. Savas Introduction to LINUX
7 Working Interactively
Under Linux programs will operate either in the foreground or the
background. Programs that run in the foreground are connected to the
terminal session and will occupy that session until they terminate. By its very
nature all interactive programs run in the foreground. Programs that are
suitable to run in the background are made independent of the terminal session
by reading/writing their input/output from/to files. You can have multiple
programs running in the background.
fluent
To run a program in the background type the command then add the &
character and press the Enter or Return key. To run fluent in the background
you would type:
fluent &
xterm &
This command runs the program myprog in the background. Output (and errors
>&) are directed to the file outputfile.
March 2011 14
Bob Booth, D. Savas Introduction to LINUX
Any job that has been stopped by Ctrl Z can then be resumed to run on the
background by using the bg command
You start running a program (time.sh) that reads its data from a file
(data.in) and sends its output into another file (results.out)
If the program takes too long to run, this, in effect, locks up your terminal until
the run is completed. You get fed up of waiting and type ctrl +Z to stop the
running program.
Next, you resume the execution of this program in the background and from
where it was left;
bg &
You have now unlocked your terminal while still continuing to run your
program. You can get a list of all your background jobs by typing:
jobs
This would produce the response
[1] + Running time.sh < data.in > results.out
To stop this job (number 1) you would type:
stop %1
Note that stopped jobs can be restarted by using the fg or bg commands.
To kill this job:
kill %1
Terminated
7.3.2
7.3.3
7.3.4 Program Control Using the ps Command
March 2011 15
Bob Booth, D. Savas Introduction to LINUX
Substituting your username for username. This would produce:
You could then use the PID (Process ID) of any program to kill it
kill PID
If this does not work try
kill -KILL PID
But always try kill on its own first to allow the process to terminate cleanly.
7.3.5 Program Control Using the top Command
Type;
top -Uusername
March 2011 16
Bob Booth, D. Savas Introduction to LINUX
(1) Create a script file that contains all of the Linux commands you would
have typed at the terminal to run the job if you were working interactively.
(2) Submit this file to SGE batch processing system using the qsub command
(3) Check the process of your job using the Qstat command.
(4) When the job finished study the job output files.
The topic of batch processing on iceberg is not covered in any great detail here.
Please refer to the following URL for Further information on this topic.
http://www.sheffield.ac.uk/wrgrid/sge
March 2011 17
Bob Booth, D. Savas Introduction to LINUX
Either gedit or nedit editors can be used on the iceberg worker nodes.
If your terminal will not allow graphics, please refer to section 9.3 for
information on the vi editor that works on line-mode terminals.
Refer to the following URL for the recommended sftp programs and links for
downloading them:
http://www.sheffield.ac.uk/wrgrid/using/access
When transferring files between Windows and Linux platforms one important
pit-fall to watch out for is the fact that the end of line characters are different
in these platforms.Therefore human readable ASCII text files will not have the
same binary representation.
Most file-transfer clients automatically detect ASCII files and translate the
end-of-line characters correctly. However, occasionally this may fail and you
may end up with a file on iceberg which looks the same but can not be
processed. The cure is to print such suspect files by using the -v option of the
cat command on iceberg.
March 2011 18
Bob Booth, D. Savas Introduction to LINUX
If you get ^M at the end of the lines that indicates that the file did not transfer
correctly. You could either transfer the file again using the ASCII mode of
transfer or better still use the dos2unix command.
dos2unix file_to_be_converted
You move between the three modes using various keystrokes detailed in
section 9.5. The most common ones are displayed on the following map of the
vi editor.
March 2011 19
Bob Booth, D. Savas Introduction to LINUX
If you are not sure what state you are in (some keys can take you into text entry
mode without you realising it), type Esc to ensure you are in command mode.
If you press Esc while you are already in command mode, the system beeps
and the screen will flash, but no harm will be done.
The editor vi has a history that may help explain some of these idiosyncrasies.
It is an extended version of ex (itself an extended version of ed) which is a
line editor. The ex line editor commands are available in the last line mode.
9.4 Simple Editing
To create a new file you must invoke the vi editor by typing
vi filename
where filename is the name to be given to your new file; it must not already
exist. The screen will clear, leaving a column of tildes (~) on the left-hand side
and displaying the name of your file at the bottom.
9.4.1 Command mode
You are now in command mode. To type text into your file you must go into
text entry mode. There are many, many ways to go into text entry mode, the
usual way when you are creating a new file is to type the character i (without
pressing Enter). The words INSERT MODE will appear in the bottom right
corner, text is displayed here whenever you are in text entry mode.
9.4.2 Text Entry mode
Once in text entry mode you can type in your text. Use the Backspace key to
correct mistakes on the current line and start new lines by pressing the Enter
key. If you spot mistakes on previous lines you will have to leave them for
later. Once the body of your text has been typed in you should return to
command mode by pressing the Esc key.
9.4.3 Command mode
Now you can correct any mistakes. Use the arrow keys to position the cursor
by the offending text and issue the appropriate command to make amends.
Possible commands are listed in the next section. Correcting mistakes may
require you to go into text entry mode again, once you have corrected all
mistakes ensure you are back in command mode by pressing the Esc key.
9.4.4 Command mode
Having entered your text and corrected all mistakes your file is now complete.
You can save your file and exit the editor. To save your file you must enter last
line mode. To do this, in this situation, type in a colon (:) character without
pressing Enter.
9.4.5 Last Line mode
The colon will appear on the bottom line of the screen and you can type in any
last line command, useful ones are listed in the next section. In this case we
want to save the file and exit the editor which we do by typing
wq
March 2011 20
Bob Booth, D. Savas Introduction to LINUX
after the colon, then pressing the Enter key. The file will be saved and you
will exit vi, returning the shell command prompt.
9.5 Command Summary
Cursor movement:
arrow keys these keys work as expected
Enter move to the start of the next line
$ move to end of current line
Ctrl b move back one screen
Ctrl f move forward one screen
Ctrl d down half a screen
Ctrl u up half a screen
w move forward one word
b move backward one word
1G go to top of file
G go to bottom of file
221G go to line 221
Deleting/undeleting text:
x delete single character at cursor
dd delete line
D delete line to right of cursor
p put deleted text below current line
P put deleted text above current line
Other useful commands:
J join this line with the line below
u undo previous command
Ctrl L redraw screen
Last line commands:
March 2011 21
Bob Booth, D. Savas Introduction to LINUX
less dataset1
you need only type enough of the filename to specify it uniquely, then press
Tab. For example, you could type
less da
If you have only the one file whose name starts with the letters "da", the name
will be completed for you, and all you have to do is press Enter to execute the
command.
If you have several files with names beginning "da", then when you press Tab,
your filename will be completed up to the point where ambiguity starts, and
you will get a bleep. For example if you have files called data1, data2 and
data3, then when you type
less da
less data
and you will get a bleep. Now you can press Tab again to get a list of possible
filenames, with your partially completed command displayed again. Type in as
much more as you need to specify the file uniquely, then press Tab again to
complete it.
This may sound complicated, but do try it out. You will find it a very useful
facility, and extremely easy to use.
If you are using a departmental Linux host (i.e. one that is not managed by
CICS), you may find this facility has not been set. To make it work for the
current session, type
set filec
March 2011 22
Bob Booth, D. Savas Introduction to LINUX
10.2 Repeating Previous Commands
The system can keep a history of the commands you type and is able to repeat
previous commands. In bash shell you can scroll through your previous
commands using the up and down arrow keys. To execute a displayed
command press Enter.
history
1 ls
2 ls -a
3 ls -la
4 more LINUX.intro
5 cp empty_file copyfile
6 ls *file
7 mv copyfile myfile
8 rm myfile
9 pwd
To re-issue a command type !n, where n is the number of the command from
the history list.
!4
!string
to execute the most recent command starting with the specified string. For
example if your most recent history is
75 vi test.f
76 f77 test.f
77 a.out
!75
or
!v
!!
March 2011 23
Bob Booth, D. Savas Introduction to LINUX
• Start with a letter (upper or lower case)
export variable=”string”
so to use the word “docs” to refer to a specific directory in the data area you
might type:
export docs=”/data/cs1xyz/shef”
cd $docs
If the variable is to be followed by a letter or digit then curl braces are required
when using the variable, so the command
cd ${docs}field
would be equivalent to
cd /data/cs1xyz/sheffield
alias name='command'
where the command must be enclosed in single or double quotes. For example,
if you type
you then only have to type hm to get your history listing piped into more.
unalias name
March 2011 24
Bob Booth, D. Savas Introduction to LINUX
For aliases to take effect for every session they must be placed in your
.bashrc file.
To get a list of all current aliases, including all aliases created by the system
administrators
alias
Within the bash shell, aliases can include previously defined shell variables.
from now on typing lds will give you a listing of the files in your /data area.
March 2011 25
Bob Booth, D. Savas Introduction to LINUX
March 2011 26