Unix Commands

January 22, 2015

The purpose of this lab is to teach you some basic Unix commands for those who are using a Mac OS X machine. Mac OS X is a version of Unix. If you are using a Windows machine, read the DOS Lab instead.

You will find these basic commands useful to move around the file system on your Unix operating system. Sometimes you may have to deal with some of the internals of the Unix operating system in which case you may have to use some of these Unix commands.

1. Some Unix Commands

You can try these commands once you open a Unix terminal window on a Mac OS X machine. You open one by following this chain of steps:

       Finder
       -> FAVORITES
       -> Applications
       -> Utilities
       -> Terminal (or Terminal.app)
When you open a terminal window this way, you will be put into your home directory (aka folder). Each user in a Unix system is assigned a home directory. Every time a user logs into the system, the user is put into the home directory assigned to the user. You might want to put a copy of the Terminal application on the dock.

Using these commands, try to move around various directories and view the contents of the files in some of the directories in the file system of your computer. As you do this exercise, try to see the directories and files using the Mac's Finder tool in parallel to see the effects of the commands. Remember that the directories are structured in a hierarchical fashion (an upside down tree structure). These are some of the terms we use in this context: directory (folder), parent directory, sub-directory, and file. In any situation where a directory contains another directory as its sub-directory, the one containing the sub-directory is called the parent directory of the sub-directory.

  1. exit [ To exit the Terminal session when you are done.  After you 
            exit from the Terminal session, you can close the window
	    using the usual Mac command. ]

  2. To see where you are in the file system. To see the current
     folder, that is.

     pwd

  3. To connect to a directory/folder (cd stands for connect directory)

     cd    [ Connect to your home directory.  If you are somewhere
             else in the file system, wherever you are, and want to
             get back to your home directory in one simple command,
             try this. ]

     cd .  [ Connect to the current directory, a dot (.) means the
             current folder.  It would not do anything useful in this
             situation, but understanding the meaning of the dot (.)
             will be useful in a different context later. ]

     cd .. [ Connect to the parent directory ]

     cd subfoldername [ connect to a sub-folder named subfoldername]

     [ Using the cd command, move around some in the file system
       before you continue.  Try pwd first to see where you are before
       you try the next command and remember where you are.  Now, try
       'cd /' and try to get back to your home directory.
       'cd /' connects to the root of the entire file system. ]

  4. To list the content of a directory.  Try these variations and see
     the content of the current directory.  You will be able to see
     what each variation does.

     ls
     ls -l     (that is an el, not one)
     ls -a
     ls -la
     ls -ltr
     ls -latr
     [ Later below you will see how to create your customized names
       for these commands if you wish. ]

  5. To see the content of a file

     more filename [ the entire file, a page at a time ]
     tail filename [ the tail portion of the file ]
     head filename [ the top portion of the file ]

  6. To create a new file

     Use an editor to create a new file or download something from the
     Internet, say from the course website.  You will also learn how
     to use a text editor soon to create/edit files.
  
  7. To delete a file

     rm filename
     rm -i filename

     [ Be careful with the first of the two.  Once you delete
       one, it is gone forever!  The second one asks for a 
       confirmation. ] 

  8. To rename a file named junk.txt to temp.txt (mv for move)

     mv junk.txt temp.txt

  9. To clear the screen

     clear

 10. To see what commands you have tried so far

     history

 11. For other commands, try Googling on Unix commands.

2. Using a Text Editor

On your first reading, skip the remainder of this page and come back later to finish it.

If you already know a text editor such as Sublime Text 2 or emacs to create/edit a plain text file, you may skip this section and use it below.

If not, do one of the following. In either case below don't spend too much time on the editor on your first try. Just learn a few basic commands and move on. You can come back later to learn more about it. At a minimum, learn to create a new file and save it with some contents in it.

Now that you know how to use an editor of your choice continue with the rest of the Unix lab.

3. Create a Few Unix Configuration Files First

The .profile file (It is a dot followed by profile.). Create a file named .profile in your home directory with the following as its content. You can use emacs to create it. It may already be there, in which case you modify the content.

# This is the first line of .profile

PATH=./:$PATH
export PATH

HISTSIZE=100

source .aliases

# This is the last line of .profile

A comment line starts with a pound sign ('#').

The PATH variable included here is what I use on my Mac. $PATH means whatever the default path that the Unix operating system uses and I am prepending './' (a dot followed by a slash) to make the system to start looking for a file in the current folder when it needs to find one. You separate values in the PATH variable by a colon (':').

Here HISTSIZE specifies that the history command show only the last 100 commands tried.

Here 'source .aliases' means that it will bring in the content of .aliases every time .profile is run. What .aliases contains in it will be explained below.

The .aliases file. Create this file with the following as its content. Each alias command in this file is creating a customization of a unix command that you can start using in your Terminal window from now on. For example, instead of typing 'ls -l' without the quotes from now on, you can simply use 'll' without the quotes. You can add other aliases as you find other useful ones. The ones I included here are some of the ones I use often.

# This is the first line of .aliases

alias ll='ls -l'
alias la='ls -a'
alias lla='ls -la'
alias lltr='ls -ltr'
alias rm='rm -i'
alias cl='clear'
alias dir='ls'
alias h='history'

# This is the last line of .aliases

Now close your Terminal window and open a new one. The configuration files will be effective in the new Terminal window. In fact they will be effective every time you open a new Terminal window from now on.

If you are using emacs, read on. Otherwise skip this section. Do you remember that my emacs window is in color? That is, when I edit a Python file, keywords are highlighted in colors. You can make your emacs on your Mac behave the same with colors too. It is not just colors; other features will be added too. To do so download and save this file into your home folder, the same folder that contains .profile. When you download that file, the name will be spelled dotemacs.txt. You need to rename it so that its name is .emacs with dot replaced by a dot ('.') and with NO file extension. If your computer added an extra extension automatically such as .html or .txt, remove it by changing the name using the mv command in Unix.