Command Line Basics Part 2: Unix File System

In the most recent post in this series I talked about the commands you use to move around the file system. Now I'll look at some of the places you might want to go.

Linux (and other Unix-like operating systems) inherited their file system layout from a long history going back to the original Unix incarnations in the 1970s. Over time various versions changed where things were located. The Filesystem Hierarchy Standard is an attempt to standardize this. It probably doesn't cover all Linux/Unix installations perfectly, but it's a fairly good guide of where stuff is in a modern installation.

The most common place you'll visit when administering a system is the /etc directory tree. That's where the configuration files for applications installed on the system reside. So if you need to change an application setting, you'll go to that application's configuration directory under /etc. For instance, if you have a web server, the configuation files will be under /etc/apache2. Here's a listing of the contents of that directory:

2012 08 13 1045 Command Line Basics Part 2: Unix File System

The right column in this display is the name of the file or directory. The first character is "-" for files and "d" for subdirectories. apache2.conf has basic setup for the apache web server. The directories mods-available and mods-enabled are used to control what optional software modules are set up to be used with the web servers.

Another important configuration on a system is the configuration of Secure SHell (/etc/ssh):

 2012 08 13 1046 Command Line Basics Part 2: Unix File System
The file ssh_config is controls how ssh works to connect from here to other systems. sshd_config (ssh daemon config) controls how ssh accepts connections from other systems.User's home directories are usually in the /home directory. So if my username is "me" then my home directory will be /home/me. (Side note: every unix-like system has a special user called "root" that has priviledges to do everything. root's home directory is a separate location, /root.Installed commands have their executable commands in directories like /bin (for normal commands) and /sbin (comands that the SuperUser, or root, use). Other directories used for those purposes are /usr/bin and /usr/sbin/. You mostly won't do anything in those directories (doing so could interfere with installed system software). If you create scripts or executable programs for everyone on your system to use, you can put them into /usr/local/bin. System package tools leave that directory alone, you can put stuff there and it won't be disturbed.

Modern Linux kernels have a window into the functioning of the kernel built into the file system, under the /proc directory. It's filled with virtual files that don't really exist on disk, but the contents of those files are populated by function calls to the kernel. The file /proc/cpuinfo, for instance, contains the kernel's information about the processor (the "more" command just prints the contents of a file to the command line display):

2012 08 13 1047 Command Line Basics Part 2: Unix File System

(I've only listed part of the output here). /proc/meminfo is useful too; it tells you information about virtual and physical RAM.

Finally, data for installed applications lives in the /var file system. The default location of the directory where the files for the apache web server live is /var/WWW. So to edit files on a new web server, you'd go to the /var/WWW and set up your files there.

So this is a very basic overview. Most of the files you'll need to get at as an admin will be in /etc or /var. Next time, I'll talk about options for editing files.

Craig Steffen cut his command-line teeth on MS-DOS 2.11 round about 1986 or 87; his first Unix-like OS was NeXT-Step on NeXT computers in 1991. He used Solaris, Irix, and increasingly Linux in graduate school, and runs mostly (Ubuntu) Linux nowadays. He lives in appalachia but oddly works for a mid-west University. In his spare time he mucks around with his vintage VW and occasionally flies small airplanes. You can see more at his blog and on twitter.

 

Command Line Basics Part 1: moving around the file system

Previous post in this series available here.

One of the advantages of a command line over a graphical control system is that you have a very very rich set of commands to run, and very fine-grained control over how they're run. The dis-advantage is that you don't have "menu"s to choose commands from, so you need a base of knowledge before you can do anything. This post and the few after it are my attempt to give you a crash course on the vital basics of doing stuff on a (perhaps) virtual Linux machine with a remote command line login.

The first thing you'll do is "log in", or establish a connection to your virtual server. You'll use some sort of tool to do that, depending on the operating system of your local machine. If you already have a unix-like machine (including Linux), you'll probably open a command window and connect to the remote machine using the command "ssh" for "Secure SHell". If you're on a Windows machine, you'll use a program like "PuTTY". In either case, when you first get logged on you'll see a blast of general information from the machine and then a "prompt":

2012 07 03 1140 Command Line Basics Part 1: moving around the file system

The prompt is the thing at the bottom that is basically the remote machine saying "Ok, I'm ready for your next command". I've set up my prompt to tell me my username, what machine I'm logged into, what time it is, what directory I'm in ("~" in this case), then the "$" character indicates the end of the prompt and that's where the cursor sits, ready for you to type.

Directories in these machines are in a hierarchy. Unlike Windows (or DOS), the file system in a Unix-like machine isn't relative to a physical drive (like "C:"). Directories are mapped to underlying media, but in a way that's mostly invisible. All directories stem from a "root" directory that doesn't really have a name; it's just referred to as "/". The root directory of a file system will have 0 or more sub-directories, each of those will have 0 or more sub-directories, and so on down. Files can reside in any of these directories. Below briefly explain three important commands which tell you the current directory, change it to a different one, and see what's there.

Any time you have a prompt, you have some notion of your "current" directory. The command "pwd" (Print Working Directory) tells you which directory you're in. pwd is usually used without options.

The command "cd" (Change Directory) is used to change the current directory to a different one. It can be invoked several ways. "cd XXX" changes to a subdirectory of the current directory with the name XXX; this is a relative directory change; where you end up depends on where you are. "cd /XXX" changes to the directory /XXX/ no matter where you are (the leading slash makes it an "absolute" directory change).

There are a couple of useful other invocations of cd. "cd .." is a relative directory change, but instead of changing to a subdirectory of the current directory, it changes to the parent of the current directory (the name is not required since each directory is only the subdirectory of one parent). In other words, in general, running "cd XXX" then "cd .." puts you back where you started.

One other invocation of cd is "cd" with no arguments. That's a special case that is an absolute directory change that puts you back in your "home" directory. Every user on a unix-like system has a point in the file system where your own files are stored (as opposed to the files that make the system run or are part of the operating system). You store your own files there as well as files that will set up your environment (another post).

The third command, command "ls" (for "list", I guess) is used to list the names of the files and the subdirectories in the current directory. With no arguments, list just gives a complete listing of all the contents of a directory, including subdirectories and files together. (How to distinguish them will be a later post.)

Here's an illustration of me logging in and moving around in the directories on my virtual server. You'll notice that my prompt here tells you what the current directory is at each step, so using "pwd" is superfluous here. However, no matter what your prompt is, even if it gets messed up sometimes, pwd will always tell you where you are, so I've used it that way here. (Words that appear like this in the trascription aren't actually part of the session; they're notes to you, the reader.)

2012 07 03 1142 Command Line Basics Part 1: moving around the file system

This gives you a very basic idea of the mechanics of moving around the directory tree. The best way to try this is to log into your own server and see what's there. If you ever get confused about where you are, the command "cd" by itself will always return you to your home directory, and "cd /" will always return you to the root of the file system.

Next time, I'll talk about the file system layout in general and some of the useful places to go.

Craig Steffen cut his command-line teeth on MS-DOS 2.11 round about 1986 or 87; his first Unix-like OS was NeXT-Step on NeXT computers in 1991. He used Solaris, Irix, and increasingly Linux in graduate school, and runs mostly (Ubuntu) Linux nowadays. He lives in appalachia but oddly works for a mid-west University. In his spare time he mucks around with his vintage VW and occasionally flies small airplanes. You can see more at his blog and on twitter.

Command Line? What’s that, and what do I do with it?

One the reasons that I've gone with vps.net for my web hosting and blogging is because some of their virtual machine install options are full virtual machines with ssh access. That means that I can log into the "machine" remotely via a text terminal, move files, change software configurations, and even write software I want to. I'm a slightly old-school computer person; I prefer to use keyboard commands to a mouse if at all possible, and the command-line suits me very well. This is the first in what I hope is a series of blog posts on what command-line style system configuration is, some of the basics of doing it, how and why you'd want to, and some of the neat tricks that are available to you when you have a direct connection to the machine.

There are a lot of factors that make command-line different than graphical-type controls; I'll talk about some of those factors in future posts. One of the advantages of a command-line interface is it gives you direct access to configuration files in the machine's file system. Some configuration is store in databases, but a whole lot of operating systems and software read configuration from text files on disk when they start up. A command-line interface gives you the ability to read and modify the location and contents of those files directly. This is a very powerful tool when something goes wrong and the graphical tools can't fix it.

I ran into an example of this type of utility while preparing this post. I needed to add a configuration option to the Apache web server on my vps.net virtual machine. Apache has been around a long time; it's very much an old-school text-file configuration piece of software. I wanted to enable a user (me) to display a page from their own directory, rather than putting in the main web directory. To do this, I had to turn on the "user directory" option.

Here's sort of what it looks like. The lines that look like this: "craig@openclasses 09:11 ~ $" is what's called a "prompt". This is a string of text that says the computer is ready for my next command. What comes after that on the same line is what I typed. Then between that line and the next line is what the computer sent back to me in response.

Here's me logged into my server. The "cd" command is me going from my home directory to the configuration directory that Apache uses. Then I use the "ls" command to check for the "user directory" module. The list of files in that directory doesn't have anything that says "userdir". I then do a listing of a different directory, which does contain two userdir files (highlighted in red).

2012 06 01 1516 Command Line? Whats that, and what do I do with it?

The fact that the userdir files are in the "available" directory means I've installed the userdir option into Apache, but I haven't told Apache to enable it.

 

So to do that, I make a symbolic link from the "available" directory to the "enabled" directory. This is like a "shortcut" in windows. It makes a file appear to also be somewhere else. "ln" is the command to create a link. I forgot I need administrator privileges to do that, so it tells me "permission denied". "sudo" is a command that gives you administrator rights for a single command, in this case "ln" to create a link. Then I ls the directory again, and this time the userdir files are there. Success!

2012 06 01 1517 Command Line? Whats that, and what do I do with it?

So that was a real-life example of logging into a server quick to change the configuration of a piece of software via the command-line interface. There will be more to come here, stay tuned!

Craig Steffen cut his command-line teeth on MS-DOS 2.11; his first Unix-like OS was NeXT-Step on NeXT computers. He used Solaris, Irix, and increasingly Linux in graduate school, and runs mostly Linux nowadays. He lives in Appalachia but oddly works for a mid-west University. In his spare time he mucks around with his vintage VW and occasionally flies small airplanes. You can see more thoughts of his at his blog and on twitter.