Navigating the File System


File system navigation is commonly one of the first lessons taught in Unix, not only because of the reasons that were just discussed, but simply because it helps to know where you are and where you are going in order to get to your destination. It's like planning a tripyou don't just walk out, hop in the car, and go; there should be some planning involved, especially based on the complexity of the trip you are about to take. In Unix, knowing what commands to use is your preparation and plan, and once mastered, it will have you working on data files, editing web pages, and so on.

The most basic commands for dealing with the Unix file system are the ones for moving between directories and finding out what files are in them.

Using the pwd Command

For users of Windows or MS-DOS, one command you would commonly type at a command prompt is dir. This command (which stands for directory) will list the contents of the directory you are currently in; if you specify the entire path statement to another directory, the command will also let you see the contents of that directory. In Unix, the pwd (present working directory) command asks your machine to tell you what directory you're currently in. The way that Unix displays this information is different than in Windows. For example, in Windows, the path C:\Windows\Temp tells you that there is a folder (directory) called Temp in a folder called Windows, and the Windows folder is currently on the C:\ drive of the operating system. In Unix, the path will seem different; for instance, /priv/home/rob/temp/ would be an example of a path in Unix. Here, the simplest explanation of a path is that it describes the shortest set of directories through which you must travel to get to the current file or directory from the root directory.

Any time you're at the shell prompt, type pwd and Unix will tell you where you are as you progress in your Unix studies. The following is an example of this command at work:

 > pwd /priv/home/rob/temp/ 

Get familiar with this layout; you will be using path statements more and more as you progress in your Unix studies.

The Home Directory

In this book we have talked about the home directory a few times but have not yet fully discussed it. In this section we will now cover the importance of the home directory for the Unix user.

Each user in a Unix system should have one directory assigned to her for her personal use, to store data. This directory is called the home directory.

Your home directory will be where you wind up when you initially log in to your Unix system. You will be in your home directory by default.

Anything in or below your home directory belongs to you, and unless you allow otherwise, the contents of this directory will be secure and not available to others for use or browsing.

What Is That Tilde? In Unix, you can refer to your home directory simply as ~. This is important to know because it can reduce the amount of typing you have to do. More specifically, to switch to your home directory, you can enter the tilde. When you want to switch to someone else's home directory, you can also use the tilde with the person's username, seen as ~username.


Now that you can use the pwd command to find your present location, let's investigate how to view the contents of a directory, whether it be more directories or files.

Unix File Listing

Up to now, all the commands you have learned have been important. Not one of them is uncommonly used or seen. This next command we discuss, however, will be a command that you use just about all the time as a typical Unix user, so we will spend some quality time reviewing its details as closely as possible. The command you do this with is the ls command, which lists files. Issued without any arguments (also known as switches), ls lists the files and subdirectories in the current directory. You can, of course, list files in a different directory by specifying that directory in the path statement or simply by changing into that directory and then issuing the ls command. An example of using pwd and ls is seen here:

 > pwd /priv/home/rob/temp/newwebsite/ > ls cgi_bin                      index.html images                       vrml 

This example is based on the assumption that I switched into the new directory I made (newwebsite) before issuing the pwd command. Then, by issuing the ls command, I got the file listing for that directory, which includes a new HTML page I created called index.html. Now, you try it. Attempt to determine where you currently are by checking your present working directory, and then list the files and other directories from your current location. As we progress we will learn how to change directories, but for now, master these two commands before moving on. Also, remember that this lesson is directed toward the Unix learner. If you know where you are in the Unix operating system, you don't need to issue the pwd command before listing files.

As mentioned earlier, if you are interested in listing files that are in another directory and you don't yet know how to change into that directory, then you can easily solve this problem using the ls <directory name> command. You can use the root directory we just learned about as your example. Because your current directory should still be your home directory, you can list the files and directories in root by doing the following:

 > ls / CDROM                      lib bin                        priv core                       tmp dev                        temp etc                        usr include                    var 

In this example, I have verified that all the data listed in the root directory are directories. This information may or may not be common knowledge, so if you are unsure, verify it with a command!

Use the ls command with the "long" option to ask Unix for more information in the listing. By issuing the command ls -l <directory name>, you can view our example of the root directory in a whole different light:

 > ls -l / total 12 dr-xr-xr-x    2 root         512 Jan  8   1996  CDROM lrwxrwxr-x    1 root           6 Dec  7   1995  bin -> /usr/bin -r--r--r--    1 root     7401946 Dec 24  20:15  core drwxr-xr-x    3 root       11264 Nov  7  13:02  dev drwxr-xr-x   11 root        3072 Nov  7  13:01  etc drwxr-xr-x   46 root        2560 Jan 11   1995  include drwxr-xr-x   23 root        4096 Oct 13  16:29  lib drwxr-xr-x    4 root         512 Jul  4   1997  priv drwxrwxrwx    3 root         512 Nov 24  00:30  tmp drwxrwxrwx    3 root         512 Nov 24  00:30  temp drwxr-xr-x   29 root        1024 Oct 27  17:53  usr lrwxrwxr-x    1 root           8 May 21   1995  var -> /usr/var 

Who Owns This File? One of the cool things about the Unix file system design is that when you use the ls command, you can also see who the file owner is, which is an important piece of information.


As we discussed earlier in this book, it may be a little confusing to learn Unix if you have a distribution that is different than the norm. If this is the case, you may enter the ls command and get output that looks different from what you see here. That is okay. Different versions of Unix have ls commands that produce slightly different output. This does not mean that you will not be able to decipher it. Simply use the man command with ls to see what syntax is used and what you may be able to glean from issuing the command on your system. You can attempt to use the help command at the shell prompt as well. For the most part, you will likely see the exact same information each time, no matter what version of Unix you use.

Now that we have gone over how to see where you are and how to see what is in a directory, we should spend a moment learning how to read the output from the ls command a little better. Knowing how to do this will become increasingly important as you progress in your Unix studies beyond the beginner level. Consider the following example:

 > ls -l / total 12 dr-xr-xr-x    2 root         512 Jan  8   1996  CDROM lrwxrwxr-x    1 root           6 Dec  7   1995  bin -> /usr/bin -r--r--r--    1 root     7401946 Dec 24  20:15  core drwxr-xr-x    3 root       11264 Nov  7  13:02  dev drwxr-xr-x   11 root        3072 Nov  7  13:01  etc drwxr-xr-x   46 root        2560 Jan 11   1995  include drwxr-xr-x   23 root        4096 Oct 13  16:29  lib drwxr-xr-x    4 root         512 Jul  4   1997  priv drwxrwxrwx    3 root         512 Nov 24  00:30  tmp drwxrwxrwx    3 root         512 Nov 24  00:30  temp drwxr-xr-x   29 root        1024 Oct 27  17:53  usr lrwxrwxr-x    1 root           8 May 21   1995  var -> /usr/var 

Building Bridges In this lesson and throughout the rest of the book, we will work at continuing to tie in the commands you learned in past lessons; this way, you can stay refreshed and see how to use commands together. Tying commands together is necessary in order for you to ultimately master Unix.

When studying Unix, think of your approach in terms of building bridges, because when you use commands, each one can build on another or be used with another to create more functionality. Unix commands were meant to be used in this way. In fact, the power of Unix isn't truly unleashed until you script a multitude of commands together to create a process or function.


In this example, which uses the ls command with the l switch, you see what is located within the specified directory. Within that listing, you see the following details:

  • The first line of output specifies the amount of content to be listed. In this example, the first line indicates that a total of 12 lines will follow.

  • Each line after that is then listed with a subset of important information. Figure 4.2 breaks down each individual section of one line for you. The listing (here broken down into five areas) is actually incredibly easy to understand once you know what you are reading.

    Figure 4.2. A sample Unix file system listing.


  • Moving from left to right, section 1 of Figure 4.2 details the attributes of the data. As you'll recall, read, write, and execute are all attributes used to set access control on the data stored on your Unix system.

  • A more granular breakdown shows r (read) and x (execute). There is also a letter d starting the line, which simply means that this line is specifying a directory, not a file. Lines that start with a - are for normal files, and lines that start with l indicate the directories known as bin and var. The l stands for link. Links will be covered later in this lesson.

  • In addition, the attributes themselves indicate specific things that will be covered in greater depth in Lesson 19, "Configuring Permissions in Unix," where we take a closer look at file ownership and groups.

  • In section 2 of Figure 4.2, you see the word "root." This simply indicates the user account that owns the specific data in this line listing. As you can see, root owns everything in the root directory. (Again, remember the difference between the two roots.)

  • Section 3 of Figure 4.2 shows the amount (in bytes) of disk space that the data is occupying at this time.

  • Section 4 of Figure 4.2 specifies the date of the last modification to this file or directory. January 1996 was the last time that this directory was altered in any way. Most times, unless you are using a data file such as the web page mentioned in an earlier example, you won't be making many changes to default system files, directories, or programs.

  • In section 5 of Figure 4.2, we see the final piece of information: the filename itself.

In the next section, we will tie up our discussion of the ls command by learning about hidden data that you may be missing.

Hidden Data

You may be familiar with working with hidden data in Microsoft Windows environments. Unix is similar in that it too will also hide data from you. You need to be aware of this and also know how to view hidden data if requested or needed. What should alert you to the fact that things are hidden in Unix? Well, the file system will tell you about this data, as seen in the following output from the ls command:

 > ls / total 10 

If you do the ls command and see you have a total of 12 lines but only 10 are viewable at the shell prompt, you probably need to add an argument or switch to the ls command, as seen in the preceding example.

We already learned about the long listing; now let us investigate those files whose names begin with a . (seen as a simple dot). These files are not shown by default, and the user must request to see them at the shell prompt. In this case, they can be viewed by using the all option for ls. The command would thus be seen as follows:

 > ls -all / total 12 

You can see the difference in the two examples; one shows all twelve files, and the other shows ten files and hides the other two.

Now that you are comfortable using the ls command, you should know that Unix has many options for expanding on the ls command in addition to the ones discussed here. For example, options for sorting, for tabulating data, and for adding flags to a "short" listing to show a file's attributes can also all be used with the ls command. Beyond the -l and -a options discussed previously, some other options that you are likely to come across include the following:

  • The -F option: This option is used to indicate file attributes.

  • The -R option: This option is used to recursively list all files below a specified directory.

Don't Forget Case Sensitivity When using arguments such as the ones just listed, you will need to take note of whether they are in uppercase or lowercase. Remember that performing the wrong command in Unix can be damaging because there are few checks implemented to keep you safe from yourself. Make sure you use the help system if you are unsure about what switches you need to define to achieve specific actions.


That wasn't so bad, was it? Now, you can log in to Unix, get help if needed, perform basic navigation, see where you are when logging into Unix for the first time, and see what contents are contained within items in your path. If this is your first time doing these things and you were successful, you should be proud of how far you have gotten in such a short time period. After only a few lessons, you are now able to work within the Unix environment to some degree. In the next section, we will make even greater progress by learning how to begin "moving" around in Unix to find what you need or to do certain tasks.

Changing Directories: cd

Changing directories in Unix is just as easy as listing them. While the ls command is used to list the contents of a directory, the cd command is used to change directories. Moving from one directory to another may be necessary if you need to edit a file, delete a file, or perform any variety of tasks. The cd command will also help you exercise your use of other commands and allow you to better navigate the Unix file system.

Using the Unix cd command will let you change directories in a flash, but how do you know where you are and where you want to go? That's where the other commands you have already learned come into play. The pwd command will show you the directory in which you are presently located. The ls command used with specific arguments (if needed) will then show you the contents of that directory, perhaps files and other directories. If, in that list of contents, you see another directory you would like to access, you can simply use the cd command to do so. The full syntax for the cd command is cd <directory name>.

Because we've already learned about the home directory and the root directory, let's practice all these commands and move from one to the other and back again.

 > pwd /priv/home/rob > cd / > pwd / 

In this example, I knew where the root directory was located by default, so I just specified it. To move back, you can do the following.

 > pwd / > cd /priv/home/rob > pwd /priv/home/rob 

Notice that I didn't have to step back from the /rob directory to the /home directory and then from /priv switch and change directories to the root directory (/). I could simply specify /priv/home/rob when using the cd command.

Unix Commands Are Similar to Windows Commands…Sometimes The cd command used with Unix is identical to the one used with Microsoft Windows-based desktops when using the command or MS-DOS prompt. You can see this command and other Windows commands that are similar to Unix commands by going to start => run (or all programs => run) and typing cmd or command. Then, type the word help. The cd command (and many others) can be seen here. The cd command is common in use and structure in both systems.


Changing Directories: pushd and popd

After you master the cd command, you need to learn to move around Unix using the pushd and popd commands. pushd <directoryname> will need to be entered if you want to specify the directory. With the popd command, however, you will not need to specify a directory name.

When changing between directories, these two commands allow you to make good use of the directory stack. The directory stack is a type of data storage that works as follows: Data is added to the top, thus creating the stack, and the most recently added "plate" of data is the first to be removed if requested, because it comes off the top of the stack. To describe this in terms of the pushd and popd commands, data is pushed (pushd) onto the stack and popped (popd) off the stack when requested by each command, thus making use of these commands quicker and more efficient than use of the cd command. The following examples will show you how to perform both pushd and popd:

 > pwd / > cd /priv/home/rob > pwd /priv/home/rob 

Or

 > pwd /priv/home/rob > pushd /var/adm /var/adm /priv/home/rob > pwd /var/adm 

Then

 > popd /priv/home/rob > pwd /priv/home/rob 

Try both commands and see which is easier and makes more sense for you. Either one will work just fine with your distribution of Unix.

You now know almost everything about navigating Unix and its supposedly cryptic file system. Let's expand this knowledge by taking a deeper look at paths, which were briefly discussed earlier in the lesson.

Relative and Absolute Paths

When changing directories, you can specify exactly where you want to go by specifying the correct path. As mentioned earlier in the lesson, a path is the shortest set of directories through which you must travel from the root directory to get to the current file or directory. Unix, however, has two different types of path statements that you absolutely need to be familiar with in order to completely understand how to navigate the system.

The two types of path statements in Unix are relative path statements and absolute path statements. Paths starting with the root directory and ending in a file or directory name are called absolute paths. In other words, absolute paths have absolutely everything contained within them, including the root statement /. One example of an absolute path statement is as follows:

 > cd /usr/local/bin 

On the other hand, a relative path is a shortened version of an absolute path. It contains only the directory in which you are currently working, and it does not include the root statement /. Consider the following example:

 > cd bin 

Remember, relative paths are relative to the current directory. Furthermore, absolute paths start with / and relative paths don't.

By this point, you know how to find out where you are, what files are in what directories, and how to move to different directories. In this lesson, we have covered not only several important commands, but some important theory as well. Now that we have dug deep into using the shell prompt to navigate the file system, let's take a moment to learn how to do it with a GUI, namely, KDE.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net