Unix Commands

Linux & Unix Commands

The development world grew up in the Linux and Unix world. As a result, all the tools we useare rooted in the Linux command line. Even when using Mac or Windows versions of tools, the Unix origins cannot be denied. For instance, git installs git bash, a minimalistic Unix command line environment as part of the standard git for Windows distribution. Familiarity with these commands will lead to better fluency and deeper understanding of the tools. It also often happens that developers are just "expected" to know these commands since they are the "IT person".

/ Connecting via SSH: Linux/Mac OS X

On Linux and Mac OS X, open a terminal. In Mac OS X, you can open the terminal by pressing - Space and typing Terminal. In Linux, the terminal can be opened by default by pressing Ctrl - Alt - T. Once open, type the following command:

ssh YOUR-CNM-USERNAME@bootcamp-coders.cnm.edu

Connecting via SSH: Windows

On Windows, open PuTTY from the Start Menu and double click on bootcamp-coders.cnm.edu. This was setup in the prework screencasts. Be sure and be practiced in this process. Like the warning above, you will be expected to be able to run this command quickly and without reference.

File Permissions

File permissions are represented by three digits using the chmod command. Each number is the sum of the individual permissions. Let r = 4, w = 2, and x = 1, for read, write, and execute respectively. To apply multiple permissions, simply sum them. For instance, to apply read and execute, apply r + x = 4 + 1 = 5. Now, there are three parties permissions are assigned to:

  1. Owner: the owner of the file
  2. Group: the group that owns the file (inherited by the owner by default
  3. World: anyone else that does not apply to the previous two items

To check the permissions, use the long format of the ls command. The permissions are the first block of text output in the command. The first character is a special flag to indicate whether it's a directory, link, etc. The next nine characters are the three permissions repeated for the owner, group, and world, respectively.

$ ls -l foo.php
-rwxr-xr-- 1 dmcdonald21 users 94 Jun 28 22:18 foo.php

In this example, foo.php has all permissions for the owner (r + w + x = 4 + 2 + 1 = 7), two for the group (r + x = 4 + 2 + 1 = 5), and one for the world (r = 4). So, the final permissions are 754.

Linux & Unix Commands Reference Table

Command Comment
cd or cd ~ Change to the home directory. The ~ character refers to your home directory.
cd .. Change to the parent directory (one directory up).
cd ../.. Change to the grandparent directory (two directories up).
pwd Print Working Directory.
ls List files in current directory.
ls -l List files in current directory (long format).
mkdir images Make directory images
rm file.php Delete file.php
rm -Rf images Delete directory images and all its contents. THIS CANNOT BE UNDONE!
mv file.php new-file.php Move (rename) file.php to new-file.php
cp file.php new-file.php Copy file.php to new-file.php
chmod 644 file.php Set the permissions of file.php to 644.

Using the vim Editor

In order to create files in UNIX, you can use a program called a text editor. To start the vim editor, type vim file, where file is the name of the file you wish to edit. For instance, to edit the file test.txt, type vim text.txt and press Enter. You can now start using vim to create or edit your file.

vim is a powerful editor used heavily in the real world. vim has two modes: command mode and insert mode. Command mode is a mode where text is edited by using standard vim commands. Insert mode is a mode where text is edited by directly typing text in (the mode most familiar to Windows users). vim starts in command mode by default. To switch to insert mode, press i to insert before the cursor or a to append after the cursor. When in insert mode, you can switch to command mode by pressing Esc. vim has lots of commands you can use, but here's the first ones you should know:

File Related vim Commands
Command Comment
:w Save current file.
:wq Save current file & quit.
:q Quit, if already saved.
:q! Quit, forgetting changes you have made.
:e file Open file for editing.
ZZ Synonym for :wq.

One of the most powerful things about vim is the ability to construct your own command and then have vim do your bidding. The basic building blocks are:

vim Command Building Blocks
Command Comment
n A positive integer. Repeat this command n times. If this building block is omitted, the command is run only once.
^ Start from the beginning of the line and go until the cursor.
$ Start from the cursor and go until the end of the line.
w Start from the cursor and go until the end of the word.

Now that you have your building blocks, you can now use the commands themselves. Note that if you use either $ or ^, you can't use another building block.

Core vim Commands
n? Command ^, $, or w? Comment
Yes d Yes Cut text.
Yes y Yes Copy text.
Yes dd No Cut a line of text.
Yes yy No Copy a line of text.
Yes x No Cut character of text.
No P No Paste text from the d, dd, y, and yy commands before the cursor.
No O No Start a new line before the cursor.
No p No Paste text from the d, dd, y, and yy commands after the cursor.
No o No Start a new line after the cursor.
No u No Undo.

To delete three words, type 3dw. Paste these later by pressing p. As another example, to copy 5 lines, type 5yy. Try these on your own. Think up of as many combinations as you can and test them out in the vim editor. There's much more to the vim editor. Entire reference books, pocket guides, and even coffee mugs are available to help you learn vim commands. The preceeeding commands should be enough to get you started and the best way to get comfortable with vim is through practice.

Supplemental Video

Additional Reading

For more, see Learning the Shell.