Command Cheats - Cheat sheet for commonly used Unix commands


Version: 0.92

Last Modified: Sun Dec 15 00:43:32 EST 2013


The information contained herein is copyright (c) 1998-2013 by Frank J. Edwards of Edwards & Edwards Consulting.


The general format of each entry is:

command - the command name itself and one-line summary

A brief description of what the command does. Usually only a sentence or two.

    A description of the options available for the command.
    Sample command lines that demonstrate common usage.

There may be additional text if the command needs more explanation.

Hopefully, this strategy will allow you to find what you're looking for quickly. All of the extraneous options that I don't use myself are removed, so you'll see just the more common ones.


All of the commands which take filename parameters may use wildcards in those positions, but remember how to use the shell's single and double quotes to prevent wildcard expansion when parameters contain spaces, tabs, etc.

ps - process status

Process status. Generates a list of all running processes.

    -f  full listing of command lines (use -l on some systems)
    -e  every process on the system
    -u  particular user's processes, i.e. "ps -uroot,fred"
    $ ps -f
    $ ps -ef

mail, mailx, Mail - read or send email

Electronic mail.

To send email, specify user names on the command line, i.e. mail root fred

To read email, don't give any user names, i.e. mail

ls - list the contents of a directory

List directories. List the contents of the given directories.

    -l    long listing
    -a    all hidden files (those that start with a dot) are listed also
    -F    append an attribute character to each filename [see ls(1)]
    $ ls [-options] [directory(s)]

more - display file contents one page at a time

Display the content of text files one screenful at a time. Use h at the prompt to view the help screen.

If multiple filenames are provided they are displayed one at a time.

    $ more [filenames...]

cat - display file contents

Read one or more text file(s) and send the output to stdout (normally the screen).

    -v    To see control characters printed as visible sequences.
    -e    To mark the end of each line with a dollar sign
    -t    To display TAB characters as Ctrl-I (^I)
    $ cat [filenames...]

tee - write output to both stdout and given filenames

Read stdin and send the output to stdout; also save a copy of the data in files specified.

    -a    Append to the output file(s) instead of overwriting them.
    $ ls -l | tee file1 file2 | sort -r -n

grep - search for string within one or more files

Globally search for Regular Expression and Print. This is the ubiquitous "string search" command, although it searches for more than just literal text. See below for a fuller (but still not complete) list of regular expression special characters.

If filenames are given then those files are searched. If no filenames are given, only stdin is searched (so grep can have data to be searched piped into it).

    -i    Perform a case-insensitive search
    -c    Count the number of lines that match, but don't display them
    -e    Treat the following word as the search string, even if it
          starts with a dash
    $ grep [options] 'regexpr' [filenames...]

cd - change directory

Change current directory. No parameters returns the shell to your $HOME directory.

    $ cd [directory]

pwd - print working directory

Print the full pathname of the current directory.

    $ pwd

vi - full screen visual editor

Full-screen visual editor.

    $ vi [-options] [files...]

rm - remove files or directories

Remove specified files and/or directories.

    -r    recursive - removes given file or directory and all subdirectories
    -i    interactive - prompt for each file before removing
    -v    verbose - print a message for each file removed
    $ rm [-options] files...

chmod - change permissions (access modes)

Change the access modes (ie. permissions) on files and directories.

    Options allow specification of (see example syntax, below):
        ugo   user, group, or other permission fields
        +-=   whether to add, remove, or set specific accesses
        rwx   read, write, and execute permission bits
    $ chmod [ugo][+-=][rwx] files...

chgrp - change group ownership

Change the group id on files and directories; only the file owner and root may do this.

    $ chgrp group files...

chown - chwon owner

Change the owner of files and directories; only root may do this.

    $ chown userid files...

cp - copy files or directories

Copy files from one directory or filename to another.

    -r    recursive - copies given file or directory and all subdirectories
    -i    interactive - prompt for each file before removing
    -v    verbose - print a message for each file removed

(Notice the options are the same as rm.)

    $ cp old_file new_file
    $ cp file(s) directory
    $ cp -r directory directory

mv - move files or directories

Move files from one directory/filename to another.

    $ mv old_file new_file
    $ mv file(s) directory

sort - sort one or more input files

Sorts given files; many options for controlling sort key(s).

    Reads stdin if no files given; writes to stdout if no output
    file given [see sort(1)].
    $ ls -il | sort -n

find - search directories (and subdirectories) looking for matching files

Locates files by recursively searching a directory list.

    -type [bcdflp]   Only particular file type (d=dir, f=file, l=symlink)
    -name "pattern"  Only files which match shell wildcard "pattern"
    -print           Print list of names to stdout
    -mtime n         Only files with certain modification time:
                         n < 0: less than 'n' days
                         n > 0: more than 'n' days
                         n = 0: today
    -size n          Only files with size 'n' [see above for ranges]
    -user id         Only files owned by user id (alpha or numeric);
                     ids are separated by commas
    -exec "cmd" ';'  Execute "cmd" for each filename found
    -ok "cmd" ';'    Same as -exec, but prompts before executing "cmd".
                     The current filename is substituted for {} within
                     "cmd", ie. -exec "ls -l {}" ';'
    <exp> -o <exp>   Logical OR of two expressions (default is AND)
    `(' <exp> `)'    Logical grouping of expressions
    ! <exp>          Logical negation of expression

Regular Expressions - metacharacters used when working with text

    In the following descriptions, an RE is any single character
    unless one of the special metacharacters is used.  The
    metacharacters may be made literal by preceding them with
    a backslash ("\").
  Character   Meaning
     [ ]      Match any single character from given set.  Allows '-' for
              range, and '^' immediately after open bracket to negate the
              set (same way "[]" work in the shell).
      .       Match any single character [same as "?" wildcard in the shell]
      *       Match zero or more of preceding RE
              (".*" RE same as "*" in shell wildcard)
      +       Match one or more of preceding RE (only in egrep/awk/sed)
      ?       Match zero or one of preceding RE (only in egrep/awk/sed)
      $       Match EOL when '$' appears at the end of the pattern
      ^       Match BOL when '^' appears at the beginning of the pattern
      |       Logical OR between two REs
  The following only work in egrep/sed/vi, not grep.
     \<       Match beginning of word.
     \>       Match end of word.
    \( \)     Creates a subpattern RE.
     \n       Where 'n' is 0 through 9, specifies subpattern 'n' in
              the replacement string.