Mastering the UNIX Command Line: A Beginner’s Guide

Tweet

When a web developer or designer uploads web site files to a web server, there’s a strong likelihood it’s running on an UNIX-style operating system. There’s an equally strong likelihood that, although developers and designers know all about how to upload their files through their favorite file transfer clients, the details of how it actually works are hidden from them.

While it’s possible to survive without this knowledge, many of you may have wondered what goes on up in that cloud where the UNIX gnomes carry out their work on your web creation. This desire to know how it all fits together probably occurred to you when things became unstuck, when you inadvertently placed your files in the wrong folder, resulting in dysfunctional CSS, or even a web site that didn’t function at all. And indeed, a bit of UNIX knowledge may have come in very handy at these times.

So, if you’d like to know what’s going on behind the scenes, and take a first step towards learning the nuts and bolts of a UNIX-style operating system, read on!

UNIX-style Operating Systems

So what does it mean for an operating system to be UNIX in style? How is it different from an operating system like Windows? Well, all UNIX-style operating systems are comprised of a group of similar tools that work together to produce a result, as opposed to a monolithic and stand-alone system such as Windows. UNIX’s groups of tools are also flexible enough to provide a number of ways to accomplish things. In fact, the common workaday toolbox is a useful metaphor here.

When you use a UNIX style operating system you have a useful box of tools at your disposal, and it’s well worth getting to know the basics of these tools. Furthermore, thanks to the similarities between all UNIX-style operating systems, once you know one tool you have a decent grounding for knowledge of them all.

While all UNIX-style systems have a standard set of tools, they can also contain specialized tools to meet certain objectives. The toolbox may come with every tool imaginable, or a limited set of tools to suit a very specific purpose.

The typical standard toolkit includes such things as the function to change directories, list files, move, copy, and delete files, control file permissions, see what programs are running, and so on. In their basic form, all these tools are very similar between different UNIX style operating systems. Think of the different types of toolboxes out there, from those treasured by tradespeople to those gathering dust in the sheds of home improvers. They’ll share common implements, such as screwdrivers, hammers, and wrenches. However, one may have an automated screwdriver, another may have different sizes of hammers, and still another may have strong grip wrenches.

The list of available UNIX-style systems is extensive, but here are links to the most common ones:

You may recognize them by their logos, pictured.

Various UNIX-style operating system logos

Despite their specializations, these operating systems will perform the same functions.

Package Management

Packages

While most UNIX style systems include a large selection of programs, you may find that you need additional functionality. The creators of UNIX operating systems can’t predict everything you’ll want installed, so they provide tools – known as package managers – to help you install new features.

Firstly, you may be asking, “What is a package?” A package is simply another way of describing a program or piece of software. It’s referred to as a package because it’s often “packaged” using a compression format like gzip or bzip2 – similar to a zip file on a PC. Package managers help you manage the location, installation, upgrade, and removal of software packages.

Repositories

Package Managers keep a list of software repositories. These repositories hold all the packages that the system’s package manager can install. Systems will have an “official repository,” which contains all the packages that the developers of the operating system support.

Unofficial repositories carry packages that the package manager can install, but there won’t be any support for them. These unofficial repositories often contain experimental packages, packages that aren’t guaranteed to work on the operating system, or simply don’t adhere to the goals of the operating system. For example, Debian doesn’t allow certain packages due to licensing issues, so any such packages would need to be installed from an unofficial repository.

Where’s My Package Manager?

Those of you who use Windows are used to the standard install.exe or setup.exe files, where you download a program that installs the application package upon execution. In UNIX-style operating systems, however, your package manager depends on your operating system. Common package managers include:

  • APT, which is found on systems that are based on Debian Linux. The many easy-to-use graphical interfaces include the popular Aptitude and Synaptic.
  • RPM, which is the Red Hat package manager. You can also install the automatic updater and package installer Yum, which enhances the ease of operation of RPM.
  • Ports, which is commonly found in BSD-type systems.
  • Portage, which is used by Gentoo Linux.

Here’s the Synaptic Package Manager interface in Ubuntu Linux.

The Synaptic package manager

There are many other package managers besides the ones mentioned here, but these are the main ones worth knowing.

The UNIX File System

An FTP program or the command pwd (print working directory) will tell you what directory you’re currently in. pwd will output something like /home/website/public_html. This is referred to as a path. A path is like an address, in that it describes a location in a file system. In a path, directories are separated by a / character. To put it in plain English, the above path translates to the public_html directory, which is within, or under, the web site directory, which is under the home directory, which is under the root directory.

Some of you might be thinking, “Wait, I don’t see anything called ‘root’ in there!” That’s correct – the root directory is a special directory indicated only by the initial / character, which means it has no directories above it. On every system, there’s only one root directory from which every other directory stems. You might think of the root directory as the front door to the file system. Upon entering it, you have access to everything inside.

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments