Unix Cheat Sheet

This unix cheat sheet has a pdf version for download. Remember that all unix commands and filenames are case-sensitive. ls is different from Ls and LS.

Extra typing is annoying. Here are some shortcuts and tricks:

Using the up or down arrow at the command prompt lets you select commands you’ve previously typed. The up arrow goes further back in your history of commands; the down arrow moves from older to more recent.

If you’re typing a file name or path or command, and don’t want to type the whole thing, try typing the first few letters and hitting the tab key. Tab attempts to complete your entry, so that typing myfTAB will fill in the missing ile.txt, giving you myfile.txt. If you haven’t typed enough of the file name to point to only one file, tab will fill in as much as it knows and beep, and you have to be more specific about the rest of the name.

Wildcard characters: * and ? are special characters. * can be used in pathnames to indicate any characters any number of times. For example, a*.txt would refer to the files a1.txt, a2.txt, a45.txt, and a37236236.txt. If you type *, that would refer to all files and folders in the current working directory. For that reason, rm * is a very dangerous command. ? is a limited version of *, and refers to only one character. Hence, a? would refer to a1, but not a32.

. refers to the current directory, and is often used when executing programs. E.g.

# ./mycode

runs the mycode program that lives in the current directory.

.. refers to the directory above the current one. E.g.

# cd ..

moves you up one level.

– refers to the last directory you were in. E.g.

# cd –

moves you to your previous location.

~ refers to your home directory. E.g.

# cp afile ~

copies afile into your home directory.

Basic file system commands

BEGIN: ssh to hpcc.williams.edu and log in.

pwdprint working directory, which is the folder you’re in. the name of the folder is in the unix command line prompt (clio:foldername:); pwd shows you the whole “path” to the current folder. When you log in, you are automatically moved to your home directory, a folder for your use created with your user account.

Example:

command:   # pwd

result:              /home/you-user-id

lslist files in the current working directory.

Variations:

–          ls –l shows more information about each file: permissions, owner, group, size, and modification date

–          ls –a shows all files in a directory (any file with a name that starts with a . is hidden by default)

–          ls –t show all files in a directory sorted by time

Example:

command:   # ls lt

result: drwxrwxr-x 9 abc1 abc1 4096 Jun 17 10:20 jobs

drwxr-xr-x 3 abc1  abc14096 Jun 13 11:58 R

drwxrwxr-x 3 abc1 abc1 4096 Jun  7 22:16 scripts

Directories (folders)

cdchange directory to the location you specify. After you change folders, the command prompt will change to reflect your new location.

Variations:

–          cd with no directory name on the command line sends you to your home directory.

–          Use a ../ in the file path to move up one level in the file structure.

–          cd with a file path beginning with a slash (/) starts at the root level of the entire directory structure.

–          cd with a directory path not beginning with a slash (/) continues from the current directory into subdirectories.

mkdirmake a directory. After the command, specify the name of the directory you want to create.

Example:

command:   # mkdir myfolder

result:              a directory named myfolder is created in the current directory.

rmdirremove a directory. After the command, specify which directory. The directory must be empty for this to work.

Example:

command:   # rmdir myfolder

result:              myfolder is removed, if it is empty.

Advanced file operations

mvmove a file. Deletes the original, and creates a copy in the location you specify. Can be used to rename a file if you specify a file name for the new copy.

Syntax:

mv [old file path and name] [new path, including name if you want to rename]

Example:

command:   # mv myfile.txt myfolder

result:              myfile.txt is moved into the subfolder myfolder

Example:

command:   # mv myotherfile.txt myfolder/index.html

result:              myotherfile.txt is moved into the subfolder myfolder and renamed index.html

cpcopy a file.

Syntax:

cp [file path and name] [path (and name, to rename) for copy]

Example:

command:   # cp myfile.txt myfolder

result:              myfile.txt is copied into the subfolder myfolder

Example:

command:   # cp myfile.txt my.txt

result:              myfile.txt is copied to the same folder, with the new name my.txt

rmremove a file. Specify the file name after the command.

Variations:

–          rm –i asks for confirmation before deleting a file

Example:

command:   # rm myfile.txt

result:              myfile.txt is deleted

File Permissions

UNIX, as a multi-user operating system, has to keep careful track of who is allowed to use which files, and in what ways. There are three basic operations users can perform on files and programs: read, write, and execute. UNIX recognizes three basic classes of users: the owner of the file or directory, a group of users, and all users.

Each file (directory, program) has permissions information stored with it, and has a separate set of permissions for its owner, for its owner’s user group, and for all users. These are visible using the ls –l command.

chmodchange mode, or permissions, for a file or directory.

Syntax:

chmod [user/group/other][+/-][read/write/execute] [path]

The basic idea is that you specify who gets or loses permissions, then use a plus or minus sign to indicate adding or subtracting rights, then indicate which permissions they gain or lose. To specify who gets rights, use the following abbreviations: u=user, g=group, o=other, a=all (user, group, and other). To specify what rights they get, use these abbreviations: r=read, w=write, x=execute.

Variations:

–          chmod –R makes the permissions changes to every file and folder within the directory, rather than just to the specified file or folder.

Example:

command:   # chmod ug+rw file.txt

result:              gives file owner and group read and write permissions to file.txt

Example:

command:   # chmod –R o-rwx myfolder

result:              removes others’ ability to do anything (read, write, or execute) with all files and folders in myfolder.

chownchange file owner.

Syntax:

chown [new owner] [file]

Example:

command:   chown john myfile.txt

result:              makes the user john the new owner of myfile.txt

Programs

To run a program, simply type its name. For example:

command:   # emacs

result:              the emacs text editor opens

less — lets you look at the contents of a file one screen’s worth of text at a time. Use arrow keys to move up and down line by line, space bar to move forward one screen’s worth, b to move back one screen. You must type q to exit less when you’re finished reading. You can also search by typing /.

nano — a simple text editor. ^o (^ means ‘hold down the control key while pressing the character that follows) will save. ^x will exit. ^g will show help.

emacs — a complicated text editor. ^x^s (control-x then control-s) will save. ^x^c will exit. Look for command references and tutorials on the net.

clear — clears your terminal window. Useful for getting rid of a mess.

man — shows you the UNIX manual.

Syntax:

man [name of a command you want more information about]

Variations:

man –k [topic] lets you search for a topic, rather than a command

passwd — change your password.

grep — search through the contents of the given files for a given string or pattern. E.g.

# grep ‘cow’ *

looks for the string ‘cow’ in all files in your current location.

wc — count all the characters, words, and lines in the given input.

Redirecting

Unix lets you chain together programs and files using IO redirection.

 

IO means Input-Output. Input is the information that goes into a program. Output is what a program sends to the screen. Normally program input is what the user types and the output is what the user reads. Special symbols let you change that.

 

> (output redirect) — take the output from the program on the left and dump it into the file on the right (replacing whatever used to be in that file). E.g.

# ls -l > listing.txt

 

>> (output append) — as above, but append to the end of the file rather than replacing it.

 

< (input redirect) — take program input from the file specified. This is used less often – depends on particular program.

 

| (pipe or bar) — take the output of the program on the left and use it as input to the program on the right. E.g.

# ls -l | less

You can chain together several programs/commands in a row:

# ls -l | grep -v ‘cow’ | wc | less