Linux Quick Reference
(inspired by Unix Quick Reference from my
alma mater)
This is a quick reference of commands you can use with the Linux shell. The command will be in red. Everything to
the right of the # will be comments.
Files and Directories
Make a Directory
mkdir directory-name # creates a new directory in the current working directory
Display File Contents
cat file # display contents of file
more file # page through contents of file
less file # less is equivalent to more
head file # display first 10 lines of a file
head -100 file # display first 100 lines of a file
tail file # display last 10 lines of a file
tail -50 file # display last 50 lines of a file
tail -f file # print out the last part of a
# file that is contiuously being appended to.
# usefull for monitoring log files.
For binary files:
od file # will print out the file in octal
od -c file # will print out the file in character mode
Comparing Files
diff file1 file2 # line by line comparison
cmp file1 file2 # byte by byte comparison
Changing Access Modes
chmod mode file1 file2 ...
chmod -R mode dir # (changes all files in dir )
u user (owner)
g group
o other (world)
+ add permission
- remove permission
r read
w write
x execute
chmod go-rwx foo.c # removes read, write, and execute permissions for group and
# other on foo.c.
List Files and Directories
ls # list contents of directory
ls -a # include files with "." (dot files)
ls -l # list contents in long format (show modes)
Move (or Rename) Files and Directories
mv src-file dest-file # rename src-file to dest-file
mv src-file dest-dir # move a file into a directory
mv src-dir dest-dir # rename src-dir, or move to dest-dir
mv -i src dest # copy & prompt before overwriting
Copy Files
cp src-file dest-file # copy src-file to dest-file
cp src-file dest-dir # copy a file into a directory
cp -R src-dir dest-dir # copy one directory into another
cp -i src dest # copy & prompt before overwriting
Remove File
rm file # remove (delete) a file
rmdir dir # remove an empty directory
rm -r dir # remove a directory and its contents
rm -i file # remove file, but prompt before deleting
Compressing files
gzip file # encode file, replacing it with file.gz
gzip -d file.gz # decode file.gz, replacing it with file
Change Working Directory
cd # return to your login (home) directory
cd dir # change to directory dir
Find Name of Current Directory
pwd # display absolute path of working directory
Pathnames
- simple:
- One filename or directory name for accessing local file
or directory.
Example: foo.c
- absolute:
- List of directory names from root directory
to desired file or directory name, each separated by /.
Example: /src/shared
- relative:
- List of directory names from working directory to desired
file or directory name, each separated by /.
Example: Mail/inbox/23
Directory Abbreviations
~ # Your home (login) directory
~username # Another user's home directory
. # Working (current) directory
.. # Parent of working directory
../.. # Parent of parent directory
Date
date display date and time
Environment Variables
env # displays all set environment variables
export VAR=val # set or reset environment variable VAR
unset VAR # unsets the variable VAR
echo $VAR # prints out the value of VAR
Most Important Environment Variable: PATH
When you enter a command into the shell, this variable is used to determine the program to run.
echo $PATH # will print the following
/usr/local/bin:/usr/bin:/bin:/usr/local/oracle10/bin:/usr/bin/X11:/usr/games
Notice that each path is an absolute path to a directory, with each directory separated by
a ":" . The shell determines which program to run by appending it to each of the paths respective
to their position within the environment variable. Once the program is found, it is executed.
Redirection
command > file # direct output of command to file
# instead of standard output (screen),
# replacing current contents of file
command >> file # as above, except output is appended
# to the current contents of file
command < file # command receives input from file
# instead of standard input (keyboard)
cmd1 | cmd2 # "pipe" output of cmd1 to input of cmd2
Search Files
grep string filelist # show lines containing string
# in any file in filelist
grep -v string filelist # show lines <b>not</b> containing string
grep -i string filelist # show lines containing string, ignore case
Aliases
alias string command # abbreviate command to string
History: Command Repetition
history # show command history
!num # repeat command with history number num
!str # repeat last command beginning with string str
!! # repeat entire last command line
!$ # repeat last word of last command line
Process and Job Control
Important Terms
- pid
- Process Identification number.
- job-id
- Job identification number.
Display Process and/or Job Ids
ps # report processes and pid numbers
ps -elf # as above, but include everything you ever wanted
# to know about a process
top # interactive command for viewing currently running processes
Stop (Suspend) a Job
ctrl-Z # NOTE: process still exists!
Run a Job in the Background
- Start job in background: Add & to end of command.
xdvi unixintro.dvi &
- To Force a running job into the background:
ctrl-Z # stop the job
bg # "push" the job into the background
Bring a Job to the Foreground
fg bring a job to foreground
fg %job-id foreground by job-id
Kill a Process or Job
ctrl-C kill foreground process
kill pid # kill process specified by process id pid
On-line Documentation
man command-name # display on-line manual pages
man -k string # list one-line summaries of manual
# pages containing string
The find Command
find . -name "file.txt" # will recursively search under . for the file named file.txt
find . -name "*.txt" # find any file that ends with .txt
find . -name "*.txt" -size +100k # like above but the file must be greater than 100K in size
find . -name "*.txt" -size -100k # like above but the file must be smaller than 100k in size
find . -name "*foo*" # will find any file with foo in the file name
find . -iname "foo*" # Case insensitive match for a file that starts with foo or Foo or fOo or foO or FOo , etc...
find . -mmin -5 # find all files that were modified less that 5 minutes ago
find . -mmin +5 # find all files that where modified greater than 5 minutes ago
find . -mmin 5 # must be exactly 5 minutes old (don't use this)
The grep Command
grep string file # will print out all lines that contain the string "string" within the file file
grep -c string file # will print out the number of lines that match
grep -n string file # will print out all lines, but prefix each line with the line number
The awk Command
The awk command is quite handy at processing text that is generally columnar
awk '{print $1}' filename # will print the first column of each line in the file filename
# Each column is separated by whitespace
awk -F, '{print $1}' filename # will do as above, but now the columns are separated by commas