UNIX Tutorial


3. Looking around

Commands covered in this section:   pwd, ls, cd

Like other operating systems, UNIX organizes user data, programs, etc. into structures called files. Files, in turn, are placed in directories (often referred to as "folders"). Directories are organized into a hierarchical structure, something like an upside down tree. This entire structure, including all of the directories and files, as well as the special structures the operating system uses to keep track of them, is called a filesystem. A simplified UNIX filesystem is shown in Figure 3.1.

Figure 3.1   A UNIX filesystem

At the top of the hierarchy is a special directory, called the root directory (following the upside down tree analogy — try standing on your head if this is not clear!). This directory is represented by the "/" character. Under the root directory, you will find a number of other directories (e.g., "bin" or "etc" in Figure 3.1). Under each of these, you might find more directories, and so on. Within these directories, you will find files containing such things as programs (like the passwd command), system configuration information, or user data (like natasha's list of favorite imported ales).

As you use UNIX, the shell keeps track of your current location within the filesystem. This is called the working directory. The pwd ("print working directory") command tells you where you are in the filesystem by printing the current working directory. Let's give it a try.

Exercise 3.1

Enter the pwd command at the shell prompt.

$ pwd
/home/natasha

When you log in, UNIX initially sets your working directory to your home directory. This is a special location where you store your personal data as well as the configuration files that govern your account. Since you've just logged in and haven't yet learned how to change directories, what you see when you enter the pwd command is your home directory. Naming the home directory after the user and placing it under a directory called "/home" (as in our example) is a sensible convention, but it's not a strict rule. You may find that the name of your home directory is somewhat different.

The location of a file or directory within the filesystem is called a pathname, because it describes the path you must follow to find it. You can construct a pathname by starting at the root directory and working your way down. Each level in the hierarchy is separated by the "/" character. For example, you will notice that there is a directory called home underneath the root directory in Figure 3.1, and underneath that is another directory called natasha. The absolute pathname for the natasha directory would be /home/natasha (the first "/" character represents the root directory; the second separates "home" from "natasha"). This type of pathname is called an absolute pathname because it starts from a fixed reference point, the root directory ("/"). A relative pathname, on the other hand, describes the location of a file in relation to the current working directory. If the current working directory were /home, the relative pathname for accessing the natasha directory would simply be natasha.

To change the current working directory, use the cd command followed by the pathname of the desired directory.

Exercise 3.2

Use the cd command to move to the directory just above your home directory.

$ cd /home
$ pwd
/home

Note that cd does not give you any feedback if it succeeds. You can use pwd to verify the new working directory. Now try cd without any arguments. Where does it take you?

$ cd
$ pwd
/home/natasha

In exercise above you used an absolute pathname ("/home") to move to the directory above your home directory. You could also have used a relative pathname. The directory above the current working directory, known as the parent directory, is identified by a special notation: ".." (two period characters).

Exercise 3.3

Use the cd command to move up one level in the directory tree.

$ cd ..
$ pwd
/home

Now use a relative pathname to return to your home directory.

$ cd natasha
$ pwd
/home/natasha

Now that you know where your home directory is, perhaps you'd like to know what files and directories are stored in it. You list files and directories with the ls command.

Exercise 3.4

Enter the ls command at the shell prompt.

$ ls
ales		myprog		poetry		zippy
mail		phonelist	projects

Notice that the list printed by ls does not show any distinction between files and directories. To get a more useful listing, enter the command again, adding "-F" option.

$ ls -F
ales		myprog*		poetry/		zippy@
mail/		phonelist	projects/

The "-F" option causes ls to produce a "fancy" listing. A special character, appended to the filename, is used to identify certain types of files (see table 3.1). Regular files remain unadorned. Some system administrators create an alias which causes the "-F" option to be added to the ls command automatically. If this is the case on your system, you will see the fancy listing without having to include "-F".

Table 3.1
Character Type of file
/ directory
* executable (program)
@ symbolic link (pointer to another file)

The ls command is a bit reticent; it won't actually tell you about all your files unless you explicitly ask it to do so via the "-a" option.

Exercise 3.5

Enter the ls command with the "-a" and "-F" options.

$ ls -aF
./		.bash_profile	ales		phonelist	zippy@
../		.bashrc		mail/		poetry/
.aliases	.mailrc		myprog*		projects/

Several new files showed up this time. "./" represents the current directory and "../" represents the parent directory. The other files beginning with "." are configuration files that control the behavior of various programs (for example, .bashrc and .bash_profile are used by the bash shell).

You can get more information about files you are listing by using the "-l" option with ls.

$ ls -alF
drwx--x--x   4 natasha  staff     512 Apr 21 23:33 ./
drwxr-xr-x  37 natasha  staff    5120 Feb  9 12:56 ../
-rw-r--r--   1 natasha  staff     121 Oct 19 2011  .aliases
-rw-r--r--   1 natasha  staff      76 Oct 19 2011  .bash_profile
-rw-r--r--   1 natasha  staff     137 Aug  4 2013  .bashrc
-rw-r--r--   1 natasha  staff     109 Oct 19 2007  .mailrc
-rw-r--r--   1 natasha  staff    2112 Apr 20 21:49 ales
drwx------  29 natasha  staff     512 Apr 21 23:33 mail/
-rwxr-xr-x   1 natasha  staff  213985 Jun 22 15:52 myprog*
-rw-------   1 natasha  staff    3467 Jan  7 11:46 phonelist
drwxr-xr-x  12 natasha  staff     512 Apr 14 16:13 poetry/
drwxr-xr-x  12 natasha  staff     512 Jan 22 11:57 projects/
lrwxrwxrwx   1 natasha  staff       9 May  2 2006  zippy -> /usr/share/zippy

The long listing generated by adding the "-l" option to ls produces information in seven columns as described in Table 3.2.

Table 3.2
Column Description
1 file type and permissions (discussed in the next section)
2 number of links (don't ask...)
3 owner of the file
4 group owner of the file
5 size of the file (in bytes)
6 time/date of last modification
7 name of the file

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.