Unix Cheat Sheet


Help on any Unix command.
man {command} Type man ls to read the manual for the ls command.
whatis {command} Give short description of command.
apropos {keyword} Search for all Unix commands that match keyword, eg apropos file.

List a directory
ls {path} It's ok to combine attributes, eg ls -laF gets a long listing of all files with types.
ls {path_1} {path_2} List both {path_1} and {path_2}.
ls -l {path} Long listing, with date, size and permisions.
ls -a {path} Show all files, including important .dot files that don't otherwise show.
ls -F {path} Show type of each file. "/" = directory, "*" = executable.
ls -R {path} Recursive listing, with all subdirs.
ls {path} | more Show listing one screen at a time.

Change to directory
cd {dirname} There must be a space between.
cd ~, or cd Go back to home directory, useful if you're lost.
cd .. Go back one directory.

Make a new directory
mkdir {dirname}

Remove a directory
rmdir {dirname} Only works if {dirname} is empty.
rm -r {dirname} Remove all files and subdirs. Careful!

Print working directory
pwd Show where you are as full path. Useful if you're lost or exploring.

Copy a file or directory
cp {file1} {file2} Copy file1 as file2,
cp -r {dir1} {dir2} Recursive, copy directory and all subdirs.
cat {newfile} >> {oldfile} Append newfile to end of oldfile.

Move (or rename) a file
mv {oldfile} {newfile} Moving a file and renaming it are the same thing.
mv {oldname} {newname}

Delete a file
rm {filespec} ? and * wildcards work like DOS should. "?" is any single character; "*" is any string of characters.
ls {filespec}
rm {filespec}
Good strategy: first list a group to make sure it's what's you think...
...then delete it all at once.

View a text file
more {filename} View file one screen at a time.
less {filename} Like more, with extra features, won't auto-quit at the end, type 'q' to quit.
cat {filename} View file, but it scrolls.
cat {filename} | more View file one screen at a time.

Edit a text file.
nedit/gedit {filename} GUI (Graphic User Interface) editors, more user friendly.
pico {filename} The same editor PINE uses, so you already know it. vi and emacs are also available.

Compare two files
diff {file1} {file2} Show the differences.
gdiff {file1} {file2} GUI diff, only at HHMI @ UMBC

Other text commands
grep '{pattern}' {file} Find regular expression in file.
sort {file1} > {file2} Sort file1 and save as file2.
sort -o {file} {file} Replace file with sorted version.

Find files on system
find {directory} {filespec} Works with wildcards. Handy for snooping. Very powerful tool, read the manul!

Make an Alias
alias {name} '{command}' Put the command in 'single quotes'. More useful in your .cshrc file.

Wildcards and Shortcuts
* Match any string of characters, eg page* gets page1, page10, and page.txt.
? Match any single character, eg page? gets page1 and page2, but not page10.
[...] Match any characters in a range, eg page[1-3] gets page1, page2, and page3.
~ Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it.
. The current directory.
.. One directory up the tree, eg ls ...

Pipes and Redirection (You pipe a command to another command, and redirect it to a file.)
{command} > {file} Redirect output to a file, eg ls > list.txt writes directory to file.
{command} >> {file} Append output to an existing file, eg cat update >> archive adds update to end of archive.
{command} < {file} Get input from a file, eg sort < file.txt
{command} < {file1} > {file2} Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt.
{command} | {command} Pipe one command to another, eg ls | more gets directory and sends it to more to show it one page at a time.

Permissions, important and tricky!
Unix permissions concern who can read a file or directory, write to it, and execute it. Permissions are granted or withheld with a magic 3-digit number. The three digits correspond to the owner (you); the group (?); and the world (everyone else).

Think of each digit as a sum:
execute permission = 1
write permission = 2
write and execute (1+2) = 3
read permission = 4
read and execute (4+1) = 5
read and write (4+2) = 6
read, write and execute (4+2+1) = 7

Add the number value of the permissions you want to grant each group to make a three digit number, one digit each for the owner, the group, and the world. Here are some useful combinations. Try to figure them out!

chmod 600 {filespec} You can read and write; the world can't. Good for files.
chmod 700 {filespec} You can read, write, and execute; the world can't. Good for scripts.
chmod 644 {filespec} You can read and write; the world can only read. Good for web pages.
chmod 640 {filespec} You can read and write; group can read, the world can't do anything.Good for group project..
chmod 755 {filespec} You can read, write, and execute; the world can read and execute. Good for programs you want to share, and your public_html directory.
chmod 750 {filespec} You can read, write, and execute; the group can read and execute, the world can't do anything. Good for programs you want to share within group.

Permissions, another way
You can also change file permissions with letters:

u = user (yourself) g = group
a = everyone
r = read w = write
x = execute

chmod g+rw {filespec} Give group read and write permission
chmod u+rw {filespec} Give yourself read and write permission
chmod u+x {filespec} Give yourself execute permission.
chmod a+rw {filespec} Give read and write permission to everyone.

System info
date Show date and time.
df -h Check system disk capacity/free space.
du -sh Check your disk usage.
w Who's online and what are they doing?


Unix Directory Format

Long listings (ls -l) have this format:


    - file
    d directory,                                            * executable
    ^   symbolic links (?)  file size (bytes)   file name   / directory
    ^           ^               ^                  ^        ^
    drwxr-xr-x 11 mkummel      2560 Mar  7 23:25 public_html/
    -rw-r--r--  1 mkummel     10297 Mar  8 23:42 index.html
                                            ^
     ^^^        user permission  (rwx)      date and time last modified
        ^^^     group permission (rwx)
           ^^^  world permission (rwx)


How to Make an Alias

An alias lets you type something simple and do something complex. It's a shorthand for a command. If you want to type "dir" instead of "ls -l" then type alias dir 'ls -l'. The single quotes tell Unix that the enclosed text is one command.

Aliases are more useful if they're permanent so you don't have to think about them. You can do this by adding the alias to your .alias file, and put this line in your .cshrc file "source ~/.alias", so they're automatically loaded when you start. Just remember that if you make an alias with the name of a Unix command, that original Unix command will become unavailable.

Here are a few aliases examples:

            # enter your aliases here in the form:
	    	 # alias     this    'means this'

            alias       h       history         
            alias       m       'more'
			 alias 		 mynv	 'cd /path/to/nmrview/datefile'

            alias       bye     exit

            alias       dir     ls
            alias 	cdup	cd ..


Dotfiles (aka Hidden Files)

Dotfile names begin with a "." These files and directories don't show up when you list a directory unless you use the -a option, so they are also called hidden files. Type ls -la in your home directory to see what you have.

Some of these dotfiles are crucial. They initialize your shell and the programs you use, like autoexec.bat in DOS and .ini files in Windows.These are all text files that can be edited,

Here are some useful dot files:

.cshrc my C-shell startup info, important!
.alias list of alias.
.signature my signature file for mail and news, ok to edit.


Other Unix Links