Directories and Commands

You’ve woken up in a strange place. Maybe you’ve decided you want to be a power user with your mac. Maybe you’ve finally decided to check out this strangle ‘Linux’ thing you’ve heard so much about. Maybe its your first day in software engineering class and the teacher is telling you you are going to have to work on the school’s server to turn in all of your work. Either way, you find yourself eye-to-eye with a terminal (commonly called the command line) and you don’t know what to do.
Where Are We?
Let’s start by doing what anyone would do when dropped off in a new strange land. Let’s figure out where we are and take stock of our surroundings. Type the following command:
pwd
this will tell you the directory you are currently in. It may look something like this:
/home/glenn/
Let’s make sure we are still who we think we are:
whoami
This will tell you your username:
glenn
Now, let’s see what else is in the directory with us:
ls
this gives us a list of all the files and directories in the directory we are currently in. but maybe we want a bit more information about this. let’s type the following command:
ls -la
and we get something like this:
drwxr-xr-x 70 grp glenn 4096 2012-02-15 09:35 .
drwxr-xr-x 3 root root 4096 2011-03-15 16:55 ..
drwxr-xr-x 2 grp glenn 4096 2012-02-07 09:21 Pictures
drwxr-xr-x 2 grp glenn 4096 2011-08-23 15:29 Public
drwxr-xr-x 5 grp glenn 4096 2011-09-28 13:30 Rails
drwxr-xr-x 6 grp glenn 4096 2011-11-08 16:35 Repositories
drwxr-xr-x 12 grp glenn 4096 2012-01-15 17:09 Server
drwxr-xr-x 2 grp glenn 4096 2011-03-15 17:03 Templates
drwxrwxr-x 2 grp glenn 4096 2011-08-11 08:46 Ubuntu One
drwxr-xr-x 2 grp glenn 4096 2011-03-15 17:03 Videos
Whoa! there is a lot to take in here. Why did adding -la to the ls command change the output so much, And what is all of this stuff in front of the file names now? Why are there two files named . and ..?
Commands And Options
ls and pwd are both commands. pwd is short for Print Working Directory and well, prints your current working directory. ls is shorthand for list and lists the contents of the current working directory. These are the first of many commands we’ll be going over. You can pass options to commands to change how they work. These are almost always formatted in the form of a single hyphen and a single letter, or two hypens and a complete word. You can combine single letter options with one hyphen, which is what we did when we typed ls -la. You could also write ls -l and ls -a and both would be valid commands. the -l argument tells the ls command to list the files on a single line with additional details. The -a command tells ls to show all files, including hidden ones. All directories contain at least two hidden directories, one called . and one called .., but we’ll get to those in a moment.
Many commands have similar options, where the same arguments have similar effects with similar commands. There is one option that works with almost every command on the terminal, and its the most important one you’ll ever learn. Are you ready? Got a pen?
--help
This will print out an explination of the commands function, and a list of options it can take and what they do. Go try running the following commands and checkout the output:
whoami --help
ls --help
You’ll see that whoami is a relatively simple command with very few arguments, where ls has a lot more options availble. I could just replace a lot of text in this and upcoming articles with “just run --help”, but I’m not going to. Feel free to explore some of the other commands and options that are available to them, and see what you can do with them.
Traversing Directories
Now I bet you are still wondering about that . and .. stuff. These are both directories, or rather things that point to directories, that can be found in every single directory inside of linux based system. . Always points to the current directory. .. Always points to the parent directory. If your current working directory is /home/glenn/, then .. would point to /home/. If you make it all the way to the bottom directory (/), then .. will still be there, but will also point to /.
Now let’s learn how to change our working directory so that we can start traversing the folders around us. This brings us to our next command and last one of this article:
cd
cd is short for ‘Change Directory’. To use this, we’ll look at another thing we can do with commands: pass arguments to them.
Passing Arguments
Commands take arguments when they need additional information on what they do. You could think of it as a ‘target’ for the command. Commands can take multiple arguments, but in this case we’ll only need one: Telling cd what directory we would like to change our directory too. So, if we want to go into the Pictures directory earlier. try running these two commands:
cd Pictures
pwd
Now, based on the folder structure we looked at earlier, we should get an output similar to this:
/home/glenn/Pictures
If we wanted to go back to where we were, we can reference that .. directory:
cd ..
pwd
and we are back at
/home/glenn
cd ProTips
You can chain together directories to move multiple steps at once, like so:
cd Pictures/Vacations/Vegas
cd ../../../
There are also two handy shortcuts available to the cd command:
cd -
cd ~
the first is sort of an ‘undo’ option. if will take you back to your last working directory. the tilde (~) is short hand for your home directory which every user has.