Guide to unix

Prepared by Alexandre Becker, CS107 TA

This tutorial walks you through the use of most basic unix commands for navigating the file system, viewing files, and reading the manual pages.

Browsing the file system

Open a terminal. The terminal window will show:

myth9:~>

What does this mean? This is called the "command prompt". The prompt shown here includes the hostname (in this case, myth9) and the path to the current working directory (in this case, ~ which is your home directory). The prompt usually ends with a > or $ to show it is waiting for you to enter a command. (Your specific prompt may vary depending on the host and your configuration.)

Let's try running our first command. Enter the ls command and then press the return key (or "enter" key):

myth9:~> ls
binky.c  Downloads  Mail  mystuff  WWW

What does this command do? It lists all the files and folders contained in the current directory. To include more details about each file, such as the type or the size, you can add the -l flag to the command:

myth9:~> ls -l
total 12
-rw------- 1 andre operator   47 Apr  5 10:17 binky.c
drwx------ 3 andre operator 2048 Feb 13 11:09 Downloads
drwx------ 7 andre operator 2048 Mar  6 08:47 Mail
drwx------ 6 andre operator 2048 Jan  8 09:22 mystuff
drwxr-xr-x 2 andre operator 2048 Sep 24  2013 WWW

Then, you may need to change the current directory. Use the cd command to set a different directory as current:

myth9:~> cd mystuff
myth9:~/mystuff>

As you can see, the prompt has changed to show your new location! Wherever you are in the filesystem, you can print the full path of the current directory with the pwd command.

Congratulations, you can now browse and display the files wherever you have access on your machine!

Creating folders and files

You may want to create a folder dedicated to CS107. To do that, go in the folder where you want to create a directory, and use the mkdir command:

myth9:~/> mkdir my107
myth9:~/> ls
binky.c  Downloads  Mail  my107  mystuff  WWW

You can create a new file binky.c using your favorite editor (Emacs or Vim). You can move the file to another directory with the mv source destdir command.

myth9:~/> mv binky.c my107/

If destination is a file, the mv source destination command will rename the file:

myth9:~/my107> mv binky.c winky.c
myth9:~/my107> ls
winky.c

You can also use the cp source destination command to make a copy of the file with a new name.

To delete folders and fies, use these commands:

Viewing a file

Besides opening a file your favorite text editor (e.g. Emacs or Vim), you can view its contents with cat file or more file . Whereas cat will print the entire file in one go, more allows you to scroll up and down. You can use these keystrokes to scroll:

Using the man pages

Each command has a manual page that shows you how to use it and what its options are. You can view a man page using man command.This is one of the most useful commands to know!

myth9:~> man ls

Tip: you can use the same keyboard shortcuts to scroll the man page as in the more program!

Your life made easy: tab-completion and wildcards

Tab-completion will autofill a long name for you. Just start typing the beginning of the file or folder, press Tab and voilà, the full name is auto-completed!

Let's imagine I want to run "reassemble alphabet_frags". Instead of typing the full command, I can start the command and finish with tab-complete:

myth9:~/my107/assign1/> reas[Tab]
myth9:~/my107/assign1/> reassemble                   // Autocompleted!
myth9:~/my107/assign1/> reassemble al[Tab]
myth9:~/my107/assign1/> reassemble alphabet_frags    // Autocompleted! Isn't it wonderful?

If there is more than one match, pressing Tab twice will show a list of all possible completions.

Most commands can accept a pattern argument which expands to all filenames matching the pattern. For example, the wildcard * pattern matches any non-empty sequence of characters. You could list all the object (.o) files with the command ls *.o. The command rm *z* would delete all the files containing a z in their name.

Frequently asked questions about unix

I successfully compiled my program, but when I try to invoke it, I get an error about command not found, like that shown below. Help!

myth9:~/my107/assign1/> reassemble
reassemble: command not found.

When you invoke a program without an explicit path, the shell will look for the named program in a specified collection of directories until it finds a match or runs out of places to look. You might expect that the current directory (known as .) would always be included in the directories being searched, but it's not (for somewhat paranoid security reasons). There are two ways to deal with this.

  1. Adopt the habit to always use the full path. The current directory is referred to as . and thus the full path to a program named reassemble that is in the current directory is ./reassemble
  2. Change your search path to add the current directory so you can use the short name. The unix command echo $PATH will show you the list of directories currently in your search path. If you add . to this list, the shell will also look in the current directory. To make a permanent change, you must edit the path setting in your shell configuration file. How to do this varies a bit depending on the shell and how your account is set up. Determine your shell using the command:

    myth1> echo $SHELL
    
    • If your shell is reported to be /bin/csh or /bin/tcsh, your configuration files are .cshrc and .login in your home directory. Look in these files and find a line that sets the path set path=( x y z ) and add . to the end of the list (separated from others by spaces). If path is not set in either file, add set path=( $path . ) to .cshrc
    • If your shell is reported to be /bin/bash, your configuation files are .bash_profile and .bashrc in your home directory. Look in these files and find a line that sets the path PATH=a:b:c and add . to the end of this list (separated from others by colon). If path is not set in either, add export PATH=$PATH:. to .bash_profile

    Now logout and log back in. From now on, you will be able to find programs in the current directory.

How do I set up a nickname for a command?

If you have an often-used command that is cumbersome to retype, define an alias to give it a memorable short nickname. Determine which shell you're using (see previous question) and add the desired alias definition to your shell configuration file. Let's say you wanted an alias to run valgrind with full leak-checking. For csh/tcsh shells, add to your .cshrc

alias showleaks "valgrind --leak-check=full --show-leak-kinds=all"

For bash shells, add to your .bash_profile

alias showleaks="valgrind --leak-check=full --show-leak-kinds=all"

Now logout and log back in. From now on, you can use showleaks as a nickname for the longer valgrind command.

How do I set an environment variable?

Here is an example to set the LC_ALL which controls your shell's language/character-set. For csh/tcsh shells, add to your .cshrc

setenv LC_ALL C

For bash shells, add to your .bash_profile

export LC_ALL=C