Unix Quick Reference

Moving Around

Find out where you are: pwd
Prints the name of the directory you are in (print working directory).

List the files in the current directory: ls
ls has some useful options:

  • ls -l for long listings gives you more information about the files;
  • ls -a to list all files;
  • ls -F to display characters beside each filename that give the type of file (/ for a folder, * for a program, @ for an alias, etc.).
Or put them all together: ls -alF

Move to a different directory: cd <dirname>
Stands for change directory.
Example:

    $ cd public_html/modeling
Move up one directory: cd ..
In any filename, .. (two periods) represents the parent directory and . (one period) represents the current directory.

Go back to your home directory from anywhere: cd
With no arguments.

Working with files and directories

Copy a file: cp <old_file> <new_file>

Rename or move a file: mv <old_file> <new_file>

Delete (remove) a file: rm <filename>

Delete an entire directory: rm -r <dirname>
Note: be very careful with this -- if you delete a directory, you can't get it back.

Create (make) a directory: mkdir <dirname>

View contents of a short file: cat <filename>

View contents of a long file: more <filename>
Use the spacebar to see more (hence the name) of the file. Use Ctrl+b (on some systems) to move up one page in the file.

Send command output to a file: <command> > <output_file>
Example:

    $ ls > output
    $ cat output
    myprogram.pl
    output
    test.java
    test.pl

Getting help

See help file for any command: man <command>
Stands for manual.

Get all that stuff off your screen: clear

I'm in a strange directory and I don't know how to get home: cd
With no arguments.

Getting out of a program that won't stop: Ctrl+c
This will cause many programs to quit.

Text editors

Edit a file with the simplest text editor: pico <filename>

Edit a file with a text editor: emacs <filename>

Edit a file with the best text editor: vi <filename>

Compiling and running programs

Run a PERL program: perl <filename>.pl

Compile a Java program: javac <ClassName>.java

Run a Java program: java <ClassName>
Note: do not type the extension (.class) after the class name.

Logging in and out

Connect to another computer: ssh -l <username> <machine>

Logout: logout

Doing things faster

Typing filenames: tab key
To quickly type a filename or directory name, type the first few letters and then press the tab key. If you've typed enough letters to differentiate the name from other file and directory names in the current directory, Unix will complete the name for you.

For instance, if the current directory has two subdirectories, foo/ and bar/ and you want to change to bar/, you can type this:

    % cd b[tab key]
This trick works almost anywhere a file or directory name is needed in a command.

Recalling previous command lines: up arrow
Pressing the up arrow once will bring up the last command line you typed, pressing it twice will bring up the next-to-last command line, and so on.

Repeating the last command: !!

Repeating the last command that starts with certain letters: !<letters>
For instance, if you previously did javac MyClass.java, then executed a few more commands, you can compile MyClass.java again by just typing !javac.

Other commands

Print list of running processes: ps
Stop process # n from running: kill -9 <n>
Print date and time: date
Discover the current user: whoami
Print a history of commands you've used: history
Run the command numbered n in the history: !<n>
List contents of a file: cat <filename>
Concatenate two files into a third file: cat <file1> <file2> > <file3>
Make a file executable: chmod a+x <filename> (change mode)
Search for text in a set of files: grep "text" *.java
Find the differences between two files: diff <file1> <file2>
Use a text-based web browser: lynx <website>