The first thing you need to learn is how to traverse and examine your file system. In UNIX, directories are used to organize files in a hierarchical structure. Rather than clicking directories to enter them and seeing each file as an icon, you use a series of commands and lists to view and traverse a UNIX file system from the command line.
If you're using a
UNIX-like operating system for the first time but you've used a
DOS or Windows command line in the past,
ls is roughly equivalent to
dir. It's short for List Directory.
ls can be used with a variety of
command-line options to get detailed lists, show hidden files,
recursively list subdirectories, and so on. Enter the following examples:
$ ls $ ls -l $ ls -a $ ls -R
If you're sitting on the command line thinking about your next action, a quick
ls can help you visualize where you are and what
you're doing. Think of it almost like a screen refresh in a graphical user
interface, updating you on the system's current state.
To traverse the file system, use
cd to change directories. Type
cd and then the name of the directory you
want to go to. If you use a leading /, the directory you name is an
absolute directory path: It starts from the
root of your file system. If you don't use a leading /, the directory is a
relative path: You start from your current
working directory. For example, if you're in your home
directory, (/home/tuser), and you want to move to a
cd followed by the directory name you
want to go to. For instance, if you have a Documents directory, you can
type the following:
$ cd Documents
Because you start in /home/tuser/, this command puts you in /home/tuser/Documents/.
In contrast, you can use an absolute path name to specify a location. As an example, you can move into /tmp and then back to your home directory as follows:
$ cd /tmp $ cd /home/tuser
In UNIX, special directory names make traversing the file system easy.
The three most important ones refer to the current directory, the current directory's parent,
and the user's home directory. The current directory is specified by a dot (or period).
For instance, if you type
$ cd ., you remain in the current directory, which is still
/home/tuser/. This character becomes especially important
when you're running executables in the current working directory. By default, many UNIX
shells search application directories for applications, but it does not search the current working
directory. You can always explicitly refer to files and applications in your current working
directory by using ./ preceding a filename. Parent directories are referred
to with two dots (or two periods). To traverse to your current working
directory's parent, type the following:
$ cd ..
If you were in /home/tuser before, now you're in /home. To demonstrate the third special directory name, use the shortcut to get back to your home directory (the tilde character). Type the following command:
$ cd ~
To check your current directory, you can use
pwd, which stands for Print Working
Directory. It tells you where you are in the file system, which
helps you determine what to use when specifying relative path
names. Try experimenting with the three special directory names,
absolute paths, and relative paths to traverse your file system. At each step,
pwd to check your current location.
Check your current directory (you should be in your home directory if you followed the steps in the previous section):
rmdir are used to create and remove
rmdir works only if the directory is
empty (it doesn't remove files).
Try the following commands:
$ mkdir TUTORIAL $ cd TUTORIAL $ pwd $ ls
You're now in your newly created TUTORIAL directory, and it's empty.
You can also get to this directory by using the
~ (tilde) character. To get to
$ cd ~/TUTORIAL $ pwd
Now that you know how to move around directories and get listings, you're ready to look at the directory layout on a typical UNIX distribution. You can organize a UNIX file system several ways. This tutorial discusses a few root-level directories that are common to most UNIX-like distributions. There are other important root-level directories, but this is where you'll find yourself operating in most cases:
/home (or /users) /etc /bin /sbin /usr /car /tmp
/home is where user directories are found. For instance, the
is located in /home/tuser.
/etc is the directory used to store most system-wide settings, including startup scripts and network configuration files. You need root access to edit most files in this directory.
/bin and /sbin are the directories
used to store system executables (like the
commands you're learning about in this tutorial).
/sbin is used for system commands, such as
/bin is used for user commands.
Applications are usually installed in /usr. A /usr/local/ subdirectory often holds applications installed that aren't part of the base distribution.
/var is the directory where things like log files, which are continually updated, are stored.
Temporary files are stored in /tmp. This directory is usually writable by all users on the system, and old files are periodically removed on some systems.