Skip to content

UNIX and the command line

Kenny Yu edited this page Sep 23, 2013 · 6 revisions

So what is UNIX anyway?

UNIX was an operating system created by Dennis Ritchie and Ken Thompson, and many modern day operating systems are based off the designs and primitives first introduced by UNIX. One major idea they introduced was the idea of a programmer using the computer interactively via the command line, and the language and designs they used still exists today in Mac OS X and Linux-family operating systems (sorry Windows!). You can read the original paper introducing UNIX here!

Using the command line

In the programming environment you had setup at our last meeting, open up a terminal and enter this command:

ls

This will list all the current files and directories (folders) in your current directory (current working path). To see your current working directory, use the pwd command ( print working directory).

Arguments and Options

Many commands typically take arguments. Type ls again, pick a directory in your current working path (e.g. "Photos"), and then type ls Photos. This will list all the files and directories in the Photos directory.

Many commands typically takes options (also called flags) as well, and they are usually denoted with - or -- prefixes. For example:

ls -l

will list all the files, and print out additional data.

ls -l -a

will list all the files (including hidden ones, which have a . prefix), and print out additional data.

Man pages

How did I know these options exist? What other options exist for ls? To see documentation for a command, we can use the man command:

man ls

This will open up a screen containing more info about ls, and list any available options. You can scroll using the arrow keys, and press q to quit. If you're interested, man opens the documentation using less, a program that allows you to navigate text files. For more information and hotkeys, see here, or type man less!

In general, use man COMMAND to open up the manual pages for the command, if they exist.

Useful Commands

Here's a bunch of useful commands (see their man pages for more uses and options!):

Navigating your file system

  • ls - list the contents of the current directory
  • cd DIRECTORY - change your current working directory
  • pwd - print working directory
  • cat FILE - prints out the contents of a file
  • find - finds files in a given directory. More on this later.

Manipulating your file system

  • cp SRC DEST - copies a file from SRC to DEST
  • mv SRC DEST - movies a file from SRC to DEST
  • mkdir DIRECTORY - makes a new directory
  • rm FILE/DIRECTORY - remove a file or directory
  • chmod - change the permission of a file. More on this later
  • touch FILE - create a file if it doesn't already exist

Miscellaneous

  • echo STUFF - prints STUFF to the command line
  • wc - word count. Useful for counting lines, characters, etc.
  • cut - cut text. Useful for slicing strings and text.
  • curl LINK - grab the HTML from the specified LINK
  • wget LINK - download the file at the given LINK. Mac users will need to run brew install wget to get this command.
  • sudo COMMAND - some command require you run as a different user, or as the privileged root user (e.g. for installing new software).
  • sed - stream editor. Useful for manipulating streams of text. More on this later.
  • grep - use regular expressions to pattern match on text. More on this later.

File Permissions

You may have noticed output like this when you ran the ls -l command earlier:

-rw-r--r--  1 kennyyu  staff   154 Sep 23 01:14 README.md
drwxr-xr-x  2 kennyyu  staff    68 Sep 23 02:08 foo
-rwxr-xr-x  1 kennyyu  staff   394 Sep 23 01:01 scraper.sh
-rwxr-xr-x  1 kennyyu  staff  1353 Sep 23 01:11 scraper_pretty.sh

The d in front of foo indicates that foo is a directory. The r, w, x indicate read, write, and execute permissions on the current file. The - indicates that the permission is not granted. The three sets of rwx indicate permissions in this order: User (me), Group (a user can belong to multiple user groups on a computer), and other (all other users on the computer).

Thus, I have read, write, and execute permissions on scraper.sh, whereas everyone in my user group and all other users only have read and execute permissions.

To change permissions on a file, we can use the chmod command. For example, let's create a file:

touch bar
ls -l

You should see output like this:

-rw-r--r--   1 kennyyu  staff     0 Sep 23 02:12 bar

Now let's add execute permissions:

chmod +x bar
ls -l

And you should see output like this:

-rwxr-xr-x  1 kennyyu  staff     0 Sep 23 02:12 bar

If you wanted to add read or write permissions, you can also do chmod +r bar or chmod +w bar. To revoke permissions:

chmod -x bar

For more usages of chmod, see man chmod.

Finish the rest of the bootcamp

Go back to the main page here.