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

Example

Path 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

Abbreviation

Meaning

~

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

Character

Meaning

?

Match a single character

*

Match zero or more characters

Some examples:

Expression

Description

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

Command

Effect

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

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 Ctrl+D

Creating a directory

Command

Effect

mkdir dir2

Creates a directory (within the current directory) named dir2

Displaying file contents

Command

Effect

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

Command

Effect

diff file1file2

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

cmp file1file2

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

Changing access modes

Command

Effect

chmod modefile1file2

Changes the modes of both files file1 and file2 to mode

chmod -R modedir

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: groupo: other (world)

Actions:+: add permission-: remove permission

Access types:r: readw: writex: 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

Command

Effect

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

Command

Effect

mv src-filedest-file

Renames the file src-file to dest-file

mv src-filedst-dir

Moves the file src-file into the dest-dir directory

mv src-dirdest-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 srcdest

Moves src to dest but prompts before overwriting existing files/directories

Copying files

Command

Effect

cp src-filedest-file

Makes a copy of the file src-file and names the copy dest-file

cp src-filedest-dir

Copies the file src-file into the dest-dir directory

cp -R src-dirdest-dir

Copies the src-dir directory and all of its contents into the dest-dir directory

cp -i srcdest

Copies src into dest, but prompts before overwriting existing files

Deleting files and directories

Command

Effect

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

Command

Effect

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

Command

Effect

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

Command

Effect

pwd

Displays the absolute path of the current (working) directory

Searching within files

Command

Effect

grep hellomyfile

Displays the lines within the file myfile that contains the string hello

grep hellofile1file2

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

PID: process identifier.

job-ID: job identifier.

Listing process and jobs

Command

Effect

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

Command

Effect

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]+ Stoppedprompt> 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.