This laboratory involves the use of common file and directory archiving and compression utilities available on UNIX-like systems.
Setup
Log into the FreeBSD machine for the class.  There's no script you have to run.
Standard Locations for Files
So far we've focused on your home directory and its subdirectories.  In this lab, we'll look at the entire file system hierarchy and show what files live where.  The laboratory will be specific to FreeBSD, but will discuss where other systems differ from FreeBSD.
There's a useful web site at http://www.pathname.com/fhs/pub/fhs-2.3.html – it describes a standard for the file system hierarchy for UNIX-like operating systems and looks like a good general reference for UNIX-like systems.
The Root Directory
Run:
% cd /
This sets the root directory as your working directory.  Now,
% ls -F
The -F option of the ls command adds some extra information to the directory listing.  You'll see the contents of the root directory, where:
•	each directory name has a slash appended to it
•	each symbolic link name has an @ appended to it
•	each executable file has an asterisk appended to it
There are a lot of entries that are common to all FreeBSD systems and some that are unique to this particular machine.  Here's some of the more significant entries, separated by function.
Booting the Operating System
The following files and directories are related to the start up of the operating system.
Entry Name	Description	
kernel, kernel.DEFAULT, kernel.GENERIC, kernel.old	These are files that contain builds of the operating system kernel.  The file named kernel is the one the boot loader loads at boot time in order to start up the operating system.  The other files with kernel in their name are other kernel builds that were probably used before the current kernel was built and installed.	
/boot	Contains static files used when booting the operating system.  These files generally do not change from build to build.  It's generally considered a good practice to separate static files (files that do not change depending on the operating system build) from dynamic files (files that change more frequently, like the kernel file in the root directory, which changes with each build).	
/modules, /modules.old	As part of operating system startup, the loader loads various code modules into memory after loading the kernel into memory.  These modules may have to do with special devices that are included into the build (but which don't have to be included in every build of the operating system), such as support for a scanner that connects to the computer via a USB port.  /modules contains the modules that are loaded by the loader.  /modules.old contains the modules that were loaded by the loader when the kernel file (now named kernel.old) was the active kernel file (and was named kernel).  Part of the installation process of a new kernel build renames /kernel to /kernel.old and /modules to /modules.old before installing the files for the new kernel build.	
Operating System Configuration
These entries are related to configuration of the operating system (including the legal bounds to configuration).
Entry Name	Description	
COPYRIGHT	Contains the BSD license agreement effective at the time this version of the operating system was published.  It's useful to look at it to see how free from restrictions it is, compared to a lot of other software license agreements.	
/etc	This directory (pronounced ett-see) is the root of the hierarchy where operating system configuration files live.  This will included configuration files for programs that are developed by third parties, but which are installed as part of the operating system.  This would include programs like sendmail (a mail transfer agent) or ntp (the network time protocol daemon).  Configuration files are data files and shell scripts, all readable by humans.  This is where the message of the day file lives (this is the file whose contents are presented when you log in).  This is also where the files containing information on the users and groups on the system are kept.	
Device Support
Entry Name	Description	
/dev	This directory contains entries for each hardware device supported by the operating system build.  This includes entries for each disk partition (the partitions that are mounted at mount points within the file system hierarchy).  There are a couple of devices worth mentioning, including /dev/null, the null device, which accepts all input (but trashes it) and provides nothing when asked to supply output, and /dev/urandom, the random number generator.	
/mnt, /cdrom	These directories serve as mount points for temporary file systems, such as ones contained on a floppy disk, a CD-ROM, or a tape drive.	
Applications and Data
Entry Name	Description	
/proc	This directory contains information on each running process, in the UNIX-like operating system tradition of trying to provide access to everything through the file system.  Run:  % ls -F /proc/1 and:  cat /proc/1/cmdline to see some of the information presented about running processes.  Also, run the following multiple times and compare the results:  ls -l /proc/curproc	
/root	The root user's home directory.	
/home	The directory that contains all of the non-root user home directories.	
/bin	Binary files containing required commands that are used by every user.	
/sbin	Binary files containing system commands, including commands to shut down the operating system, mount various devices, and the system information command dmesg.	
/stand	Binary files related to installation and recovery.	
/usr	The root of another hierarchy of binaries, configuration files, and data files.  We'll go more into detail later.	
Application Support
Entry Name	Description	
/tmp	The temporary directory, in which every user has the rights to create files.  Often, files in the temporary directory are deleted as part of the booting process (that's why this is for files that don' t need to persist for a long time).	
/var	Contains dynamic/variable data files.  The files within this hierarchy can change frequently.  We'll go more into detail later.	
The /usr Directory
The /usr hierarchy generally contains relatively static, mostly read-only information, including applications, configuration files, and data files.  This system doesn't quite follow this guideline, since its user home directories are at /usr/home.
Entry Name	Description	
X11R6	Files related to any X Windows installation	
bin, sbin	System utilities that are available only after the /usr file system has been mounted (usually done relatively late in the boot process)	
share	A hierarchy containing various files (like sample configuration files) from various software packages installed on the system	
include, lib, libdata, libexec	Contains files used to build applications	
src, obj	Contains source code and compiled object modules, respectively, for operating system builds	
local	Another hierarchy, containing third-party software installed on the machine not as part of the operating system installation.  There is a configuration directory for such software at /usr/local/etc.	
The /var Directory
The /var directory contains a number of frequently-changing files.  These include the following.
Entry Name	Description	
/var/account	Directory containing persistent log files for accounting	
/var/log	system/application status	
/var/spool	print jobs and outgoing mail	
/var/run	current nonpersistent system runtime information.  The /var/run directory contains files that contain the IDs of various processes, like the NTP daemon or the SSH daemon, and also contains the output of dmesg at boot time (/var/run/dmesg.boot)	
/var/tmp	another temporary directory like /tmp.	
Finding Files
Now that you know where everything is, how do you find it?  There are a number of utility programs that help you do so.  You will work with the following utility programs in this laboratory:
•	which:  a utility related to the PATH environment variable
•	(s)locate:  access to a database of files that exist on your machine
•	whereis:  a utility that combines which and (s)locate and adds more
•	find:  a utility that lets you find files having specific characteristics within a given file hierarchy
which
The which utility allows you to search for executable programs within a special list called the PATH environment variable.  An environment variable is a piece of named data that is held onto by the shell you are running.  Run the following:
% set
then run the following:
% env
You'll get a couple of lists, both of which contain lines within which there exists a name and value pair.  For those of you running the C shell, the list you get with the set command represent the configuration variables specific to the C shell, and the list you get with the env command represent the shell-independent environment variables.  If you run the Korn shell, you'll see the same result with both the set and env commands.  These lists of name/value pairs presented by the env command represent the variables in the environment.  One of these variables is PATH.
The PATH variable contains a list of paths within which the shell will search for executable programs, in the case that you specify an executable on the command line without specifying its absolute or relative path.
The entries in the value for the PATH environment variable are separated by colons (this differs from Windows, which has the same environment variable meaning the same thing, except the entries are separated by semi-colons).
You can also see the value of just the PATH environment variable by running:
% echo $PATH
Since the value of the PATH environment variable can refer to more than one path, what would happen if two of the paths contained an executable file having the same name?  A path that is closer to the beginning of the value has precedence over a path that is further toward the end of the value.
The which utility allows you to see the relative or absolute path containing the executable which would be run were you to specify just the name of the executable on the command line.
Run:
% which ls
You should see:
/bin/ls
%
The ls command is located in the /bin directory because it is a program that is supplied by the operating system and is intended for all users to use.
Run:
% which vilearn
vilearn is a program that tutors you in the use of vi.  You should see:
/usr/local/bin/vilearn
%
The vilearn executable is located in /usr/local/bin because it is a third-party program that was installed separately from the installation of the operating system.
Security Issues with PATH
Notice that all the entries in your PATH are absolute paths.  You can also add relative paths to the PATH.  Some people have the convention of adding the current directory to the beginning of their PATH; they do this by adding a single dot to the beginning of their PATH (recall the directory entry for the current directory is a single dot, and the directory entry for the parent directory is a double dot).  This has some negative security consequences, since if you run a program that a hacker compromises, the hacked program can create an executable program named the same as some common operating system utility.  What would happen if the hacked program created another executable named ls in the current directory?  If you ran the ls command by:
% ls
instead of:
% /bin/ls
you'd run the hacker's version of the ls command, which might do something else that you don't want, and call the real ls to cover for itself.
If you want to run an executable in the current working directory, you need to prepend the name of the executable with dot and slash:
% ./someLocalExecutable
You'll learn how to change the value of environment variables when we study command shells.
Adding Executables to Directories in Your PATH
If you add an executable to a directory contained within your PATH, and then attempt to run the executable by supplying just its file name on the command line, the shell might not be able to find it.  That's because the shell caches a list of executable files in your PATH, and looks in its cached list rather than searching the file system each time you run a command.  To fix this problem, run the rehash command from the command line to tell the shell to refresh its list of executables in your PATH.
(s)locate
The locate program on FreeBSD (and the slocate program on Linux) locates file system entities whose names include some search phrase.  The locate program uses a database of all system files; that database is updated weekly.
Run:
% locate vilearn
As a result, the locate program generates a list of all file system entities on the system whose name contains the search term vilearn.  This includes:
•	the executable at /usr/local/bin/
•	the man pages for the program at /usr/local/man/man1/
•	some accessory files at /usr/local/share/vilearn/
•	a ports system entry (that allows administrative users to build and install the program) at /usr/ports/editors/vilearn
•	data on the program's installation by the ports system at /var/db/pkg/vilearn-1.0/
whereis
The whereis utility combines information from which and locate in order to provide information on a program's installation.
Run:
% whereis vilearn
You should see:
vilearn: /usr/local/bin/vilearn /usr/local/man/man1/vilearn.1.gz /usr/ports/editors/vilearn
%
The whereis utility shows the location of the executable at /usr/local/bin/, the location of the man page at /usr/local/man/man1/, and the location of the port information at /usr/ports/editor/vilearn/.
find
The find program is a general utility for ad-hoc searches within a file system hierarchy.
The basic syntax of the find command with the most useful/common arguments is:
find pathToBaseDirectory -name pattern -type typeDesignator -path pattern -perm [+|-]mode -user userName -group groupName -size number[c] -ls -exec commandNameAndArgumentList
Here's the meaning of the above arguments to find.
Argument	Description	
name	Accepts a pattern (including the use of *, ?, and [], escaping them with a backslash \) that is used to find matching names of any file system entities encountered	
type	Accepts a designator specifying a type of file system entity:  d is directory, f for file, l for symbolic link	
path	Accepts a pattern, like for the name argument, that is used to find matching paths within which to search for file system entities	
perm	Accepts a mode setting, like for chmod, to either exactly match (=) a permission on a candidate entity, or to specify that the permission on the candidate entity must (+) or must not (-) have the specific permissions enabled	
user	Accepts a user name or a user ID that specifies a specific user with which to limit search results to only those entities owned by that user	
group	Like user, except accepts a group name or group ID	
size	Limits search results to only those file entities having a file size matching the value passed with this argument (either in 512-byte blocks or in characters/bytes)	
ls	Presents information on the found entity much like you would see with ls -il	
exec	Specifies that the specific command line be invoked for each matching file system entity (the specified command line must be terminated with “;”, including the surrounding quote marks, and must use “{}”, including the surrounding quote marks, to refer to the matching entity)	
Exercises
List all the symbolic links within the /etc hierarchy by running:
% find /etc -type l
The value passed with the -type argument is a letter ell, not a digit one.
Find out what the symbolic links are pointing to by running:
% find /etc -type l -exec ls -l "{}" ";"
This tells find to invoke the ls command for each symbolic link entry it finds.  The quotes around the last two arguments are required.  The {} notation refers to the matching file system entry found.  The semicolon ends the command line passed as the value of the -exec argument.
You can generate similar results using a simpler command line as follows:
% find /etc -type l -ls
The -ls argument does essentially the same as the -exec argument did in the previous command.
You can find all the file system entities within /etc whose names contain the string pd by:
% find /etc -name “*pd*”
You must surround the value for the -name argument in quotes to prevent the shell from treating the asterisks as metacharacters.
You can broaden your search by finding all entities whose names contain a p followed by a d by:
% find /etc -name “*p*d*”
You can restrict the above search to only match directories with that name pattern by:
% find /etc -name “*p*d*” -type d -ls
You can find all file system entities within the /etc hierarchy for which the user (owner) permissions are read, write, and execute by:
% find /etc -perm u=rwx -ls
You can find all file system entities within the /etc hierarchy not owned by root by:
% find /etc \! -user root -ls