ToC1. Using Unix

Unix is a computer operating system. Strictly speaking, ‘Unix’ refers only to systems produced under license from the descendants of Bell Systems or Berkeley University, but there are so many derivatives both free and commercial, that it's simpler to use ‘Unix’ to mean all of them, because they all work alike and often look alike.

In this document, ‘Unix’ is therefore [ab]used to include SunOS, HP/UX, Digital Unix, GNU/Linux, and Apple Macintosh OS X.

For most work, people use a graphical user interface (the X Window System or similar). However, there are some tasks for which typed commands are faster and more powerful, particularly when you need to bulk-manage groups of files or manipulate data in formats not provided for by your windowing system. If you need to manage files and folders on a server or other remote system (elsewhere on the Internet) to which you have login access, but no windowing facilities, then typed commands are your only way.

Up to start of section1.1. Typing commands

When you are logged into a Unix system, you get a chat-like interface where you can type your instructions to the computer and it will respond with results.

The computer signals its readiness to accept a new command by displaying a dollar sign ($), sometimes preceded by your username or current directory.

When you have typed a command, you must press the key after each command, otherwise nothing will happen. This is also referred to as the Enter, Return, or Newline key.

Remember you must press the key after each command, just like a chat system. Until you do so, the computer doesn't know you have typed anything, so nothing will happen.

Unix commands consist of a command name followed by options and arguments, as shown in the examples in Table 1 below.

Table 1. Examples of Unix commands

Type Command Meaning
Command with no options or arguments top display the list of most active processes running
Another command with no options or arguments passwd change my password (it will ask for it twice)
Command with one option ls -l list all file details in ‘long’ format
Command with one option and one argument ls -l thesis.pdf list details about a specific file
Command with just one argument rm thesis.tmp remove the specified file
Command with an option (-O) with its own argument (newinfo.dat) as well as an argument for the command itself (the URI) wget -O newinfo.dat   http://www.foo.com/daily/data download data from a website straight into a named file
Command with three combined options ls -laR list all files, even hidden files, and including all files in subdirectories
Command with an option and multiple arguments cp -i thesis.* /var/backup/ copy all thesis files to a backup directory but ask me before overwriting anthing

You can only do things to files and directories that you own: if they're not yours, leave them alone (or contact the system manager if you think you ought to have access).

Up to start of section1.2. The Unix file system

Like all modern computers, Unix, GNU/Linux, Apple Mac OS X, and Microsoft Windows all use a hierarchical file system (see Figure 1 below) like an inverted family tree, with the root (starting-point) at the top. The only major difference is that Microsoft Windows has multiple roots, one for each disk (called C:, D:, E:, etc), whereas all other systems only have one root (called ‘/’).

Figure 1. A hierarchical file system displayed in a graphical directory browser

The Nautilus file-manager on Ubuntu Linux

A typical directory-and-file browser, Note the ‘family-tree’ type display of the directories and their subdirectories at the left-hand side.

At any one time, you are ‘in’ a particular directory, referred to as your current working directory. When you log in, you are put into your account's home directory. If your username is jean then your home directory would typically be /home/jean or /usr/users/j/jean or something similar depending on the type of machine and how it is set up. You can show your current working directory with the pwd command, and you can change into any directory to which you are permitted access with the cd command.

Using Figure 1 above as a graphical example, you are in /dtds/tei; that is, starting with the root of the file system (/), your location is in the dtds directory, and in the tei subdirectory of that. To get there from your home directory, you would type  cd /dtds/tei  . This is an absolute reference, because it works from anywhere on the system, and always goes to the same place.

Within this directory, however, you would refer to any of the four subdirectories of the current working directory simply by name; for example  cd p4x  . These are called relative references, because they relate only to the directory you are in when you use them. You could type  cd /dtds/tei/p4x   but there would be little point if you're already in the /dtds/tei directory.

ToC2. Getting access

Up to start of section2.1. Logging in

To log into a Unix-based system you need a username and password, and the hostname of the system you are authorised to log into. These will be provided by the system manager (which may be you if it's your own desktop system).

See howtossh for how to connect and log in.

Up to start of section2.2. Don't panic! (What to do in an emergency)

If you accidentally type a command that starts doing something you don't want, you can abort the command with Ctrl+C (hold down the Ctrl key and press C). This key combination shows in the display as ^C.

$ ls -lR /
/:
total 418
drwxr-xr-x   2 root root   4096 Nov 19 04:04 bin
drwxr-xr-x   4 root root   1024 Apr  7  2008 boot
drwxr-xr-x   3 root root   4096 Nov  1  2007 data
drwxr-xr-x  10 root root   3800 Oct 28 09:40 dev
drwxr-xr-x 102 root root  12288 Nov 19 04:04 etc
drwxr-xr-x  14 root root   4096 Nov 14 16:35 home
drwxr-xr-x  14 root ^C
$

A complex command or program may take a second or two to stop. A program that intercepts control characters may need a second Ctrl+C to force it to stop.

Be careful about using and aborting commands and programs that do significant processing, expecially those which read or [re]write files, as premature termination may leave files open or partially-written and unusable.

Up to start of section2.3. Logging off (Get me out of here)

To log off, type the  exit  command. Always log off after finishing work. If you don't log off, you may be exposing your account to unwanted intruders.

$ exit
User jsmith logged off at 2008-12-25 13:38:34.54

The  exit  command may display some accounting information, or just say ‘Goodbye’, or simply close the window.

ToC3. Navigation: How to move around

Up to start of section3.1. Finding out where you are

To find out the name of the directory you are currently in, type the  pwd  command:

$ pwd
/usr/local/lib/xml

The  pwd  command displays the name of the current working directory.

Up to start of section3.2. Seeing what files are there

To see a list of files in the current directory, type the  ls  command. This displays the names of files (in two or more columns, if they'll fit).

$ ls
backup                                         office
bin                                            restore.odf
Desktop                                        styles.tex
holding                                        ucc.png
lib                                            vehicle.dat
menus                                          word.log
n800
$

Unless you specify otherwise, it lists the names from the current directory. See the pwd command for how to find out what your current working directory is.

You can specify a different directory to list by following the  ls  command with the name of that directory. See section 1.2, ‘The Unix file system’ for details of how to refer to your own and other directories on the computer.

$ ls /usr/local/share/texmf/bibtex/bst/
abstyles      computational-complexity  IEEEtran    oxford
achemso       dinat                     ifacmtg     perception
     [lots more left out here to save space]
bibhtml       harvard                   natbib
camel         hc                        nature
chem-journal  ieeepes                   opcit
$

To see a list with all the file details (the ‘long’ format), type  ls -l  (The l option stands for ‘long’.) This shows the permissions, links, owner, group, size, date, time, and name of each file and directory (folder).

The first letter on each line shows the type of file: d for a directory; l for a link (shortcut or alias), and - (dash) for a normal file.

$ ls -l
total 15
drwxr--r--  3 ontology  webster        14 Dec 28 14:22 images
drwxr--r--  2 ontology  ontology      256 Feb 15 22:37 Mail
-rwxr--r--  1 ontology  ontology   123847 Jan 22 15:35 thesis.pdf
lrwxrwxrwx  1 root      root           31 Feb 27  2005 web -> /var/www/html/ontology
$

In this example, the user has subdirectories called images and Mail, a file called thesis.pdf, and a soft link (alias or shortcut) called web which goes to their web site.

You can see the directory entry for a single file by following the  ls  command and the l option with the filename you're interested in:

$ ls -l thesis.pdf
-rwxr--r--  1 ontology  ontology   123847 Jan 22 15:35 thesis.pdf

Up to start of section3.3. Changing directory

To change your current working directory, type  cd dirname  (replacing dirname with the name of the directory you want to change into.

When you want to do some work in another directory, you need to change to it:

$ cd staff

See the ls command for how to find out what directories you have got. See section 1.2, ‘The Unix file system’ for details of how to refer to your own and other directories on the computer.

Some systems display the name of the current directory before the dollar prompt. If not, to find out what directory you are in, use the pwd command.

Up to start of section3.4. Creating a new directory

To create a new directory, type  mkdir dirname  (replacing dirname with the name of the new directory you want to create).

$ mkdir reports

You can then use the cd command to change into the new directory.

If you need to create a directory and a subdirectory within it (and maybe a subsubdirectory within that), you can do so all in one command by adding the option p before the new path.

$ mkdir -p reports/management/2009/financial

See section 1.2, ‘The Unix file system’ for details of how to refer to your own and other directories on the computer.

Up to start of section3.5. Creating a soft link (shortcut or alias)

If you want to provide a shortcut to a directory or file, the  ln -s  command links a new name to an existing one. It lets you create an alias or alternative name for a file or directory. The s option creates a ‘soft’ link; there are other types but this is the one you want.

Suppose you have created homepage.htm but it ought to have been called index.html; but you have already publicised the existence of homepage.htm and people are using it, so you can't just rename the file or you'll break their existing links. You create an alias:

$ cd web
$ ln -s homepage.htm index.html

Now people can refer to either name, and they'll get the same file.

ToC4. File management

Up to start of section4.1. Creating new files

Files are usually created by an application like an editor, wordprocessor, or spreadsheet, or when you download a file with your web browser. It is possible to create a file from the command-line, but it's extremely rare that you would need to do so.

Up to start of section4.2. Moving and renaming files

Moving or renaming a file or directory in the Unix file system are essentially the same thing. The file or directory doesn't actually get moved physically, it just has the name of its entry in the table of files changed.

To move a file to a new location, type the  mv  followed by the current filename, followed by the name of the directory you want it moved to.

The directory must already exist, so use the mkdir command first, if necessary.

$ mkdir old
$ mv thesis.pdf old/

You can use the ls command afterwards to verify that the file has been moved.

To rename a file or folder (directory), type the  mv  followed by the current name, followed by the new name.

$ mv thesis.pdf thesis-2009-03-27.pdf

You can use the ls command to verify that the file has changed name.

You can do both at the same time if you need to:

$ mkdir old
$ mv thesis.pdf old/thesis-2009-03-27.pdf

This moves the file to the specified directory and renames it as well.

Because directories are just a type of file, you can rename them the same way as you do normal files.

$ mv old /var/archive/backup/oldfiles-2009-03

The new location must of course exist before you can move a file or directory into it. Use the mkdir command to create it if necessary.

The only restrictions are: a) you can't use the  mv  command to move whole directories between disks; and b) you can't move files into directories you do not own. To move files or directories between disks, you must use the cp command to copy the file[s] to the new location, and then use the rm command to delete the originals. To move files into directories you don't own, you must either be the Administrator (root) or you must ask the owner of that directory to allow you in.

Up to start of section4.3. Copying files

To copy a file, type the  cp  command followed by the name of the existing file, followed by the name of the new copy you want to create.

The destination can also be the name of a directory you want to create the copy in.

$ cp thesis.pdf web/phd/

Like the mv command, the destination can be either a file or a directory.

If the destination filename already exists, you will get a warning message and a question about whether you want to overwrite it or not. Answer y or n appropriately.

To copy a whole directory-full of files, and all its subdirectories (if any, and all their files), use the r option.

$ cp -r Mail/ /var/archive/backup/mail-2009-03/

Up to start of section4.4. Deleting files

To remove (delete) a file, type the  rm  command followed by the filename[s] you want to delete.

$ rm thesis.tmp thesis.log thesis.aux

When it's gone, it's gone. Forever. Permanently. There is no Trash or Wastebasket feature when you use this interface. If you delete a file by mistake, you must contact the Operators to get it restored from backup. If it's your own machine, and you don't have a backup, tough.

It is also possible to delete a whole directory and all its subdirectories and all their subdirectories and all the files in them as well with an option to the  rm  command, but you should contact an expert if you think you need to do this.

ToC5. Account management

These are administrative commands which affect your account, status, or rights.

Up to start of section5.1. Changing password

Passwords should be changed at irregular (unguessable) intervals. Never use short or guessable passwords: the best advice is here.

Type the  passwd  command. It will ask for your existing password first, to make sure it really is you and not someone borrowing your account; and it will then ask for a new password, and then ask you to repeat the new password:

$ passwd
Changing password for ontology
(current) UNIX password:         
New UNIX password:         
Repeat new UNIX password:         
$

The password is not shown as you type, not even with asterisks or bullets, so take care (that underlining is just to show in this example you where you type: the underlining does not actually appear when you type the password). If you get the repeat wrong, it will ask for the new password again. You can abort the process and leave the password unchanged by typing a Ctrl+D twice.

Up to start of section5.2. Finding out how long before your password expires

Passwords usually have a fixed lifetime, determined by your systems administrator. You can find out how long your password has left before it expires with the  chage  command:

Type the  chage  command with the -l option (a lowercase ell) and your account name:

$ chage -l ontology
Last password change					: Nov 10, 2011
Password expires					: Mar 22, 2012
Password inactive					: Dec 31, 2012
Account expires						: Dec 31, 2099
Minimum number of days between password change		: 10
Maximum number of days between password change		: 90
Number of days of warning before password expires	: 10, 5/4/3/2/1
$

Changing your password at unpredictable intervals is a good contribution to account security.

ToCGlossary of terms

application

A program run by computer users that is not part of the operating system. Typical applications are wordprocessors, spreadsheets, databases, and web browsers.

hostname

The name of the computer you log into. If the system is your own desktop computer, you will have named the computer yourself when you set it up.

operating system

The internal programs which make a computer run, before you start adding applications. Common operating systems are Unix, GNU/Linux, Apple Macintosh OS X, and Microsoft Windows.

password

The secret code that you give after your username, which allows you access to the system. You must never tell anyone your password, nor write it down. Memorise it.

path

The route from one directory or file to another in a hierarchical file system (applies to all modern computers including Windows and Macs, not just Unix). There are two forms of path, absolute and relative:

absolute

An absolute path always starts at the root directory (the top of the ‘tree’), so it always begins with a slash, no matter what your current directory is.

relative

A relative path always starts at your current working directory, so it never begins with a slash.

root

1. The ‘top’ of a file system, with all other directories contained within it. The only directory that does not have another directory above it.

2. The administration account of a Unix system, with permissions above those of a normal user (hence the alternative term ‘superuser’).

username

The name of your Unix account: the name you log in with. You get this from your system's administrator. If the system is your own desktop computer, you will have created this yourself when you set up the system.

RSS newsfeedKeep up to date with our RSS newsfeed