NEXT UP previous
Next: Filesystems

Directory Hierarchy

If you execute the following commands:

	$ cd /
	$ ls

you will change your current working directory to root (/) and then get output from the ls command, something like:

	bin	dev	home		mnt	sbin	var
	boot	dos	lib		proc	tmp	vmlinuz
	cdrom	etc	lost+found  	root	usr

However, in the last tutorial, I said that every directory also contains the names dot and dot-dot, so why didn't they appear in the is listing? In fact, as these two names will always be there, there seems little point in listing them. In order to ensure that they don't get listed, ls has a built-in rule which says that, by default, names that begin with a period (or full stop '.') should be ignored. This rule not only applies to the names dot and dot-dot but to any names that begin with a period.

This provides a convenient mechanism for having files contained in a directory, but not listed when you look at the directory's contents. Don't get the idea that this provides a mechanism for creating secret files, because it doesn't. It is very easy to get ls to list all files, including those that start with a period.

There are many occasions, when entering a command, where you will either want the command to perform some extra action beyond its default function or where you will want to suppress some part of the command's normal operation. In these cases, what you need to do is to pass command line switches into the command to switch on (or off) the functionality you require. In order to provide a degree of consistency, the normal way to pass switch values to a command is to use a hyphen (-) before the switch itself, to signify that this is a switch and not just a normal command parameter (such as a file name, for example). In the case of the ls command the switch required to get it to list all files is -a. Typically this would give:

	$ cd /
	$ ls -a
	.	cdrom	etc		mnt	tmp
	..	dev	home		proc	usr
	bin	dos	lib		root	var
	boot	etc	lost+foumd	sbin	vmlinuz

After any command line switches, ls can also be given the name of a directory, in which case it will list the contents of the specified directory rather than the contents of the current directory:

	$ ls -a /tmp
	.	  .X11-unix	elm.rc.OLD	ls	tagfile
	..	  NEAT		elv_61.1	modes
	.X0-lock  clocks	hosts.OLD	nets.OLD

Don't forget that you should expect to obtain different results if you execute this command on your machine because /tmp is a publicly available directory for any users to store their own temporary files.

When you start to work on a Linux system, writing programs and running pack-ages, you will start to generate files of your own. In general, you should hang all your files under your home directory. However, if you just put all your files into your home directory then it will soon become difficult to find things (take a look in /usr/bin). To overcome this problem you need to impose some sort of order and layout on your file space. You can do this by creating your own mini directory hierarchy under your home directory and imposing some simple self-discipline, to make sure that whenever you store a new file it goes in the right place.

In order to create new directories, you use the mkdir command and pass it the names of the new directories as parameters:

	$
	$ mkdir bin text

Remember that cd on its own takes you back to your home directory. The mkdir command then creates two new directories in your home directory, called bin and text. Any number of directories can be created with a single mkdir command. This obviously means that mkdir can cope with a variable number of parameter values. In fact, as a general rule, any command where it makes sense (so, not cd, for instance) can take a variable list of names as parameters to operate on, and function correctly on the entire set.

Having created the directories, the next task is to copy some files into them. The copy command in Linux is cp:

	$ cp /etc/passwd text/mypass

Assuming you are still in your home directory when you type this command, it will take a copy of the system password file (/etc/passwd) and store it in your text directory under the name mypass.

In this form, cp just takes the pathnames of two ordinary files as parameters and makes a copy of the first into the second. There is also a second form of the cp command, where the last parameter is not an ordinary file name but the name of a directory. In this form, a variable length list of ordinary file names can be specified instead of the first parameter, and all of the files specified will be copied into the given directory:

	$ cp /etc/passwd /etc/motd text
	$ ls text
	motd	mypass  passwd

Notice from the above sequence that using the second form of cp gives no opportunity to change the names of the files copied.

Incidentally, the file /etc/motd contains the system's message of the day. By default, the contents of this file are listed to users as they login. This means that if your system administrator needs to get a message to users, entering the message into this file is the easiest way to do it.

Just as files and directories can be created, they can also be removed. In order to remove a directory you use the rmdir command:

	$ rmdir bin
	$ rmdir text
	rmdir: text: Directory not empty

As an added safety feature, the rmdir command only works if the directory is empty (except for the dot and dot-dot entries). So, here, the bin directory has been removed but the text directory has not. In order to remove the text directory you need first to remove the three ordinary files contained in it. Ordinary files can be removed with the rm command:

	$ rm text/mypass

NEXT UP previous
Next: Filesystems