Skip to content

Command Line Unix Tutorial

Nick Diakopoulos edited this page Nov 1, 2015 · 14 revisions

Back in the olden days of computing there were no graphical user interfaces and people had to type commands into a terminal, or "command line" to tell the computer what program to run and what data to operate on. This is still a very useful way to interface with computers and is typically how internet servers are setup and administered. This tutorial introduces some of the basics of navigating your computer from the command line. It assumes you're using a Mac computer, built on an operating system called Unix that has a command line terminal built-in.

To get started, launch a new terminal window by clicking the "Terminal" app under the "Utilities" application folder.

It should look something like this, with the name of the computer (e.g., "Nicholass-Macbook-Pro") and the name of the account you used to log into the computer (e.g., "nad").

You can navigate between different directories on your computer from the terminal. (Directories contain files and can also hierarchically contain other directories).

To determine where you currently are in your file system type use the "pwd" command. The terminal will print out the current directory:

Nicholass-MacBook-Pro:~ nad$ pwd

To list the contents of the current directory, use the "ls" command. The terminal will print out any files or directories within the current directory.

Nicholass-MacBook-Pro:~ nad$ ls

On the command line, you can modify commands by adding "flags" which tweak what the command means. To list the contents of the current directory with all of the details like file size, access permissions, and creation date, use the "-l" flag, so "ls -l"

Nicholass-MacBook-Pro:~ nad$ ls -l

If you ever want to learn about all of the flags that are available for a command, use the "man" command (short for "manual"). The man command expects you to pass an input, also called an argument, which specifies the name of the other command you want the manual on.

Nicholass-MacBook-Pro:~ nad$ man ls

Changing Directories
Let's say there is a directory called "Documents" in my current directory. To move into the Documents directory you can use the "cd" command, short for "change directory".

Nicholass-MacBook-Pro:~ nad$ cd Documents/

Note that the command line reflects your current directory as being "Documents":

Nicholass-MacBook-Pro:Documents nad$ 

To move out of the Documents directory back up to the parent directory, use the "cd" command with an argument of "..":

Nicholass-MacBook-Pro:~ nad$ cd ..

To create a new directory within your current directory, use the "mkdir" command with an argument specifying the name of the new directory:

Nicholass-MacBook-Pro:~ nad$ mkdir new_directory_name

File Commands
To create an empty text file, use the "touch" command with the name of the file to create:

Nicholass-MacBook-Pro:~ nad$ touch file1.txt

To move a file or change its name use the "mv" command with two arguments specifying the source filename and the target filename:

Nicholass-MacBook-Pro:~ nad$ mv file1.txt file2.txt

You can move the file to a different directory by specifying the directory path:

Nicholass-MacBook-Pro:~ nad$ mv file1.txt Documents/file1.txt

To copy a file use the "cp" command with two arguments specified the source filename and the target filename:

Nicholass-MacBook-Pro:~ nad$ cp file1.txt file2.txt

To remove (delete) a file use the "rm" command with an argument to specify the filename to delete (CAUTION, there's no undo!):

Nicholass-MacBook-Pro:~ nad$ rm file1.txt

Running a Local Server
When you're developing projects on your local computer (rather than on an internet server), you can use a command line program to run a local server that can be used to serve files.

Nicholass-MacBook-Pro:~ nad$ python -m SimpleHTTPServer 8888 &

Edit file1.txt to contain the text "Hello, World!", then you'll see that displayed when you load file1.txt in your browser using the following address:

http://localhost:8888/file1.txt

Learn More
This is just the tip of the iceberg. There's lots more commands you can use on the command line. Here's a another tutorial: https://kb.iu.edu/d/afsk