Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Page 1 of 10

EE1030
Introduction to UNIX
Salt Lake Community College
Department of Engineering
(Electrical Engineering) MHM

ASSIGNMENT #2 FILES&DIRECTORIES

Unix Files and Directories


This lesson focuses on the Unix file system, and how to manipulate files and navigate directories.
We will begin by looking at basic Unix commands for manipulating files and directories. In Unix,
a file can be one of three types: a text file (such as a letter or a program), an executable file (such as
a compiled program), or a directory (a file ‘‘containing’’ other files).

When you consider that there are thousands of users of the local workstation network, you will
realize that the computers must keep track of tens or hundreds of thousands of files. Unix uses
directories to organize these files, much like a filing cabinet uses drawers and folders to keep track
of documents.

The Unix file system is organized around a single structure of directories, where each directory can
contain more directories (often called subdirectories) and/or files. The entire file system, often
spanning many machines and disks, can be visualized as a tree. Picture this tree as growing upside
down, with the root at the top and the leaves toward the bottom. The leaves are all text and
executable files, while the root, trunk, limbs, branches, and twigs are all directories.

The file system is called the directory tree, and the directory at the base of the tree is called the root
directory. Every file and directory in the file system has a unique name, called its pathname. The
pathname of the root directory is /.

As a Unix user, you are given control over one directory. This directory is called your home
directory, and it was created when your account was established. This directory is your personal
domain, over which you have complete control. You are free to create your own subtree of files
and directories within your home directory.
To determine the pathname of your home directory, you may enter the following command.
cd; pwd

Shell Command pwd: .....Display Working Directory


[sophist/home/smartpe]$ pwd
/home/smartpe
Page 2 of 10

Try: pwd

Also you may you may use cd; pwd. The first part of the command makes sure that you are indeed
connected to your home directory; the second command displays its name.

[sophist/home/smartpe]$ cd
[sophist/home/smartpe]$ cd

Try: cd ( Copy and Comment in your document)

Now

Try: cd;pwd (Copy and Comment)

Everyone has a different home directory, but two things are certain. The pathname of your home
directory will start with a slash (everything is rooted in the root directory) and it will end with your
user name. For example, suppose that a user smartpe has a home directory /home/smartpe as you
have noticed in your Try:.... From this, we can tell that the root directory / contains a subdirectory
called home, which contains a subdirectory called smartpe . Every directory has a pathname that
shows the sequence of directories that lead from it back to the root.

Below, you will learn how to explore the file system at large as well as how to create directories
and files inside your own small piece of it. Be sure to try out the example commands as you learn
about them. Otherwise, you won’t learn very much.

Examining Directories
What files and directories are contained in the working directory? You can find out with the
command
ls [list directory]

Try: ls

which lists the contents of the working directory. If you are just getting started with Unix, there
will only be one or two files already present, which are left over from the previous lesson. Notice
that only the names of the files are displayed, not their full pathnames. But if you know the name
of the working directory, and you know the name of a file within it, you can easily figure out that
file’s full pathname. What would be the full pathname of a file named ‘‘smartpe_a’’ in your home
directory?

There are actually several more ‘‘hidden’’ files present in your home directory. Unix does not
Page 3 of 10

display files whose name begins with a dot (so-called dot files) unless you insist. You can insist by
issuing the command
ls -a [list directory, including hidden files]
The option -a, pronounced ‘‘dash a,’’ asks that all files (even the hidden dot files) be displayed.
Some dot files are used to change the default behavior of Unix, X, emacs, and other applications.
We will discuss their use in a later lesson.

There are many other ls options. One of the more useful ones is the -F option,
ls -F [list directory and identify files by type]
which marks files that are directories by appending a ‘‘/’’ to their name and marks files that contain
executable programs by appending a ‘‘*’’ to their names. Your account probably has ls -F
established as the default, so that every time you enter ls you are actually executing ls -F.

Another useful option is the -l option,


ls -l [list directory with long listing]
which gives a long listing for each file. Each listing gives the file name, the access privileges
(which we will discuss later), the file size in bytes, and the date of the last modification.

More Options. There are many more ls options, which you can read about by using the man
command.
man ls [display the manual page for ls]
(Use the space bar or the Enter key to move through the man page, and use q to quit.) The man
command will display information about any Unix command, and is a useful way to learn about the
fine points once you’ve mastered the basics.

What do you do if you want to specify more than one option to ls (or to any other Unix command,
for that matter)? If, for example, we wished to get a long listing of all files (even the hidden ones),
we could type
ls -a -l [list directory with long listing, including hidden files]
Alternatively, we could write the options together and simply type
ls -al [list directory with long listing, including hidden files]
and achieve exactly the same thing.
Try: ls -a -1 (Comment)
Try: ls -a1 (Comment)
Page 4 of 10

Making Dirctories and Changing Directories


To this point we have never moved away from your home directory. Let’s learn how to navigate the
directory tree. Before we do this, let us create some directories ( Remember to use your login
name instead of “smartpe” to identify you.
Utility: mkdir -p new directory name
The mkdir utility creates a directory. The -p option creates any parent directories in
the new.DirectoryName pathname that do not already exist. If new DirectoryName
already exists, an error message is displayed and the existing file is not altered in any
way.
Try: mkdir smartpe_aa1
Try: ls ( Copy and Comment)

Try: mkdir smartpe_aa2 ( Copy and Comment)


Try: ls ( Copy and Comment)
Try: mkdir smartpe_aa3 ( Copy and Comment)
Try: ls ( Copy and Comment)

Shell Command cd ( directoryName)


The cd shell command changes a shell’s current working directory to be a new
directory (directorNanme). If the directoryName argument is omitted, the shell is
moved to its owner’s home directory.

Try: cd smartpe_aa1 ( Copy and Comment)


Try: mkdir smartpe_aa1_b1 ( Copy and Comment)
Try: pwd ( Copy and Comment)

Command cd .. move up one level


Command cd changes to the original directory
Try: mkdir smartpe_aa1_b2 ( Copy and Comment)
Try: pwd ( Copy and Comment)
Try: cd ..( Copy and Comment)
Try: pwd ( Copy and Comment)
Try: ls ( Copy and Comment)
Try: cd ( Copy and Comment)
Page 5 of 10

You have noticed the command cd takes a directory as an argument and makes that
directory your working directory. There are two ways to specify the name of a
directory or file. One way is to give the full pathname, and the other way is to give
enough of the pathname to let Unix know how to get to the desired directory from
the working directory. We’ll look at these two methods in turn.

Absolute Pathnames. You can give the full pathname of the desired directory or
file. For example, if samrt wanted to go to his home directory, he could use the
command
cd /home/ smartpe
Or, if he wanted to connect to the smartpe_aa1 directory within his home directory,
he could issue the command
cd /home/smartpe/smartpe_aa1

Try cd /home/smartpe/smartpe_aa1

As you experiment with moving around the directory tree, be sure and get used to
looking at the prompt to verify that things are working as you expect. If you get
completely confused, you can use pwd to find out exactly where you are.

Typing a full pathname can be a real pain, especially when it is a long one.
Fortunately, there are several convenient abbreviations. Unix will treat a tilde
followed immediately by a user name as an abbreviation for the full pathname of that
user’s home directory. For example, if you wanted to connect to a user jones’ home
directory, you could do so with
cd ~Smartpe
Use this form of abbreviation (with your user name, of course) right now to
reconnect to your home directory.

If it is your home directory in which you’re interested, and not someone else’s, there’s
a second abbreviation. A tilde all by itself stands for your home directory. So, you
can connect back to your home directory with
cd ~ [connect to your home directory]
Try: cd smartpe_aa1 ( Copy and Comment)
Try: ~smartpe ( Copy and Comment)
Finally, here’s the ultimate shortcut. If you issue the cd command with no argument,
you will
connect to your home directory:
cd [connect to your home directory]
Relative Pathnames. Be sure that you are connected to your home directory. You
should know how to do that without any help.
Page 6 of 10

You can also specify a directory or file by describing to Unix how to get to the
desired directory or file from the working directory. For example, suppose that you
want to connect to your ‘‘smartpe_aa1’’ directory from your home directory. You can
do this by simply issuing the command
cd smartpe_aa1

Try: cd smartpe_aa1
Unix knows that the pathname argument to cd is a relative pathname because it does
not begin with a slash or a tilde, as all absolute pathnames do. When Unix
encounters a relative pathname, it glues the relative pathname onto the end of the full
pathname of the working directory to obtain an absolute pathname.

You should now be connected to your ‘‘smartpe_aa1’’ subdirectory. You can connect
back to your home directory by issuing the command
cd ..
When ‘‘..’’ appears in a pathname, it refers to the parent of the current directory. So
the net result of issuing the cd command above is to move one step closer to the root
of the tree. You should now be connected to your home directory.

Relative pathnames can be composed. For example, to connect to the ‘‘other’’


directory of your ‘‘ smartpe_aa1’’ directory (from your home directory), you could
issue the command
cd smartpe_aa1/other
To get from the ‘‘other’’ directory of your ‘‘example’’ directory to the ‘‘brother’’
directory of your ‘‘example’’ directory, you could navigate as follows:
cd ../brother
That is, you navigate up one level in the directory tree and then go back down to the
‘‘brother’’ directory.

Using Absolute and Relative Pathnames. You might be wondering when you
should use absolute pathnames and when you should use relative pathnames. It is
entirely a question of convenience. If you need to name a directory that is ‘‘close’’ to
your working directory, then relative pathnames are quite convenient. This will
usually be the case, since you’ll do most of your work in or near your home directory.

On the other hand, if you need to name a directory that is ‘‘far away from’’ your
working directory, then you should use an absolute pathname.

We have been looking at using pathnames as arguments to the cd command, but


there are many commands that also take pathnames as arguments. One that you
already know is the ls command. If you don’t give ls an argument, it will give you
a listing of the working directory. However, you can obtain a listing of any directory
Page 7 of 10

by following ls with the absolute or relative pathname of a directory.

For example, if you’d like to see what’s contained in the root directory, you could do
ls /
If you’re connected to your home directory and you’d like to see the contents of the
example subdirectory, you could do
ls example
If you’re interested instead in the ‘‘other’’ subdirectory of your ‘‘example’’ directory,
you could do
ls example/other
and so on.

Examining Files
You earlier learned how to examine the contents of directories with the ls
command. Connect to your ‘‘example’’ directory and we’ll experiment with looking
at the contents of files.

One way to look at a file is to read it into Emacs. But sometimes we’re interested in
taking a quick look at a file and don’t need to edit it. The cat command (cat stands
for catenate) displays an entire file on the screen. If we wanted to display the file
‘‘long,’’ we would enter
cat long
As you can see, there are some problems with displaying long files. The file is
several screenfuls long and zips by too fast to read. Luckily, we also have the more
command. If we enter
more long
then only one screenful at a time is displayed. Hitting the space bar will display the
next screenful, hitting the Enter key will display the next line, and typing a ‘‘q’’ will
skip the display of the rest of the file and quit.

If you have already reached the end of the file, then typing ‘‘q’’ will result in an error
message like ‘‘q: Command not found.’’ That’s because more is no longer running
and your ‘‘q’’ command was interpreted by Unix instead of by more.

Manipulating Files and Directories


Use pwd to see what your working directory is. If you are not in your home
directory, use cd to connect to it.
Try: cd .. Or do it several time to get into your home directory)
or Try cd ~smartpe
Page 8 of 10

Creating Files and Directories. You can create a new (empty) directory using the
mkdir command:
mkdir newdir [Make a new directory called newdir]
You can verify that the directory has actually been created by listing the contents of
your home directory.
Try: mkdir smartpe_c_1
Relative pathnames work the way that you might expect with the mkdir command.
For example, suppose that you now want to create a directory called ‘‘newerdir’’ in
your ‘‘newdir’’ directory. One way to do that is to connect to ‘‘newdir’’ and then use
‘‘mkdir’’. Another way is
mkdir newdir/newerdir

Try: mkdir smartpe_c_1 / smartpe_c_1_2

When you need to create a file, you will generally do it by using Emacs( a text editor
that will be covered in the other assignment). Suppose that you’d like to create a file
called ‘‘newfile’’ in your ‘‘newdir’’ directory. You should select the ‘‘Open File...’’
option from the ‘‘File’’ menu. Emacs will then prompt you for the name of a file to
read.

Deleting Files and Directories. By now you know how to create and examine files
and directories. It is almost as important to know how to get rid of unwanted files
and directories.

To delete a file we use the rm command. (It helps to know that ‘‘rm’’ stands for
‘‘remove’’.) Connect to your ‘‘Smartpe’’ directory, which should contain some files.
Let us create a file and then delete or remove it by the command rm.
Try: cat >smartpe_test ( Tpe a few sentences , Then ^c)
Try: ls
Now
Try: rm smartpe_test ( Comment)

Depending upon how your defaults are set up, Unix may ask you to confirm that you
really mean to delete the file. Just enter a ‘‘y’’ or a ‘‘yes’’ to confirm.

Connect back to your home directory.

The command for deleting an empty directory is rmdir. More often, you will want
to delete a directory and everything that it contains. The command for this is rm -r.
Page 9 of 10

For example, if you wanted to delete the ‘‘newdir’’ directory and everything that it
contains, you would issue the command
rm -r newdir

Try: rmdir smartpe_C_1 ( Comment)

Now

Try: rm -r smart_c_1 ( Comment, note that the directory was probably empty in the
first case)
You can wipe out a lot of files and directories with this command, so be careful.

Copying Files and Directories. Often you will want to copy a file from one place to
another. For example, an instructor in a class might place a file into a central location
and ask everyone in the class to make a private copy. Or you might decide to make a
backup copy of some file before modifying it.

To copy a file we use the cp command.:


Try: cat> smartpe_d1 ( Type a few sentences )
Try: cp smartpe_d1 smartpe_d2 (Comment)
Try: ls ( comment)

Either argument to cp can be an absolute or relative pathname. For example, to copy


‘‘long’’ to a file called ‘‘longcopy’’ in the ‘‘brother’’ directory we issue the command:
cp long brother/longcopy
You should now use ls to verify that both copies were made.

You can also use cp to copy a directory and all of the files that it contains. Just as
with the rm command, however, we must specify the -r option to do this. For
example:
cp -r brother sister
Check now to verify that the new ‘‘sister’’ directory contains the same things as the
‘‘brother’’ directory.

Renaming Files and Directories. Sometimes you will want to change the name of a
file or directory without copying it. To rename a file we use the mv command. (It
helps to know that ‘‘mv’’ stands for move.)

For example, we can change the name of ‘‘longcopy’’ to ‘‘copylong’’ with


mv longcopy copylong

Try: mv smartpe_d1 smartpe_d3


Page 10 of 10

Try: ls (Comment)

We can also rename directories. (Notice that when renaming a directory, we do not
use the ‘‘-r’’ option.)
mv other else

File and Directory Summary


Here is a summary of the commands that we covered in this section.
SUMMARY OF UNIX FILE SYSTEM

Directory abbreviations

. Current directory

.. Parent of current directory

~<user> Home directory of user <user>

~ Your home directory

Exploring the file system

pwd Print name of working directory

cd <pathname> Connect to directory <pathname> (.


by default)

ls <pathname> List contents of directory


<pathname> (. by default)
-a List even the files that begin
with "."
-F Mark directories and executables
in the listing
-l Display extra information

You might also like