knowledge-base:linux-topics:quick-reference

Linux Quick Reference

This Linux quick reference has been compiled by current and former members of the EECS IT Support staff as a guide for the most basic Linux commands.

In a Linux shell, there are two types of paths. There are absolute paths (beginning in /) that define a full path from the root directory and relative paths (paths that don't begin with /) that are relative to your current directory. Example:

Example Path Description
foo.c The relative path to the file foo.c in the current working directory
pgms/foo.c The relative path to the file foo.c in the pgms directory within the current working directory
/home/jruser/pgms/foo.c The absolute path to the file foo.c based on the root of the file system
Abbreviation Meaning
~ Your home (login) directory
~username The home directory for the username
. The current working directory
.. The parent directory of the current one
../.. The parent directory of the parent directory
Character Meaning
? Match a single character
* Match zero or more characters

Examples

Expression Description Examples
fo?.c Matches fo followed by any single character, followed by .c foa.c, fob.c, foc.c, fo1.c
foo.* Matches foo. followed by zero or more characters foo.txt, foo.exe, foo., foo.png
Command Effect
command > myfile Redirects the output of command to the file myfile instead of stdout (the terminal output). If myfile exists, its contents will be replaced.
command » myfile Similar to > except that it appends to the end of the file rather than overwriting.
command < myfile Redirects the contents of myfile to the input of command rather than stdin (keyboard terminal input).
cmd1 | cmd2 Redirects (“pipes”) the output of cmd1 to the input of cmd2.
command | tee myfile Duplicates the output of command to both stdout and myfile.
script myfile Logs everything displayed on the terminal to the file myfile. The logging is terminated with exit.
Command Effect
cat > myfile Allows you to enter text with the keyboard to be stored in the file named myfile. After entering the desired text, press Crtl+D.
vim myfile Launches vim text editor on the file myfile
Command Effects
cat myfile Displays entire contents of the file myfile at once. Note that to scroll this content, your terminal pragram must be able to handle scrolling.
less myfile Displays contents of the file myfile in a full screen, scrollable view.
Command Effect
diff file1 file2 Performs a line-by-line comparison of the files file1 and file2. The differences are displayed in the shell output.
cmp file1 file2 Performs a byte-by-byte comparison of the files file1 and file2. The differences are displayed in the shell window.

Compressing files can reduce the size of files and help conserve space.

Command Effect
gzip myfile Compresses the file myfile, replacing it with the file name myfile.gz
gunzip myfile.gz Uncompresses the file myfile.gz, replacing it with the file named myfile
Command Effect
grep hello myfile Displays the lines from myfile that contain the string hello
grep hello file1 file2 Displays the lines within files file1 and file2 that contain the string hello
grep “some string with spaces” myfile Displays the lines within the file myfile that contain the entire string some string with spaces
grep hello * Displays the lines within any file in the current directory that contain the string hello (Fun fact: the * is not parsed by grep but instead by the shell. * is a wildcard in the shell that expands to a space delimited list of all files in the current directory)
grep -v hello * Displays the lines within any file in the current directory that do not contain the string hello (-v stands for invert-match)
grep -i hello * Displays the lines within any file in the current directory that contain the string hello in any letter-case. For example, lines containing hello, HELLO, and HeLlO would all be displayed
  • Process: any running program on a Linux system
  • PID: process identifier (a number that identifies the process)
  • Job: a process or group of processes that a shell is running
  • Job-ID: job identifier
  • Background job: a job that is running in the background (e.g., the process is not using terminal input and the terminal is still accepting commands or running another program in the foreground)
  • Foreground job: a job that is running in the foreground and consuming terminal input/output
Command Effect
ps Displays a list of processes and corresponding PIDs
ps gx Displays a list of processes and corresponding PIDs, including “hidden” processes
jobs Displays a list of current jobs and corresponding job-IDs.

To stop (suspend) a job/process, type CTRL+Z. The suspended process will still be listed in the list of active processes, even though it has been suspended from active execution.

To run a job in the background, the easiest way is to start the process in the background. You can do this by adding a & anywhere after the command.

Command Effect
firefox & Launches Firefox as a background process. The browser window will still appear; however, your shell will continue to accept commands.
bg Continues running the most recently suspended job in the background

You can also suspend a process running in the foreground by pressing CTRL+Z and move it to the background:

~> firefox
# firefox is now running in the foreground and the terminal is not available
# press CTRL+Z to suspend it and get the shell back
~> bg
~>
Command Effect
fg Brings the last suspended job to the foreground
fg id# Brings the job corresponding to the job-ID id# to the foreground (You can find job IDs by running jobs as mentioned above)
Command Effect
CTRL+C Terminates the current program in the foreground, telling it to quickly finish
kill id# Terminates the job/process with the job-ID/PID id#, telling it to quickly finish
kill -KILL id# Kills the job/process with the job-ID/PID id# (This is a stronger kill than the other two, which should essentially forces it to terminate without giving it time to finish)
Command Effect
date Displays the current date and time
Command Effect
lpstat -p Lists all available printers and their statuses
lp myfile Prints the file myfile on the default printer
lp -d myprinter myfile Prints the file myfile on the printer named myprinter
lp -n # myfile Prints # copies of the file myfile
lp -o sides=two-sided myfile Prints the file myfile double-sided (if the printer supports it)
lpstat -o myprinter Displays the contents of the print queue.
cancel id# Cancels the print request identified by id number id#

See Command-Line Printing for more info.

Command Effect
who Displays a list of the users logged into the current machine
Command Effect
history Displays the last 15 commands from the current session
!num Repeats the command with history number num
!str Repeats the most recent command that begins with the string str
!! Repeats the last command
!$ Expands to the last argument of the last command
grep command $HISTFILE Searches your history file for the command. history only applies to the current shell session, but the log of all shells is saved to your histfile when you exit