Some Useful Unix Commands

For those new to Linux/Unix, here are some useful commands along with some of their useful options. The brackets [ ] mean that an argument is optional. In the examples, bold type refers to commands that the user types. Unix has many more commands, and these commands have more options than those listed here. For more information use the 'man' command or the 'info' command.

man [options] command

Display a manual page (online help) for a command.
Manpages show the syntax and options for a command, and often they give examples and list similar commands. The command 'info' is also useful.

Useful options:

-f Display a one-line description of the command; synonym 'whatis'
-k List the man pages related to the command; synonym 'apropos'

Examples:

pwd

Print working directory (print the name of the directory that you are currently in)


cd [directory_name]

Change working directory

Examples:

mkdir [options] directory

rmdir [options] directory

Create and remove directories.


ls [options] [file(s)]

List files (or directories)

Useful options:

-l long listing
-h print file sizes in reasonable units, not just bytes
-a list hidden files (beginning with .)
-R list directories recursively
-d don't list contents of directories
-S sort by size (use with -l for example)
-t sort by time of last file modification
-X sort by extension
-F append a symbol to the filename depending on the file type
--color use color to distinguish between types of files
--color=never don't use color
--color=auto use color if output is going to a terminal

Examples:

cat [options] file(s)

Concatenate files, sending the output to standard output (typically to the computer screen).

Examples:

more [options] file(s)

less [options] file(s)

Display contents of files.

Useful commands while viewing a file with 'more' or 'less':

<spacebar> scroll forward one page
^b (control-b) scroll backward one page
/<text> search for string <text>
n find next occurence of string
N find previous occurence of string (search for string in opposite direction)*
g go to the beginning of the file*
G go to the end of the file*
v edit the current file with 'vi'*
:n go to the next file*
:p go to the previous file*
q quit
*works in 'less', not 'more'

Example:

mv [options] source destination

Move or rename file; 'destination' is either a file or a directory

mv [options] source1 source2 ... destination

Move file(s); 'destination' must be the name of a directory

Useful options:

-i prompt user before overwriting destination file if it exists
-f don't prompt user about overwriting
-u if the destination file exists, overwrite it only if the source file is newer

Examples:

cp [options] source destination

Copy file; 'destination' is either a file or a directory

cp [options] source1 source2 ... destination

Copy file(s); 'destination' must be the name of a directory

Useful options:

-i prompt user before overwriting destination file if it exists
-f don't prompt user about overwriting
-u if the destination file exists, overwrite it only if the source file is newer
-r or -R copy directories recursively (copy directories and their files and subdirectories, etc.)
-p preserve file attributes, such as file timestamps

Examples:

rm [options] file(s)

Remove (delete) files.
It is generally best to avoid this command late at night and on Friday afternoons.

Useful options:

-i prompt user before removing the file
-f don't prompt user
-r recursively remove subdirectories (and their files and subdirectories, etc) - Use With Caution!

Examples:

lpr [options] file(s)

Print files. (Sends files to the print server to be printed.)

Useful option:

-P<printer> print to the specified printer*
*CABI printers available: hp4050 (Image Analysis Lab), dellcolor (Raj's office), hp4m (CABI West).
If the -P option is not specified, files will be printed to the default printer for the computer that you are using. For example, files printed from a Linux computer in the Image Analysis Lab will be sent to the HP LaserJet 4050 printer by default.

Example:

lpq [options]

Shows status of print queues. (Shows files waiting to be printed.)

Useful options:

-P<printer> show print queue for the specified printer
-a lists all print queues

who [options]

List the users who are logged in

Useful option:

-H Print column headings


ps [options]

List processes

Useful options:

-e List all processes
-f Use full format.
-U <user> Select processes for user <user>
a All processes on a terminal
u User oriented format
x Processes without a controlling tty

Note: The full format (-f option) lists the parent process ID (PPID). The PPID of a process is the PID of the process that spawned it, which is sometimes useful to know.

Examples:

top [options]

Monitor processes continuously. Updates every 3 seconds by default. Displays useful information such as total memory, memory in use, % of total memory used by each process, % cpu usage of each process, and process ID (PID) of each process. Type 'h' for help or 'q' to quit. To stop ('kill') a process, type 'k' and enter the PID of the process, then enter the signal for the 'kill' command (see below) or press Enter to use the default signal (terminate).

Useful options:

-d <delay_time> Specify time between updates
-u <user> List processes for user <user>
-p<PID> List process with ID <PID>
-n <num_iterations> Specify number of iterations before quitting

Examples:

kill [options] <PID>

Send a signal (such as terminate) to a process. PID = Process ID (from ps or top command).

Useful options:

-l List possible signals to send (that's the letter 'l' and not the number '1')
-15 or -TERM Send the 'terminate' signal (the default signal) to the process*
-9 or -KILL Send the 'kill' signal to the process*
-1 or -HUP Send 'hang up' signal to the process (that's the number '1' and not the letter 'l')
*Sometimes a program is not terminated by the -TERM (-15) signal. The -KILL (-9) signal is more likely to kill it, but it might not let the program 'clean up' after itself. So if you need to stop a process, first use the default -TERM signal; if that doesn't work, you can try the -KILL signal.

Examples:

find [path...] [expression]

Search for file(s), starting with path(s) given. Searches subdirectories recursively. When a file matches the expression, its path is printed (unless -prune is the only action given in the expression).

Useful options/tests/actions:

-name <filename> Search for the file <filename>
-type <filetype> Search for file of type <filetype>. Some possible values of <filetype>: d=directory, l=symbolic link, f=regular file
-exec <command> Execute the command given
-ok <command> Like exec but prompt user before executing the command
-mtime <n> Search for file modified <n> days ago. +n = more than n days ago; -n means less than n days ago.
-mmin <n> Search for file modified <n> minutes ago. +n = more than n minutes ago; -n means less than n minutes ago.
-newer <file> Search for file newer than <file>
-prune When a match is found, do not descend into that directory. (Ignore that directory and its subdirectories.)
-not <expr> True if <expr1> is false
<expr1> -o <expr2> Or. If <expr1> is false, test <expr2>

Examples:

grep [options] <pattern> [files]

Search inside files for the given pattern. Outputs each line that contains a match. See the manpage for grep for more information about patterns.

Useful options:

-i Ignore case
-r or -R Search files in directories recursively
-v Invert match: select nonmatching lines

Examples:

du [options] [files]

Summarize disk usage for each file and directory. Handle each directory recursively.

Useful options:

-s Summarize (give total for each argument)
-h Display sizes in 'human readable format', using units of: B=bytes, K=kilobytes, M=megabytes, G=gigabytes
--exclude <pattern> Exclude files or directories which match the given pattern

Examples:

ping [options] <computer>

Send a signal across the network to another computer. Useful for testing whether that computer is running and accessible on the network, or if your Linux computer can reach the network. You can specify the computer by name or by IP address. If the computer that you are pinging is accessible via the network from your computer, you will get a response every second until you exit. Exit the program with control-c.

Examples:

bc [options] [files]

Arbitrary-precision calculator.
Very useful when you need to calculate something but are too proud or lazy to go get a calculator. In interactive mode you can calculate expressions from command-line input. To quit interactive mode, press control-d or type 'quit'. Input can be read from files. It can also be used in bash (in a normal shell or in a script) to calculate expressions. Actually bc is a whole calculator language. For details on programming in bc (for example for defining your own functions, or for using loops) see the manpage.

Useful option:

-l Define the standard math library. Needed for the functions listed below.
(The standard math library is not needed for sqrt, but you should use it to avoid low precision.)

Some useful functions:

sqrt( ) Square root
s( ) Sine (argument in radians)
c( ) Cosine (argument in radians)
a( ) Arctangent (result in radians)
l( ) Natural logarithm
e( ) Exponential

Examples:

units [options] [from-units [to-units]]

Convert between units. If the units are not specified, the program enters interactive mode; exit by typing ^d (control-d)

Examples:




This page was last updated on 1/28/2008.