LEARN UNIX FAST

HELLO UNIX

Learning to use your shell is an essential part of becoming an effective Rails developer.

Here are a handful of tips for Unix newbies to get oriented.

1) File System and CWD

Typically you use a program called Terminal to access the files and navigate through the directories on your file system. (There are also several other options for ‘shell prompts,’ among them: iTerm2, Alacritty, Hyper, Kitty, MacTerm. Whichever program you use, the screen that looks like this is called your shell prompt.

The directories and their sub-directories of your file system make up a hierarchy. As you code, you will navigate your shell prompt to different folders on your hard drive.

Where you currently are at your shell prompt— that is, the current directory— is called the current working directory (or CWD). You can navigate away from the current working directory using the next Unix command…

2) cd — change directory

this tells your shell prompt to switch the current working directory to a new directory. To understand how it works, we’ll need to take a small deviation to learn about the important abbreviations for any Unix system. Anytime that you begin to tell Unix about a filepath, the first character you type is very important to what you are telling Unix.





. means the current directory I am in (or the current working directory)

.. is the parent directory, that is, the directory above this one

~ means my home directory

/ is the root of my hard drive

(none of the above) … that is, simply a folder or file name is what we call a relative path, and means the current directory I am in (also known as the or the current working directory). This is actually just like . (single period). In some cases, it makes more sense to specify the .. Typically, when you are navigating in Unix, you can leave it off and trust the shell prompt will interpret your input as relative to the current directory (hence, “relative path”).

The first character of how you specify your pathname determines how the system will interpret what you say. If you begin it with . you mean “from the current directory.” If you begin your path with .. you are saying “from the parent directory.”

If you begin it with a ~ you typically mean from my home directory. Your home directory is typically set up for you by the operating system. On my Mac, for example, my home directory is in /Users/jason . When I say ~ that means /Users/jason (on your system, of course, it will mean your home directory.)

If you begin your filepath/ (on the shell prompt) that refers to the root of your hard drive.

When you cd, you are changing the directory of your current working directory for that shell prompt to a new one. The new one might be relative to the CWD (as in the case with a relative path like . or ..) or it might be non-relative (as in the case with ~ and /). Either way, your current working directory will be changed. That’s why we call this, change directory or cd for short.

3) lslist

List out the contents of a directory. If you don’t specify anything else, ls will list the contents of the current directory. That’s the same thing as

ls .

(Remember, because . means current working directory.)

If you tell ls to list a different directory, like a subdirectory

ls coverage/

will list out the contents of a directory called coverage/ inside of the current one. If there is no such directory, it will tell you like so:

ls: coverage/: No such file or directory

An honorable mention goes to ls -la, which is both the -l flag for long format and also the -a flag to show file that begins with a dot. Note -a happens to show invisible files, the working directory (.) and the parent directory too (..). Try it out anywhere and notice that you will see the files listed vertically one on each one. Also, you’ll see the permissions, owner, and group settings. (Sorry, that’s for another Unix day!)

4) man — give me the manual page of any Unix command

Any Unix introduction would be totally remiss if it didn’t teach you what man means right away.

That’s because man is your key to unlocking the world of Unix.

Perhaps it is slightly sexist that the inventors of Unix chose this word, nonetheless, man is short for manual page. You can type man before any Unix command which you are curious about (that is, any command you want to see the manual for) and then learn how to use the command you are curious about.

You can look up the existing manual pages for the commands we just learned — cd and ls —  like so:

man cd

man ls

That’s it.

You will see the instructions, or manual page, for the change directory and list Unix commands.

In fact, this tutorial should now be irrelevant. From this moment forward you are empowered to learn any Unix command you want, immediately, just by looking at its manual page. Just type ‘man’, then a space, and then the Unix command and hit return.

Nonetheless, I will continue with a one more tips just to give you a rounded sense of Unix. I promised you 5 tips & tricks, didn’t I?

5) ps — show the process list of the running Unix processes

Ok, now the right way to do this is to search you process list. To do that you use this:

ps aux |grep <WHAT YOU ARE SEARCHING FOR>

Be sure that <WHAT YOU ARE SEARCHING FOR> is a substring for a name of the job (unix process) that you are searching for.

Secondly, notice that this specific Unix command has some funny “flags” — aux — that appear without a dash proceeding them unlike other Unix command modifiers you may be used to. Finally, the |grep on the end tells Unix to search all the processes for a matching result. When you |grep, you are filtering. After a space between the preceding flag, type |grep, then space, then the thing to filter for.

So for example, to “grep” for (or search for) all processes that contain the string “car”, I search on my Mac and get

$ ps aux |grep car

And here’s my results:

jason              561   0.0  0.0  4821940   3044   ??  S    Sat06PM   0:00.84 /usr/libexec/SidecarRelay

jason            74549   0.0  0.0  4277500    688 s004  S+    2:18PM   0:00.01 grep car

Finally, notice how this actually returns the grep process itself, because while it is running it also has the word ‘car’ in it, so you see the process basically report itself. (That’s useless because of course that’s not the one you want, so just ignore it.)

I hope you’ve enjoyed these simple tips & tricks to getting set up with a Unix operating environment. Tomorrow I’ll look at 5 more tips & tricks for orienting yourself to using Unix for a programming context.