Department of Electrical Engineering and Computer Science



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. For more detailed information about some of these commands, see our section of Linux Topics.

Unix Fundamentals

Pathnames

ExamplePath Description
foo.c The file foo.c in the current (working) directory
pgms/foo.c There is a directory within the current directory named pgms, which contains a file named foo.c (pgms/foo.c is called a relative path.)
/home/jruser/pgms/foo.c The full path to the file foo.c based on the root of the file system

Directory abbreviations

AbbreviationMeaning
~ Your home (login) directory
~username The home directory for the username
. The working (current) directory
.. The parent directory of the current one
../.. The parent of the parent directory

Wildcards

CharacterMeaning
? Match a single character
* Match zero or more characters

Some examples:

ExpressionDescription
fo?.c Matches "fo" followed by a single character, followed by ".c".
Examples: "foa.c", "fob.c", "foc.c", "fo1.c", "fo1.c", etc.
foo.* Matches "foo." followed by zero or more characters.
Examples: "foo.txt", "foo.exe", "foo.", "foo.png", etc.

Redirection

CommandEffect
command > myfile Redirects the output of command to the file name myfile instead of the terminal (standard output). If myfile already exists, its contents will be replaced.
command >> myfile Similar to >, except the output of command is appended to the current contents of the file myfile.
command < myfile The input to command now comes from the file myfile instead of from the keyboard (standard input).
cmd1 | cmd2 "Pipes" the output of cmd1 to the input of cmd2.
script myfile Logs everything displayed on the terminal to the file myfile. The logging is terminated with exit.

Account Password

To change your EECS account password, run eecs-passwd. See the Passwords page for more details.

File system

Creating a file

CommandEffect
cat > myfile Allows you to enter text with the keyboard to be stored in the file named myfile. After entering the desired text, press Ctrl+D
vi myfile Launches the Vi (ViM) text editor, given a file name of myfile

Creating a directory

CommandEffect
mkdir dir2 Creates a directory (within the current directory) named dir2

Displaying file contents

CommandEffect
cat myfile Displays entire contents of the file myfile at once.  Note that to scroll this content, your terminal program (e.g., Gnome Terminal on Linux, PuTTY on Windows, Terminal on Mac) must be able to handle the scrolling.
more myfile Displays contents of the file myfile one page (full screen) at a time
less myfile Displays contents of the file myfile in a full screen view, is scrollable with the up and down arrow keys.

Comparing files

CommandEffect
diff file1 file2 Performs a line-by-line comparision of the files file1 and file2.  The differences are displayed in the shell window.
cmp file1 file2 Performs a byte-by-byte comparison of the files file1 and file2. The differences are displayed in the shell window.

Changing access modes

CommandEffect
chmod mode file1 file2 Changes the modes of both files file1 and file2 to mode
chmod -R mode dir Changes the modes of all files and directories within the dir directory to mode

Here are the mode settings:

Users affected:
u: user (owner)
g: group
o: other (world)

Actions:
+: add permission
-: remove permission

Access types:
r: read
w: write
x: execute

For example, chmod go-rwx foo.c removes read, write, and execute permissions for "group" and "other" users for the file foo.c.

Listing files and directories

CommandEffect
ls Lists the contest of the current (working) directory
ls -a Lists the contents of the current directory, including dotfiles
ls -l Lists the contents of the current directory in long format (shows access modes)

Moving (or renaming) files and directories

CommandEffect
mv src-file dest-file Renames the file src-file to dest-file
mv src-file dst-dir Moves the file src-file into the dest-dir directory
mv src-dir dest-dir Renames the src-dir directory to dest-dir.  Alternately, if dest-dir already exists, move the src-dir directory into the dest-dir directory.
mv -i src dest Moves src to dest but prompts before overwriting existing files/directories

Copying files

CommandEffect
cp src-file dest-file Makes a copy of the file src-file and names the copy dest-file
cp src-file dest-dir Copies the file src-file into the dest-dir directory
cp -R src-dir dest-dir Copies the src-dir directory and all of its contents into the dest-dir directory
cp -i src dest Copies src into dest, but prompts before overwriting existing files

Deleting files and directories

CommandEffect
rm myfile Deletes (removes) the file named myfile
rmdir mydir Deletes the empty directory named mydir
rm -r mydir Deletes the mydir directory and all of its contents
rm -i myfile Deletes the file myfile, but prompts before doing so

Compressing files

CommandEffect
gzip myfile Compresses the file myfile, replacing it with the file named myfile.gz
gunzip myfile.gz Uncompresses the file myfile.gz, replacing it with the file named myfile

Changing the working directory

CommandEffect
cd Changes the current (working) directory to your login (home) directory
cd mydir Changes to the mydir directory

Displaying the name of the current directory

CommandEffect
pwd Displays the absolute path of the current (working) directory

Searching within files

CommandEffect
grep hello myfile Displays the lines within the file myfile that contains the string hello
grep hello file1 file2 Displays the lines within the files file1, file2, etc. that contain the string hello
grep hello * Displays the lines within any file in the current directory that contain the string hello
grep -v hello * Displays the lines within any file in the current directory that do not contain the string hello
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 and job control

Important terms

  1. PID:  process identifier.
  2. job-ID:  job identifier.

Listing process and jobs

CommandEffect
ps Displays a list of processes and corresponding PIDs
ps gx Displays a list of processes, including "hidden" processes, and corresponding PIDs
jobs Displays a list of current jobs and corresponding job-IDs

Stopping (suspending) a job/process

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.

Running a job in the background

CommandEffect
firefox & Launches Firefox as a background process (note the ampersand). The browser window will still appear; however, your shell will continue to accept commands.

Suspending a process will also run that process in the background. Here's an example:

prompt> firefox     (Ctrl+Z pressed here)
[1]+ Stopped
prompt> bg

The first command launches Firefox, and pressing Ctrl+Z suspends that Firefox process. Running the bg command resumes Firefox, but begins running it in the background.

Bringing a job to the foreground

CommandEffect
fg Brings a suspended job to the foreground
fg id# Brings the job corresponding to job-ID id# to the foreground

Terminating a job/process

CommandEffect
Type Ctrl+C Terminates (kills) the foreground job/process
kill -KILL id# Terminates (kills) the job/process with the job-ID/PID id#

Miscellaneous commands

Displaying the current date and time

CommandEffect
date Displays the current date and time

Printing

CommandEffect
lpr myfile Prints the file myfile on the default printer
lpr -Pmyprinter myfile Prints the file myfile on the printer named myprinter
lpr -c# myfile Prints # copies of the file myfile
lpr -d myfile Prints the file myfile, interpreting it as a DVI file
lpr -Z duplex myfile Prints the file myfile double-sided (if the printer supports this feature)
lpq Displays the contents of the print queue. The -Pmyprinter argument is also accepted.
lprm -# Removes the print request # (listed with lpq) from the print queue

See the Linux Printing Guide for many more details on this topic.

Getting information on users

CommandEffect
finger username Displays information about the user username
finger username@machine Displays information about the user username logged into the machine named machine
finger @machine Displays a list of the users logged into the machine named machine
who Displays a list of the users logged into the current machine

Using the command history

CommandEffect
history Displays the command history
!num Repeats the command with history number num
!str Repeats the most recent command that begins with the string str
!! Repeats the most recent command
!$ Repeats the word of the most recent command line

Using the Linux manual

CommandEffect
man command Display the manual page for the topic command
man -k keyword Displays one-line summaries of the manual pages containing the string keyword

 


 

The University of Tennessee, Knoxville. Big Orange. Big Ideas.

Knoxville, Tennessee 37996 | 865-974-1000
The flagship campus of the University of Tennessee System