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”:
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.)
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.