Learning the Command Line Part Two: Navigating and Manipulating Files

To view the first part, take a look here.

In the last article, we learned how to set up out virtual learning environment and dipped our toes into the metaphorical command line pool. In this article, we’ll begin treading into the “deep end” by learning how to do something you do everyday – manipulating files and directories. More specifically, we’ll be learning how to use the following tools:

  • cd – change directories
  • ls – list the contents of a directory
  • mkdir – make directories
  • cp – copy files/directories
  • mv – move files/directories
  • rm – remove files/directories

Note: The command line is incredibly powerful and if used improperly may cause damage that is hard (or impossible) to recover from. As such, it is highly recommended that you read the first article and setup a virtual machine.

It’s like Finder but Without the GUI
File management is one of those boring (and perhaps tedious) tasks that you repeat on a regular basis. This process is facilitated by easy to use GUIs including Windows Explorer, Finder, Nautilus, Dolphin, Konqueror, Thunar and PCManFM to name a few (not all of these are available on OS X). Easy however doesn’t mean best and sometimes files or directories can be better managed on the command line. For example, let’s say you needed to create a folder structure that included a folder called “one” that was located in folder “two” that itself was located in folder “three”. This isn’t hard in Finder but it takes time – you have to create folder “three”, open it, create folder “two”, open it and finally create folder “one.” All of this can be accomplished with one single command on the command line (don’t worry if you don’t understand this – we’ll come back to it):

mkdir -p three/two/one

Note: File and directories are case-sensitive. In other words, “documents” and “Documents” are two different directories.

Before we begin, execute the following script to set up the files and directories needed for this tutorial:

wget https://dl.dropbox.com/u/384321/mfcli_tutorial2.sh && sh mfcli_tutorial2.sh && rm mfcli_tutorial2.sh

This will setup a few dummy files and directories that we will use to explore the tools used today.

Before continuing, I want to provide you with a little tip that will make life easier. When entering a command or file/directory name, type the first few characters and push the tab key. If you do this, you’ll notice that the remainder of the command/file/directory will magically appear. This is the magic of tab completion. For instance, type mkd and watch the rest of the mkdir command magically appear. And, as I noted, this works with file and directory names as well.

Traversing and Listing: cd and ls
Let’s begin moving around the filesystem. The two key tools here are ls and cd. The ls tool has one simple purpose – list the files in the current directory or another. That’s it. That may not seem all that impressive but think of all the times you’ve opened a Finder window and used that to view the contents of a folder. In fact, I’m willing to bet that you’ve done that a few times today. Let’s try it out in our VM. Simply enter ls and press enter. You should see the following (if you don’t, execute the command above):

Let’s say that you want to enter the Documents directory. This is where the second tool comes in. The tool you want here is cd which changes directory. This tool requires what is called an argument1. Arguments are bits of text that you add after a command, separated by spaces, either to augment the command or to give it a specific directive. In this case, cd takes one argument which is the directory that you want to change into. Let’s look at an example:

cd Documents

This will change your current working directory to the Documents directory. Remember though that Documents is different from documents – files and folders are case sensitive.

Now, let’s say you want to view the contents of a folder without actually changing into it. This would be like telling Finder that you want to view the contents of another folder without having to open it. Such a feature is easy at the command line using the trusty ls tool. If you wish to view the contents of a folder without changing into it, simply provide the name of the directory as an argument to the ls tool. For example, let’s say you wanted to view the contents of the Documents directory without actually changing into it:

ls Documents

You’re not limited to folders in the current directory however – the argument can be any valid folder. You can do the same thing with the cd tool. For instance, let’s say you have a folder in your current working directory called Documents and in that folder you have another folder called “School Work.” You can get to that folder one of two ways:

Method One:

cd Documents
cd School\ Work

Method Two

cd Documents/School\ Work

The keen observer will have noticed an extra slash. You might be asking, “why is there a backslash after School?” The answer is simple but requires an explanation. Let’s backtrack for a moment to the introduction of the argument. As I noted above, arguments are separated by spaces. In the above command, we have a directory name that has a space in it. Right away, we can see that this would cause a problem because the space in the directory name would be read as the separator between separate arguments. Let’s look at that last method again without the slash:

cd Documents/School Work

If you executed that, you’d be saying the following: “change directory to the folder named School in Documents…Work.” As you can see, that doesn’t make sense since the last word would be read independent of the first part. This is where the backslash comes in. The backslash effectively works to say “hey, a space is coming up and I need you to count it as part of the argument and not as a regular space.” In other words, putting the backslash after the word School effectively tells the cd tool (or any tool for that matter) that you want to change into the directory “Documents/School Work.” This is called “escaping the space.” The same thing can be accomplished by putting the directory in quotation marks without the escaping backslash:

cd "Documents/School Work"

The quotation marks work to accomplish the same thing and essentially tell the command that what is quoted needs to be taken literally.

You’ve now learned how to list files/folders and move about. That’s great but how do you do anything with those files and folders? This is where the next set of tools come in handy.

Manipulation Part One: rm and mkdir
Let’s start making folders and removing files!

So, you’ve got your home directory with the default folders OS X provides and not much else. That’s not very helpful though and you’ll likely want to create some folder so you can organize your files. One group of files you may want to organize are applications that you want available only to your user2. This folder is called “Applications”. You can create this with the mkdir tool:

mkdir Applications

Nice and easy no? Well, I’m going to complicate things a little by introducing what are called “options.” Options let you specify specific details for your command. They are used like arguments and can themselves take arguments. Let’s look at an example using the command used at the very beginning of the article:

mkdir -p three/two/one

Here we’ve got the -p option. Options such as these are generally in one of two forms: a dash followed by a letter or two dashes followed by a descriptive word. In many cases, an option will be available in both forms. In either format, the same cautionary note at the beginning of the article applies here: options are case-sensitive. The option “-p” may do something very different than “-P” (in the case of mkdir, there is no such option as “-P”) so be careful.

Back to the option. In this case, “-p” tells mkdir to create all the necessary directories required to create the one that is last in the list. In this case, you want to create the folder “one” inside of “two” which is itself inside of “three.” Adding the “-p” option tells mkdir to create any directories it needs to make sure that this folder structure works. So, if none of those folders existed, it would create all of them in one go.

Now, let’s say you want to remove a file. For instance, that “dummy_file” is no longer needed. The tool you want here is the rm tool which works to serve one purpose (as do many Unix tools): remove files. Here’s how you might use it:

rm dummy_file

That’s it. Note that this won’t work if you aren’t in the directory that the file is located or if you don’t specify the full path. In other words, either be in the directory (using the cp tool) that the file is in or use the path to the file like so:

rm ~/dummy_file

That’s it!

Manipulating Part Two: Copying and Moving
Copying a file is quite simple and the syntax here is the same for moving. Thus, you can apply the logic from one to the other. Let’s look an example to clarify what I mean.

Say you want to copy that “importantdoc.docx” file to the Documents folder. You can use the cp command here. This command takes two arguments: the source and the destination (in that order). In essence, you want to say “copy (cp) file one (source) to this location (destination).” To accomplish this, execute the following:

cp importantdoc.docx Documents

The move tool works in much the same manner. In fact, the syntax is the same so all you have to do is replace cp with mv, the move tool:

mv importantdoc.docx Documents

That’s all you need to know for basic file management.

Final Notes
Should you feel like wiping out the file layout in the VM and starting fresh, you can execute the following:

rm -rf ~/* && wget https://dl.dropbox.com/u/384321/mfcli_tutorial2.sh && sh mfcli_tutorial2.sh && rm mfcli_tutorial2.sh

Take some time to fool around and learn the tools. If you want to learn more about each of the tools, execute man and the name of the command as an argument to read what is called the man page (think of it as a how-to for commands). For instance, if you want to learn more about the mkdir command, simply execute the following:

man mkdir

To quit that, simply push q and to navigate, push up or down. Until next time, enjoy!

1Although not technically required with the cd tool, you will most likely provide one. If you don’t provide one however, it will change your working directory to your home directory.
2Applications stored in your user directory are accessible only to you. Convention states that the folder be called Applications so that is what we’ll use.