Unix Basic Tutorial

Command Line Environment Directory Structure and Navigation

Everything in a Unix environment spreads outward from a single "root" directory, much like a tree trunk and its branches.

Image Description

Typical Unix directory tree structure

The root directory is the top level, and is denoted by a slash (/). Other directories are created below the root directory - typically, you will find a "bin" directory, which contains binary files required for commands and processes like those we'll cover next, as well as a "tmp" directory for temporary files, and directories like "home" that contain information for individual users.

Within the linux simulator we'll be using, the directory structure looks like this:

Image Description

Online linux simulator directory structure

In the online simulator, we are not assigned a user account in the home directory. Instead, we will be working from the "root" subfolder. The other directories that we will explore in the simulator are read-only and un-editable.

Click through to the linux simulator (which opens in a new window) if you haven't done so already. If you have already clicked through, refresh the page.

To see that we are indeed located within the "root" directory, which is a subdirectory of "/", type the letters "pwd" at the prompt in the simulator then press enter.

/root # pwd /root /root #

"pwd" is a command that stands for "print working directory". You can use this command to find out your location within the Unix file structure at any time. Here we can see that we are in "root" which is a subdirectory of the main root directory, "/".

Next, let's find out what's in our current working directory. List the contents of the current working directory using the list command, "ls", in the simulator.

What did you find? The simulator should have shown the two entries below:

/root # ls dos		hello.c

What do we know about these listed entries? Are they directories or files? Can you edit them, or are they read-only? The ls command by itself simply lists all the contents of a directory. You can add options to it to find out more information.

Try typing "ls -l" into the simulator and press enter. What kind of information are we shown now? The image below shows some examples of the standard information that is returned with the -l, long-format option.

Image Description

Standard long format and labels for each field

The first character in the listing denotes the type of content:

  • d = directory,
  • l = link,
  • - = regular file.

This is a listing of the most common content types.

The next 9 characters denote permissions for the user, group and others (also called public). "r" denotes read privileges, "w" denotes write privileges, and "x" denotes execute privileges.

Thus, the first entry in the listed contents above is a directory in which the owner, group and all others have read, write and execute privileges. The second entry is a regular file that can be read, written to, and executed by the owner but is read-only for the group and other users.

The remaining fields returned in the long format listing (from left to right) include the number of links, owner, group, size (in bytes), date modified and the file or directory name.

Returning to our long listing in the current directory within the online linux simulator, we can see that "hello.c" is a regular file (a C program, in fact), and "dos" is a symbolic link to another "dos" directory that is in the "root" directory.

There are a few other common ls options that you may find useful.

Command and Option

Description

ls -a

List all files, including hidden files

ls -lt

List files sorted by the time last modified

ls -R

List files recursively (descend through all directories and list files from those sub-directories as well)

ls --help

As with most commands, if you add a --help to it, it will return all of the possible options for that command. Note there are two dashes, and no space between them.

Now that we know what's going on in our current working directory, let's change to another directory to see what's there. We know that "dos" leads to a directory, so let's use that.

Type "cd" followed by a space and the name of a directory you want to change to (dos) into the simulator and press enter. Remember: all commands must be followed by a space before their target file/directory or process!

Now you should be in the "dos" directory. Notice that the prompt changed to show you the current directory. This is not always the case in Unix. Explore the contents of the directory with some of the listing commands we introduced earlier and then answer the question below.

Question

Within the dos directory

a) there are files
b) and directories.

You can tell that asm-1.9 is a directory by using the "-l" option and noting that that line begins with the letter "d". Additionally, it shows as a different color in the simulator. Finally, you could try to change directories into it - doing so will work for directories, but not for files.

Next let's change to the asm-1.9 directory. There are several files listed in that directory. In addition to easily listing all the contents in a directory, Unix allows users to quickly show the contents of individual files. One way to do this is the concatenate command, "cat". Try entering "cat" followed by one of the filenames in asm-1.9, and then press enter. Readme.txt is an easy one to view (don't forget a space between the command and filename!)

You should see the contents of readme.txt printed to your screen until the end of the file is reached, like so:

 ....  Format is: Symbol-Name File-Name Line-No. Number-of-Refs Symbol-Type Value-Hex Value-Dec   To print cross references: C:> lister -x asm.lst .... PathSize                        asm.s                2        Equate        0040        64   asm.s        148   asm.s        153   2                references found ...   Format is: Def: Symbol-Name File-Name Line-No. Number-of-Refs Symbol-Type Value-Hex Value-D ec Ref:        File-Name        Line-No.                                    REFERENCES  1. Tannenbaum A S, "Operating Systems : Design and   Implementation", Prentice Hall of India, New Delhi,   1989.  2. Rector R and Alexy G, "The 8086 Book", Osborne/   McGraw-Hill, California, 1980. ~/dos/asm-1.9 #

Contents of long files can be viewed stepwise by using the "more" command. This is similar to "cat", but it prints the file contents to screen and allows the user to step through them using the spacebar. To exit the "more" command, press "q" for quit.

Finally, let's move from the asm-1.9 directory back to the dos directory. Try getting there using the cd command.

What did you type? And what did it do?

Since you are in a sub-directory of the directory you're trying to access, the "cd" command must be used with an absolute path, or an appropriate relative one - we cannot simply type "cd directoryname" like we did before, because the directory we want to access is no longer below our location in the directory structure.

Here's the error message you would have received if you simply tried "cd dos":

~/dos/asm-1.9 # cd dos sh: cd: can't cd to dos ~/dos/asm-1.9 #

To change back to the "dos" directory, we can use the absolute address "cd /root/dos" or we can use a relative path "cd .." where ".." indicates the directory above your current working directory. "." is always the current directory. To access the directory above /root from the asm-1.9 directory, we could type "cd ../.." as that directory is two directories up from our current location.

For quick navigation and efficient command-line usage, here are a few commands to cut down on your typing.

Command

Description

<TAB>

Before completing a file or directory name in the command line, press TAB to autocomplete the name based on the list of files/directories within this directory.

~

When navigating, this is a synonym for your home directory.

<UP ARROW>

Go chronologically backwards through the previous commands you have run from the command line.

<DOWN ARROW>

Go chronologically forwards through the previous commands you have run from the command line. (Only works after pressing <UP ARROW>)

Command

Description

Usage

pwd

print working directory

pwd

ls

list working directory contents

ls

ls -l

list working directory contents with a long-listing

ls -l

cd

change directory

cd directory

Paths

Description

Example

/

root directory if first character or sub-directory if any other character

"cd /"

Changes directory to the root of the file system

.

current directory

"ls ."

Lists the contents of the current directory (this is implied by typing "ls")

..

directory one level up from current directory

"cd .."

Changes directory to one level up from current directory