Article
Mastering the UNIX Command Line: A Beginner's Guide
Page: 1 2
Navigating Directories
In a UNIX-style system the cd (change directory) command allows us to navigate through the directories of the file system. We can enter the file system through the front door, putting us at the root directory by using the command (ignore the #, this just represents the command prompt):
# cd /
If we wanted to go to the /home directory from this location, all we would need to type in is this:
# cd home
Now that we are in the /home directory, if we wanted to proceed to the /home/websites directory we’d only need to type in:
# cd websites
This brings us to the /home/websites directory. The above command uses a relative path—that is, a path relative to your current location. If your current location was the root directory, / , typing in cd websites would simply return an error, since there’s no website directory directly under the root directory. However, from the root directory we could type in:
# cd home/websites
That would then take us to the /home/websites directory.
The opposite to a relative path is an absolute path. If we were somewhere else in the file system, we could get to the /home/websites directory by typing:
# cd /home/websites
An absolute path is the complete path from the root directory. The system will understand how to get to an absolute path from any other location in the file system.
The above examples all proceed down into the file system. To go back up, you use a special directory name. So, if you’re currently in the /home/websites directory and wish to return to the home directory, you’d use the following command:
# cd ..
The above command allows you to go up one level from your current location. You can also go up multiple levels:
# cd ../../
In this case, from the /home/websites directory, the above command would return us to the root directory. But, what would we do if we wanted to get from the /home/websites directory to the /home/cgi-bin directory? Easy:
# cd ../cgi-bin/
The above command instructs the system to change your current location to a directory one level up; then from there, one down into the directory called cgi-bin.
Special Directories
Now, some directories in an open source operating system have special functions. Ever tried to rename the C:\Windows folder on your computer? You probably don’t want to try! Doing so would cause untold damage, and you know not to mess with it because it’s a system folder.
UNIX-style operating systems also have such directories. The good news is that UNIX-style operating systems tend to have similar layouts with regards to essential system directories. Here’s a list of the system directories most relevant to beginners:
/etc/etc/init.d/usr/sbin/var/log/home
The /etc Directory
This directory contains files that configure how the system and installed applications work. You can think of the /etc directory as the manager’s office, where all the training manuals, usage documents, and policies lie. If you’re configuring a web server, you’ll usually find the configuration file somewhere under this directory. However, there’s one particular directory that’s special: the /etc/init.d directory.
The /etc/init.d Directory
The /etc/init.d (initialize daemon, daemon can be thought of as a fancy way of saying service or server) directory is used for dealing with stopping, starting, and reloading servers. The execution of programs in here is usually trivially easy. For example, starting an Apache 2 server might entail the following:
# /etc/init.d/apache2 start
And that’s it. Assuming your Apache 2 server is configured properly, a message will indicate that the server was started successfully. Sometimes glitches can happen, or you need to restart the web server for it to acknowledge changes to the configuration. This is just as easy:
# /etc/init.d/apache2 restart
Again, assuming things are okay, your server will shut down and immediately start up. Of course, you can also stop a server completely, but for web servers this probably won’t ever be used. In fact, I think the only times I’ve ever completely stopped a service is when it was going completely haywire and I needed to stop it from destroying the system, to fix configuration issues, or if a restart wasn’t doing what it was supposed to. For those who run into such situations:
# /etc/init.d/apache2 stop
This will stop the server completely, until you tell it to start again.
But, I hear you ask, where do these services lie? Most often, you can find them in the next special directory: /usr/sbin.
The /usr/sbin Directory
The /usr/sbin (user system binaries) directory is used to store programs that are non-essential to the system operation. You’ll most often find programs such as database servers, web servers, and FTP servers residing here.
While it’s technically possible to start your servers from within the /usr/sbin directory using the proper command line options, I don’t recommend it. It’s best to stick with the /etc/init.d commands, as they are much easier.
The /var/log Directory
This directory contains log files for the various servers and system programs. Most servers will create a directory under /var/log to keep the clutter down. For example, Apache might put log files in /var/log/apache. Many services on a UNIX system keep log files. Each entry in a log file provides details of an event and a date and time the event occurred. Using the information in log files, an administrator can figure out what lies behind a problem.
So now we’ve learned about the system directories, what about you, the user? Where do you come in? The answer lies in the next special directory—the /home directory.
The /home Directory
Not surprisingly, on a lot of systems the /home directory happens to be one of the largest size-wise. This directory contains folders attributed to specific users on the system. Often, you’ll be located under the folder /home/your_username. You’ll use this folder to transfer files, test changes, and a number of other things related to what you do.
Many system administrators use the /home directory for housing different web sites. When working with shared hosting providers, you’ll probably find that your site resides in a folder like /home/mysite.com, and that you upload your files to a www directory within it—this is a fairly common practice among shared hosting providers. You access your site through your domain, mysite.com, which is then mapped to /home/mysite.com/www, where all the magic happens.
This is really the last of the special directories that beginners would find of use. Many more special directories exist, but I’ve left them out because it’s unlikely that you’d ever need them, short of becoming a system administrator.
Conclusion
This concludes the first part of our series on basic UNIX-style operating system fundamentals. For those looking to go a bit further, a good place to start is the Linux Documentation Project site. You’ll find information on the more advanced topics about Linux, the most well-known UNIX-style operating system. In the meantime, keep an eye out for the next article in this series, where I’ll discuss file system permissions.