![]() |
|||||
Duke
University |
|||||
Computing & Statistics Lab | |||||
Chapter 3 - Directories and Files: What am I looking for?
Where is it and why do I want it?
Your Home Directory -
The UNIX directory structure starts from what is called the root
directory, symbolized by the forward slash (/). It is like an inverted
tree in that it branches out as you traverse down through its structure.
Every user is assigned his or her own login or home directory usually
with the same name as the person's username. To check the name of
your home directory, issue the command 'pwd' (print working directory)
right after you log in. You will see something like this -
j_user@rubin> pwd
/home/garcia/j_user
The first line shows the command prompt as we have defined it on the
geri system. It is your username, the 'at' symbol and the name of the
computer to which you have logged on. Next is the 'pwd' command that you
are to enter. The following line shows the result of the 'pwd' command,
the path to the home directory for the user named 'j_user'. In this case
it is an absolute path (as opposed to a relative path, to be discussed
later) descending from the root (/), through the directory 'home', then
through the directory 'garcia' and ending up in the directory 'j_user'.
Your home directory is your space for storing the files you have
created. You will also find a number of 'dot' files (files whose names
begin with a period, such as .cshrc). Most of these are configuration
and preference files put there by the system and by various programs
that you run and for which you make customizations. Use the command
'ls -a' to see all files, including the 'dot' files.
Paths -
In a computer system a path is the route taken through the directory
hierarchy to get to a specified file or directory. There are two types of
paths - absolute and relative. We have seen an example of an absolute
path, also known as a fully qualified path because it specifies
explicitly every directory from the root (/) to where it is you want to
go. For example, '/home/garcia/j_user/temp/bigfile'.
But what if j_user had just logged on and wanted to access bigfile.
The system knows that j_user is already situated in /home/garcia/j_user,
and j_user can now specify just a relative path. In other words, just
the 'temp/bigfile' part, because the system knows to prepend the
'/home/garcia/j_user' part.
Subdirectories -
Think of your home directory as a one drawer hanging file cabinet.
Few people would just open the drawer and put all their papers, magazines
brochures, booklets, maps, etc. in with no organization. Typically you
start with a hanging file folder with a name on the tab and put related
documents into it. You might even put a number of manilla file folders
into one hanging file folder with documents in each. Your folders usually
have tabs with identification markings on them indicating what the
contents are. You can create the same kind of structure in your home
directory.
Create a new directory (also called subdirectory or folder) in your
home directory with the 'mkdir' command and give it an appropriate name,
just as you would put on the tab of a hanging file folder. 'mkdir' stands
for 'make directory', and it takes just one parameter, a name for that
directory. Here's an example - 'mkdir temp'.
File and Directory Names -
Directory names follow the same rules as file names. You can use
most any printable character on your keyboard. Spaces are forbidden. Use
the dash (-) or the underbar (_) instead. Remember that UNIX is case
sensitive, so 'MyFiles' is not the same name as 'Myfiles' which is not
the same as 'myfiles'. Other than the dash or underbar, stick with letters
and numbers for file names. Many of the symbols on the keyboard have
special meanings to UNIX and will give unpredictable results in file
names.
The * (aka. asterisk or star) is a wildcard. Let's examine the
wildcard's behavior with some examples:
ls * - lists all files beginning with a character other than the period
ls .* - lists all files beginning with a period
ls *.* - lists all files that have a period in their name
ls *.txt - list all files ending in '.txt'
ls *sas* - lists all files with 'sas somewhere in their name
(ls will also drop down through subdirectories that match your file name
criteria unless you tell it not to with the -d option.)
Finding What You Are Looking For -
After a while you will accumulate numerous files and directories. Now
the trick is finding what you are looking for. You may not remember the
path or the file name, just that it had something to do with a paper
written by Dr. Busse. What tools do you have to find it?
If you do remember the directory in which you stored the file, you
would use the command 'cd' (change directory) to move to that directory.
If you are not sure of the file name, use the 'ls' command to look for a
file with a name that gives a hint as to its contents. (Plan ahead and use
short but meaningful file and directory names. A file name like 'a.t' is
not meaningful, and a file name like
'preventing_distributed_denial_of_service_attacks_with_egress_filtering.txt'
is practically an abstract.)
You issue the 'ls' command, but you have so many files that the a's
and b's scroll off the screen faster than you can read them. What to
do? You can 'pipe' the output to the 'more' command. Here's what it would
look like 'ls | more'. The vertical bar is the symbol for the pipe, and
the 'more' command displays the output one screen at a time, allowing you
to scroll forward one line using the Return or Enter key, one screen using
the space key or backward using the 'b' key. Press the letter 'q' to if
you wish to quit the display before reaching the end.
Use the command 'ls -l' to get a 'long listing' of the files in a
directory. This will give you the size of the files and the date and time
of the last modification. That could be helpful in determining just which
file you are looking for. ('ls -l' will give you more information than
just date and time. Do 'man ls' and scroll down to '-l' to get the full
story.)
What if the file names are no help in finding what you want, but you
know the contents of the file you are looking for have something to do a
particular topic or keyword. You can look into one or more files with the
'grep' command. The syntax is 'grep options keyword filename'. We are
still looking for that file about Dr. Busse, so we say 'grep -i Busse *'.
Since the asterisk is a wildcard, this will search all the files in the
current directory looking for the keyword Busse and will display on your
terminal those lines in the file that contain that word. The -i option
says to ignore upper and lower case, therefore you might see lines with
'BUSSE' or 'busse' or any other combination of upper and lower case.
If you do know the exact name of the file you are looking for but do
not know what directory it is in, you can use the 'find' command. It looks
like this: 'find ./ -name unixish.h'. It starts from the current working
directory, symbolized by ./, goes through all subdirectories and looks for
a file named 'unixish.h'.
And it returned the following:
./temp/local/lib/perl5/sun4-sunos/5.002/CORE/unixish.h
Meaning that the file was found deep within the labyrinth of my
current working directory (./), through my temp subdirectory and through
six more subdirectories.
If I were to cd to temp/local and issue the same command, the output
would be - './lib/perl5/sun4-sunos/5.002/CORE/unixish.h'. See the
difference? The path to the file you are looking for is relative to the
directory you are in at the time you issue the find command.
| |||||
About Us | Weekly Bulletin | Center Report | People | Research | Education | Service | Pepper Grant | Links | DUMC | Duke Copyright 2004 Duke University Center for the Study of Aging and Human Development Questions or Comments? Contact Webmaster |
|||||