Linux is an operating system, an alternative to MicroSoft Windows. To all intents and purposes, it is a version of the UNIX system. In this document, I describe some basic concepts of UNIX, all of which apply to Linux.
UNIX is a very powerful system. If you are new to it, this can trip you up. Do some reading before you start, and be careful. That leads to another problem: there is a huge amount of material about UNIX and it can be hard just finding your way through it. This document aims to get you started painlessly.
If you know nothing about Linux and you are installing it for the first time, you should read this before you attempt to use your new system.
A UNIX computer can be run as a client (also called a workstation) or as a server. If you intend to run UNIX instead of MS Windows on a desktop PC, then you are going to use it as a client. If you are going to use it to provide services to other computers on a network (web service, file service, print service etc) then you are going to use it as a server. You can use your UNIX system by logging into it from a Windows PC and controlling it remotely. In that case, you need to set it up as a server.
If you are going to run a UNIX computer as a workstation, then you will probably want to use a Windows-style interface. You can do this, in fact you have a number of choices, mainly the gnome software and the KDE software. They both offer similar features to MS Windows, but all three are irritatingly different.
An MS Windows systems comes with a set of graphical tools to administer it. Linux has some of those too, but a lot of the system administration is done using a command-line interface. Users of MS DOS will be familiar with this concept. The UNIX interface is smoother and much more powerful than DOS.
To get a UNIX system working, you have to understand how to use the command-line interface. If you are trying to build your first UNIX system, this is a problem, as you have nothing to practice on before you start building. You can get round this using Red Hat's cygwin software. It provides a set of UNIX commands that run under MS Windows. Cygwin includes a command-line execution program (shell) and the text editor vi. It is free and you can download it from the Red Hat web site www.cygwin.org.
If you read these notes and do a little practice with cygwin, you should know enough to get Linux installed on a PC.
Once you have a running UNIX system and you know how to log into it, you can download the free learn Computer-Based Training software from our web site www.merrowinternet.com. This will take you through some basic commands in detail. Learn should run on any UNIX system. There are detailed installation instructions on the site along with the software, including how to install it on a UNIX system where you don't have root access.
If you are practicing using cygwin, you can skip the next couple of sections for now. Go to Running Commands.
To use a UNIX system, you must log in, quoting a user name and password. Your system administrator will set one up for you.
(If this is your own UNIX system, you are the system administrator, so you have to set up a user name for yourself. Do this as the special root user, which is set up during installation.)
Case matters in a user name and password. SIMON, Simon and simon are different names.
To log in using a Windows-style interface, type your user name and the password into the appropriate boxes. You will see a collection of icons on the screen. Look for one that runs a terminal (maybe called a console or a command window) and left-click on it.
Unlike with MS Windows, once the terminal window is up, you have to left-click somewhere within it before you can type. If the window is obscured by another, left-click on the top bar to bring it foreward.
Type commands into the terminal window and see the results. If you want to, you can start a couple of terminal windows and switch between them. Each window has its own environment - each can be in a different directory and so on.
If you are not using a windows-style interface, you will just see a login prompt on the screen:
Type your user name, followed by the enter (return) key.
The system now asks you for your password:
password:For security, what you type is not displayed.
If you got your user name and password right, you will get a prompt. You can control the prompt. Traditionally it ends with a "$". It may just a single "$", or maybe something longer, ending with a "$".
When you are finished, remember to log out. If you are using a Windows interface, there should be a menu entry to do this. If you are not using such an interface, type logout or use ^d (hold down control and type a d).
Whichever way you got here, you are ready to type your first command. Try typing
followed by the enter (return) key. This will display the date and time, something like:
Mon Feb 4 14:17:10 GMT 2002
You must hit the enter key after every command. I won't say this again, so remember: the system won't see what you type until you hit enter.
You can type ahead if you want to. If you type something in when the system is not ready for it, it just stores it up until it is ready.
Case matters under UNIX. You have to type date, not DATE. There is no command DATE, so if you type that, you will see:
DATE: not found
The commands that you type are being obeyed by a program called a shell. There are several shells available. I assume that you are using the popular bash shell. This has a command repeat and edit mechanism, so you can obey the same command over and over, with variations when needed. Use the uparrow key to scroll through the list of recent commands. Edit the command using leftarrow, delete etc, then hit the enter key. (It doesn't matter where you are on the line when you do that.)
Bash also handles filename completion. If you are partway through typing the name of a file in a command, hit the TAB key and it will complete the name for you. If more than one name matches what you have typed, it will complete as much as it can and leave you to do the rest.
Use uparrow and enter to run date again.
Whenever you see the prompt, you can run another command. When it's finished, you see another prompt. If you don't see a prompt, the last command is still running, possibly because it is never going to finish. You can kill it off by typing ^c (hold down the control key and type c).
The bash shell is an extension of an earlier UNIX shell called sh. The original was written by Steve Bourne and is called "The Bourne Shell". Bash is the "Bourne-Again SHell". The main difference is that sh doesn't have the interactive command history.
Both shells support a powerful programming language. You can write programs called shell scripts and use the shell to obey them. We run a course on writing shell scripts and most books on UNIX cover scripting.
Each UNIX command aims to do one thing well, and the commands are designed to work together using the shell as glue. We will see how to do this later. For now, let's learn a few more commands.
The touch command creates an empty file.
The ls (list) command lists the files in the current directory. Try this
touch foo bar ls
will display the new files foo and bar.
Case matters in a filename too. the file foo is not the same as the file Foo or FOO. Try this
touch foo Foo bar Bar lsNote how ls sorts the files: in alphabetical order but with upper case before lower case. It's sorting them according to the way that characters of text are represented on your computer. (Most computers use a representation called ASCII, where digits sort before upper case, which sort before lower case.)
A command can take arguments, separated by spaces, as in the touch command above. Arguments beginning with a hyphen (options) modify what the command does. With the "-l" (long) option, ls gives more information about each file - the owner, size, last written date and so on. For a long listing of the files in the current directory, type
You will see something like
-rw-r--r-- 1 simon users 0 Feb 8 11:28 bar -rw-r--r-- 1 simon users 0 Feb 8 11:28 foo
The first part shows the file permissions (explained later, essentially who can access the file and what they can do with it). The user simon owns the file, it is in the group users, the length is 0 (it's an empty file). The date and time show when the file was last written (in this case, when it was created). If the year in the date is not this year, that is displayed too.
The options have to be the first arguments, so
ls -l foo
gives you a long listing about the file foo, but
ls foo -l
means "tell me about the files foo and -l". If there is no file called -l, you get an error message, something like:
ls: cannot find -l
which means "the ls program could not find the file -l. (Compare that with the error message shown earlier. Why is the wording different?)
If there is more than one option, you can put them together, like this
With the -l and -t options, ls produces a long file listing of the current directory, sorted in order of last written time.
If a file exists, the touch command resets its last written date. Try this
touch bar ls -lt
This file listing is in order of the date last written and the file bar was written to after foo.
An ls listing does not show a file if its name starts with a ".". The -a option makes ls show all files. Try this
touch .foobar ls ls -a
The second listing shows .foobar, plus files . and ... I will explain later what this feature is for. For now, avoid creating files with names starting ".".
Ordinary files in UNIX just contain plain text. You can create files with more complex contents (with different fonts, emphasis and so on) using a word processor, but the system is configured and controlled using plain-text files. HTML (web) pages are also plain-text files.
There are a number of programs called text editors, which can create and update plain-text files. I use vi (pronounced "vee eye"). It is very powerful and it should be available on every UNIX system you ever use: Linux, Solaris, AIX or whatever. You can also get versions of vi that run under MS Windows. Other editors such as emacs and pico are available, but not everywhere.
Start vi like so:
You can run around the screen with the cursor keys. Also, ^u (control and u) cranks up half a screen and ^d cranks down.
(Unlike other editors that you may have used, vi does not let you change anything while you are moving around the file, for instance you cannot remove text using the delete key. To change things you have to type the right command.)
To jump to a particular line in the file, type : followed by the line number, so :42 jumps to line 42. To jump to the last line, type :$.
To search for text, use /. The cursor jumps to the command line at the bottom of the screen. Type some text to search for and hit enter. It jumps to the next occurence of that text. It also remembers what you searched for last time. Type / and enter again, and it repeats the search.
Searches use wildcards. Beware when searching for punctuation characters, because many of them are wildcards. For example . matches any single character, so /a.b searches for any three-letter string starting with "a" and ending with "b".
The document you are editing is broken into words, which are separated by white space and puntuation. To move from word to word, use w (next word) and b (back one word). You can also number these - 10w jumps forward by ten words.
The ^ command jumps to the start of the line. The $ command jumps to the end.
Other keys issue commands:
You can precede commands with a number - 10dw deletes the next ten words, and so on.
If you type something that it cannot make sense of, vi will signal an error by ringing the bell (assuming that there is a bell mechanism on your computer and the volume is turned up high enough to hear it.)
When you delete any text, it is put into the paste buffer. Get it back by typing p (paste in front of the cursor) or P (paste after the cursor). For example, to remove line 98 of the file and make it line 6, do this:
:98 dd :5 p
The sequence xp swaps a pair of characters over. (Why?)
You can put the current line into the paste buffer without remove it using Y ("yank"). Note that the command is upper case. You can yank more than one line - 10Y yanks the current line and the following nine. This is a copy and paste rather than a cut and paste.
To insert text, type i. This puts the editor into insert mode. Type in your text, followed by the ESC key. That puts you back into command mode. You can use the delete key while you are in insert mode, but not the arrow keys, so you cannot jump from line to line. (There's a version of vi called vim that does let you move around the file while in insert mode.)
To add whole new lines after the current one, use o (open new line) followed by ESC.
The cw command changes the current word. The editor marks the end of the current word with a $ and enters insert mode. Type the replacement word(s) and hit ESC when you have finished. If you are in the middle of a word and you type cw, you change part of the word - from where the cursor is to the end of the word.
The . command repeats the last change - a delete, change word or whatever. You can combine this with a search. For example, /John jumps to the next occurence of the text John. You can change that to Jim using cw. After that, you can repeat the search and change over and over with / (followed by enter) and .. When you get to the end of the file, the editor will warn you and wrap round to the start. When there are no more occurences of John it will ring the bell.
You can undo the last command by typing u.
Once you have finished editing the file, write and quit like this:
To write to a different file bar and then quit:
:w bar :q
If you have been editing for a long time and you want to save what you have done, then carry on, use :w.
If you have made changes, and you try to quit without writing them back, vi will ask you to confirm that you want out. You can force it to quit without question using :q!.
Some people find vi confusing, because what happens when you type a character depends upon past history. If you are not in insert mode and you type i, you enter insert mode. Now if you type i again, that text is put into the file. You have to keep track of which mode you are in.
Typing a word when you are not in insert mode just issues a series of commands - "rod" is the "r" command followed by "o" followed by the "d" command. That will take the character that the cursor is sitting on and replace it with "o", then issue the d command, which will delete some text.
Another confusion is that a lower case command will do one thing, and the equivalent upper case will do something similar but different. For example I and i both insert text. I inserts it at the start of the line, i inserts it in front of the cursor.
Those are probably the worst sources of confusion. Once you get your head around those, things get easier.
This section has only scratched the surface of vi but it's enough to get started.
The cat command ("ConcATenate") displays the contents of one or more files. This command
cat foo bar
displays the contents of the file foo, immediately followed by the contents of bar. If the total output overflows the screen, you get a brief glimpse of the first bit, but only the end part remains.
The head and tail commands display just the start and end of one or more files. By default, they show ten lines of each file, but you can control how many, like this
head -20 foo
The more command displays the whole of a file, one page at a time. Each page fills the terminal window, however big that is.
Type the enter key to see one more line. Type a space to see the next page. Type b to go back a page Stop it by typing q for quit. Note that it reacts to each character as soon as you type it - you don't need to hit the enter key after each one.
You can change the name of a file using the mv ("move") command, like this:
mv foo bar
The file that was called foo is now called bar. If there was already a file called bar, its contents is now gone, replaced by the contents of foo.
Remove files with rm:
rm foo bar
removes the files foo and bar from the current directory.
UNIX assumes that you know what you are doing and mean what you say. There's no equivalent of the MS Windows wastebasket. If you remove a file, or move a file over it, the contents has gone forever. Unless you have a backup, and you can't restore it.
You can make your own backups using cp ("copy"). Whenever I edit a system control file, I take a copy first, so that I can backtrack quickly if I make a mistake.
cp foo foo.orig
creates a file foo.orig which is a copy of the file foo.
cp -p foo foo.orig
creates a copy, preserving characteristics like the last written date. That can be useful when you are trying to work out when you did what.
If I create backups regularly, I make copies whose names are timestamps. For example, if a file was last written on the 4th February 2002 and I want to edit it, I create a timestamped copy like this:
cp -p foo foo.2002.02.04
Because of the way that the names are sorted, foo and its backups will appear together in an ls listing, and the backups will appear in ascending date order. The name reflects the time the file was created.
(I'm generally interested in the time I produced this version of the file, rather than than the time I took the backup. Because I specified -p when I took the copy, I can find that out anyway by doing a long listing, but I have to think up a unique name for the backup file, and this one is as good as any.)
Filnames can be referenced in commands using wildcards. If the current directory contains files able, about and absolute, then:
displays the files' contents in alphabetical order of file name.
You can use similar wildcards in MS DOS, but there the handling is built into individual commands. In UNIX, the wildcards are handled by the shell itself, so they work with any command. In the example above, before the command is run, what you type gets expanded to
cat able about absolute
and those are the arguments that the cat command sees.
The wildcard ? matches any single character in a filename. If the current directory contains files cat, rat and dog, then
will remove cat and rat.
You can specify a list of characters to match using [...]. For example, if you have files cat, rat and dog as before, then
will remove cat and rat. Alternatively, if you had typed
that would remove all files with names starting "d" or "r" - in this case dog and rat.
You have to be careful when using wildcards to remove files.
removes all files in the current directory whose names start with "ab", but
rm ab *
Will remove the file ab, if there is one, and then remove all the files in the current directory.
It's possible to own a file but have the permissions set so that you can't remove it. If rm comes across one of those, it asks permission before removing it. You can force it to remove files without any questions with the -f option.
You can combine that with the -r option to remove subdirectories:
rm -fr *
removes each file in the current directory, recursively descending into each subdirectory removing everything in it. (Recursively means that if it finds a subdirectory within the subdirectory, it cleans out that, and so on.
An ordinary user should only be able to destroy his or her own files, but root can remove anything. If you log in as root to clear out some old files, be careful not to get confused..
I try to avoid using rm with wildcards. If I ever need to, I run a less destructive command first, say:
check that this lists only the files that I want to get rid of, then use bash command editing to replace ls with rm.
A command has three channels: standard input (channel 0); standard output (channel 1) and standard error (channel 2). By default these are connected to the terminal (the command window) but you can redirect them to files. For example, if you type:
ls -l foo junk
and there is no file called junk, the ls command will produce some information about the file foo on its standard output channel and an error message on its standard error channel. This command:
ls -l foo junk >o 2>e
redirects the output of the ls command to the file o and the error channel to the file e. If the files don't exist, it creates them. If the files do exist, anything in them is overwritten.
ls foo -l >>o 2>>e
does the same, but appends the output to whatever is already there.
Spaces matter in the redirection:
ls foo -l > > o 2 > > e
is not the same.
If you are only interested in what a command produces on one channel and you want to throw away anything that comes out on another channel, you can do that by redirecting the unwanted channel to the special file /dev/null. The system discards anything that you write to that file.
You can run commands in a pipeline, where the output from one command provides the input to the next. For example
a | b | c
has a similar effect to
a >t1 b t1 >t2 c t2
which is to say that the command a runs with its output channel connected to the input channel of the command b. The output from b is fed into the command c. However, with a pipeline, no intermediate files are produced.
You can use head, tail or more at the end of a pipline. If you run any of those commands with no arguments, it takes its input from the standard input channel, in this case whatever comes up the pipe. For example,
ls -l /etc | more
I said earlier that UNIX commands are designed to work together. The pipeline is one way to do this.
If you have a printer available, you can print a text file using the lpr command:
The file is printed with no headings or pagination.
You can use the command pr, to prepare a file in all sorts of interesting ways, setting in columns and so on. The simple version is just:
pr foo | lpr
You may find that pr paginates for some ancient printer, not yours, so you may have to adjust the number of lines to the page, the page width and so on. See the manual entry for pr.
The fgrep command (pronounced "eff grep") searches one or more files for a piece of text.
fgrep hat a b c
searches the files a, b and c and displays all the lines which contain the text "hat". This includes text like "chat". The search is case-sensitive, so it won't find the text "Hat".
A filename can be just a string of digits:
creates a file called 48372.
A filename can contain just about any character and the name can be very long, up to 256 characters. However, various characters will cause you problems later. For example, since spaces separate the arguments in a command, spaces in a filename are a bit of a nuisance. Take this command:
touch This file has quite a long name
Is this supposed to create a single file with spaces in the name, or seven separate files? It does the second. To do the first, you have to put quotes around the name:
touch 'This file has quite a long name'
and do the same every time you mention that name in a command.
Use full stops (periods), underscores or upper case letters to break up the words in a long filename: ThisFileHasQuiteALongName, this_file_has_quite_a_long_name or maybe this.file.has.quite.a.long.name.
You can also use hyphens, but don't use a hyphen as the first character: if we have a file called -l, and we type
what does it mean?
You can keep related files together using a directory. A directory holds a collection of files and subdirectories. In the beginning, you have a single directory called your home directory. You can store files in there and create subdirectories. This produces a hierarchical structure.
Say you have created a text file foo in your home directory. Now create some directories with mkdir:
mkdir this that
That creates directories this and that. If you run ls, you will see:
foo that thisNow change your current directory to the directory this:
The ls command lists the contents of the current directory. You have not created any files in there yet, so it will show you nothing.
Use touch to create a file bar and run ls again. It will show you just the file bar. Your other files are in other directories.
You can always refer to the current directory as ".", and the one above as "..".
to move one level up - back to your home directory.
The mv command will also move files from one directory to another:
mv foo that ls
The file foo is gone from your home directory.
You can list the contents of a directory with ls:
will show you that the directory that now contains the file bar. In fact
("list the contents of the current directory .).
While you are in your home directory, you can refer to the file bar as that/bar, for example:
If you change to another directory you can always return to your home directory using
You can remove a directory with rmdir, but only if it is empty. To remove a directory and any files and subdirectories within it, use a recursive rm:
rm -r that
(From this point on, this guide describes features of a real UNIX system. You can no longer experiment with cygwin on an MS Windows system.)
All of the directories and files stored on a UNIX computer are gathered together within a single directory called the root directory. Its name is simply /. On my system, the root directory contains a directory home and within that is the home directory of each user. My user is simon, so my home directory is /home/simon. If I have created a directory in there called that containing a text file called bar, then the file can be refered to using its full pathname which is /home/simon/that/bar. The names I used earlier are relative pathnames, (relative to the current directory).
A full pathname of a text file starts with / and is a list of directories within directories, separated by /. The last component in the pathname is the name of the file (bar in the example above).
(If you are used to MS DOS and Windows, you would expect the separator to be "\", not "/". Early versions of MS DOS did not have directories and the designers used the "/" character for another purpose. They introduced directories later, taking a lot of the ideas from UNIX, but they had to use a different separator.)
The pwd (print working directory) command prints the full pathname of the current directory.
To change your current directory to the root directory, do this:
This contains a directory etc which contains a directory sysconfig:
cd /etc/sysconfigtakes you there.
If your current directory is /etc/sysconfig, then
puts you into the directory above, /etc.
When you are in a directory, you can use a relative pathname:
You current directory was /etc, so it is now /etc/sysconfig.
If you use the bash shell, you can refer to your home directory as ~ and the home directory of the user john as ~john, so we can use file names like ~simon/that/bar.
The find command searches the directory hierarchy for a file using various criteria (name, type etc). For example:
find / -name passwd
searches the entire filestore (from the root directory downwards) for files called passwd. It prints a list showing the pathnames of the files it finds.
find . -name passwd
does the same, but it only searches within the current directory.
Find prints pathnames relative to the starting point (something like ./foo/bar/passwd).
You can search for files using wildcard names, but you must put them in single quotes (otherwise the shell will attempt to expand the wildcards before it runs find. In this case, you want find to handle them.):
find . -name 'foo*'
will search for files whose names begin foo, starting the search in the current directory.
Linux is supported by a set of Howto documents, each explaining how to do some task. When you install Linux, they should be copied from the CD onto your disk somewhere, but where?
If we guess that they will be put into a directory with a name containing "howto", "Howto" or "HOWTO", we can use find to search through the filestore looking for them. If we do this as an ordinary user, the find will display lots of errors as it hits directories that we can't access, but we can discard those:
find / -name '[Hh]owto*' -o -name 'HOWTO*' 2>/dev/null | more
Find is very powerful and has lots of features, for example, apart from displaying the names of files that it finds, it can apply a command to each one. See the manual.
Each user has a named account with a password and a home directory where that account can create and remove files. When you log in, your current directory is your home directory. Each user is also a member of a group
A user's file can be readable, writable and executable. It can be any or all of those things by the user, by any user in the same group and by others (any other user).
A long ls listing shows you the permissions. The order of the permissions in the list is user, group and others, so rwxrw-r-- means that the file is readable, writable and executable by the owner, readable and writable by members of the group and just readable by others.
You can turn permissions on and off using the chmod (change mode) command. If I own a file called foo, then:
chmod g+w foo
makes foo writable by other members of the group.
chmod o-w foo
removes the right by others to write to the file. Members of the group and the owner may still be able to write to it.
chmod -w foo
removes that right for everybody - the owner, the group and others.
chmod u+w foo
Sets write permission back on for the user (the owner).
If you write a shell script, you must make it executable before you can run it:
chmod +x scr ./scr
You can set the same permissions on directories that you own, so given read permission, somebody can list the files using ls, given write permission, they can remove files from the directory. Execute permission has a different meaning on a directory - it makes it possible for somebody to make it their current directory (to cd into it).
Groups are meant for people who work together and need shared access to a set of files. The system administrator decides which group(s) you are a member of. When you create a file, its group is your current group. If you are a member of the group hrstaff, then you can change your current group to that:
changes your current group to hrstaff. Any files you create during this session will now be in that group.
There is a wealth of information freely available in books and on the web. Sadly, some of the original classic texts are out of print, for example The UNIX System by Bourne (pub Addison Wesley) gave a terse but clear introduction for the general user. Amazon's book search service claims to be able to find copies.
UNIX comes with an online manual (see later) and you can download some free Computer-Based Training software from our web site www.merrowinternet.com.
Taught courses are a good way to get to grips with UNIX concepts quickly. We run a series of courses on UNIX and related subjects using online conferencing techniques, so you can study at work or at home, communicating with a tutor over the internet. For full details of our courses, visit our website
The Linux community has produced a set of "Howto" documents, each covering a different subject. You can find the latest versions on the Linux documentation site www.linuxdoc.org, along with an index. There will also be copies on your Linux distribution CD. (On my Red Hat CD they were in doc/Howto.) The CD should be readable under Windows as well as under UNIX, so you can read them before you build a working UNIX system.
If you are new to UNIX, you might find some of these Howtos useful:
UNIX comes with an online technical reference manual. It's very useful, but it assumes that you already have an overall grasp of the system. It's very terse and sometimes quite tricky. Since the commands are designed to work together, many commands only make sense in the context of other commands. You have to browse around before things become clear.
If you ask questions in a public forum about UNIX, you will be expected to read the manual for details. The reply "RTFM" means "Read The Manual".
(The Cygwin software has its own cut-down manual. Most of this section does not apply to Cygwin.)
The man command displays manual entries:
displays the entry about the ls command.
tells you about the man command itself.
Each manual entry follows the same pattern, starting with a one-line description, a synopsis of the the argument structure, followed by a detailed description of the command. The SEE ALSO part tells you about related manual entries, which is very useful if you are not quite sure what you are looking for.
The FILES part tells you about files that the command uses, typically control files that configure it.
The manual is broken into eight sections:
If there is are entries with the same name in different sections, man shows you the first one by default. You can specify the section number for clarity:
man -5 group
Each section has an introductory entry called intro.
displays the introduction to the commands (section 1). This list all the commands, with a one-line summary of each one. Good bedtime reading.
The shell provides many of the features of the system that you see, redirection, piplines, file name wildcards and so on. It has its own programming constructs and it implements a set of variables. We use the shell called bash
describes it. The description is very detailed and sometimes opaque, but there is information in there that you won't find anywhere else.