Unix Directory Structure

An operating system is capable of storing data in an orderly fashion. A directory tree is used to organize and data. It organizes files hierarchically, with a root that branches into subdirectories. Thus the tree structure allows users to easily organize and find their data. Just like your paperwork, the better you classification, the easier it would be to find the documents when you need them. Each file and directory in an operating system has unique address thanks to the tree structure.


source: http://www.ee.surrey.ac.uk/Teaching/Unix/unixintro.html

In the diagram above, we see that the home directory of the undergraduate student "ee51vn" contains two sub-directories (docs and pics) and a file called report.doc. The full path to the file report.doc is "/home/its/ug1/ee51vn/report.doc"

Path

A path uniquely identifies a particular file or directory by specifying its exact location in the directory. The symbol slash ‘/’ separates names of files. In a UNIX operating system, everything including directories is considered files.

There are two types of paths. The UNIX directory structure starts from the root ‘/’. The absolute path specifies a file or a directory in relation to the entire UNIX system. The relative path describes the location of a directory or a file as it relates to the current directory.

Each directory contains two links, ‘./’ and ‘../’, which refer to the current directory and its parent directory. Relative addresses use ‘../’ to backtrack and forward addresses to move forward.

Suppose your current directory is

/home/usr/student

You wish to go to the images directory,

Using absolute path: /home/usr/student/documents/images
Using relative path: documents/images

To backtrack to /home/usr, type cd ../

Naming files and directories

Files can be created using alphanumeric characters, underscores, periods, and hyphens. It is wise to avoid all other characters even if the operating system would allow you to use them.

When naming a files or folders, it is very important to give meaning full names. This would greatly help you in find a file when it is needed. Suppose there are two users, Alice and Bob, both of whom using completely different naming schemes.

Alice’s file’s:

$ ls ~
work home photos
$ cd work
Accounts.dat faq.txt todo.list

Bob’s files

$ ls ~
Dir1 dir2 dr3
$ cd Dir1
1.txt 2.txt 3.txt

Right now we are only looking three files and 3 directories. It is already clear who would be capable of finding the “to do list” with much less ease and frustration. Bob has no choice but to open and read all his files until he reaches his to do list. Now imagine that Bob has to sift through hundreds of files. Most computer users have somewhere between hundreds to thousands of files.

It is wise to aim for uniformity rather than uniqueness in file naming since humans are better at remembering and following patterns. Following are some rule of thumbs:

Classify files by their function. Don’t put your vacation photos in the accounting directory.
All files of the same type must have the same extension
The filename should be indicative of the content of the file. The filename should be sufficient to remind you of what your file contains.
Try to keep the name as short as possible but not shorter. If this is not possible, add a readme.txt file next to the file.

UNIX is very powerful and allows to much more control over you own computer. More control means fewer forced restrictions. This in turn resulted in a number of conventions. For example, Microsoft Windows requires file extension to define the file type. UNIX has no such restriction but conventions define that you should use the extension to indicate the file type. It is often wise to following established conventions. Habits, both good and bad, are difficult to break. So why not start with good habits. Get in the habit of following well-established conventions.

Directory commands

To create a directory, we use the mkdir command:

$ mkdir new_directory

To see the directory you have just created, type

$ ls

Following is a list of command which would help you navigate the UNIX directory structure. UNIX also offers graphical environments to navigate the directory structure but very often you would have to come back to the command line. Therefore, it is a good idea to learn and use these simple commands.

Summary

cd:	change directory
pwd:	identifies the absolute path to the current directory
ls:	displays the contents of the directory
ls –a:	display all files including hidden files
ls –F:	display file types. 
	Directory	/
	Executable	*
	ASCII		none
	Symbolic link	@
ls –l	list files with their details
ls –R	recursive listing of subfolders and their files
ls –t	display sorted by time
ls –r	display in reverse order
Shell Metacharacters
Shell metacharacters are specific characters which have a particular meaning to the shell.

cd ~	go to the home directory, not supported by bourne shell
cd -	same as pwd. Korn, switch between two directories
ls m*	display all files beginning with f such as my, man, mime, etc.
ls m?	display all files beginning with an m followed by one character such as my.
ls [a-c]*	display all files beginning with an a, b, or c. 

The asterisk ‘*’ is a wildcard meaning on or more characters. The question mark ‘?’ matches any single character. The square brackets ‘[ ]’ match a set or range of characters for a single character position.