next up previous
Next: Help! Up: Introduction to the Linux Previous: Linux vs Windows

Files and Directories

When you open a terminal window you will see a prompt like this:

[pjh503@bhuna Linux]$

The exact form of the prompt may vary with your machine, and in fact you can customise it yourself fairly easily. It often starts with your user identification (pjh503 in my case) followed by the machine name (bhuna) and some indication of the directory you are currently in.

it usually defaults to being in your home directory. This is a different directory for each user, and is where you will keep all your files. You can see which files are in the current directory using the list command, ls:

[phasnip@mijpnb1 ~]$ ls
a.out             ifort          minimiser.log   quality.sty  tmp
castep_CVS.tar    linux.tex      minimiser.tex   Rules
Desktop           minimiser.aux  minimiser.tex~  Teaching
hermes.addresses  minimiser.dvi  Nautilus        test.F90

The ls command lists everything in the current directory, including programs and subdirectories. It can be difficult sometimes to know what everything is, so ls has an option to add symbols to the output.

[phasnip@mijpnb1 ~]$ ls -F
a.out*            ifort@         minimiser.log   quality.sty  tmp/
castep_CVS.tar    linux.tex@     minimiser.tex   Rules/
Desktop/          minimiser.aux  minimiser.tex~  Teaching/
hermes.addresses  minimiser.dvi  Nautilus/       test.F90

Programs are marked with *, links (see later) are marked with @ and directories are marked with /. The options passed to a command are sometimes called arguments, or switches. In Linux they are usually prefixed with a hyphen, '-'.

To move into the directory we use the change directory command, cd:

[phasnip@mijpnb1 ~]$ cd Teaching/
[phasnip@mijpnb1 ~/Teaching]$

To find out which directory you're in, use the print working directory command, pwd.

[phasnip@mijpnb1 ~/Teaching]$ pwd

The output of the pwd command is often called the full path of the current directory. Linux organises all of its files in a hierarchical way like a tree, so that there is a root directory called simply '/', and then various subdirectories branching off, each of which may have its own subdirectories and so on. The full path tells you where the current directory is in relation to the root directory - i.e. in this case you start from the root directory and go to the home subdirectory. From there you go to the phasnip subdirectory, and then the Teaching subdirectory of that and finally the Linux subdirectory of that. Whichever directory we were in, we could use cd /home/phasnip/Teaching/Linux to take us straight to that directory.

There is another way of specifying paths, and that's the relative path. This tells us where a directory or file is in relation to our current directory, rather than the root directory. Obviously the relative path to a location will vary depending on where we are, but it is useful because it is often shorter than the full path.

For example, if we are in the directory /home/phasnip/Teaching and want to go to the Linux subdirectory we could use the full path

[phasnip@mijpnb1 ~/Teaching]$ cd /home/phasnip/Teaching/Linux/
[phasnip@mijpnb1 Linux]$

or we could just use the relative path

[phasnip@mijpnb1 ~/Teaching]$ cd Linux
[phasnip@mijpnb1 Linux]$

To delete a file you use the remove command, rm, to move or rename a file you use mv and to copy a file from one filename to another you use cp.

To make a directory, the command is mkdir and to remove a directory it is rmdir. You can only remove a directory if it is empty, i.e. has no files or subdirectories in it.

You can also give a path to the ls command to see the contents of a directory without actually changing to it.

next up previous
Next: Help! Up: Introduction to the Linux Previous: Linux vs Windows
Phil Hasnip 2007-08-23