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 CommandFor 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 DirectoryIn 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.
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 ListingUp 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
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
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:
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 DataYou 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:
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: cdChanging 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.
Changing Directories: pushd and popdAfter 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 PathsWhen 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. |