UNIX Command Cheat Sheets
UNIX Command Cheat Sheets
com
sort infile Sorts the contents of the input file in sort names Sorts the contents of names in
alphabetical order alphabetical order
who Tells you who is logged onto your server who None
clear Clears the window and the line buffer clear None
echo whatever I Writes whatever I type to the screen. echo hey you! Writes hey you! to the screen
type
banner big words Does the same thing as echo only in BIG banner hey! Writes hey! in large letters on the
words screen
cat file1 file2 file3 Shows the three files in consecutive order cat cheese milk This prints the cheese file to the
as one document (can be used to combine screen first and immediately
files) follows it with the milk file.
df system Reports the number of free disk blocks df ~ Both commands will print the
df $HOME total kb space, kb used, kb
available, and %used on the home
system (your system).
head file Prints the first 10 lines of the file to the head addresses Prints the first 10 lines of
screen addresses to the screen
Number of lines can be modified head -25 addresses Prints the first 25 lines of
addresses to the screen
tail file Prints the last 10 lines of the file to the tail test.txt Prints the last 10 lines of test.txt
screen to the screen
Number of lines can be modified here, tail -32 test.txt Prints the last 32 lines of test.txt
too to the screen
more input This prints to screen whatever is input— more groceries This will list the groceries file to
useful because it only shows one screen at the screen.
a time.
scroll bar continues to the next screen
return moves one line forward
Q quits
G goes to the end
1G goes to the beginning
Ctrl u moves up ½ screen
Ctrl d moves down ½ screen
Important Characters
| “pipe” directs the output of the first ls -l | more Lists your files in long format one
command to the input of another. screen at a time
> Sends the output of a command to a ls -l > myfiles Prints your listing to a file named
designated file myfiles
>> Appends the output of a command to a ls -l >> allfiles Appends your filenames to the end of
designated file the allfiles file
& Runs command in the background; you xclock & Runs xclock (a clock) allowing you to
can still work in the window keep working
~ Designates the home directory echo ~ Writes your home directory to the
($HOME) screen
< Designates input from somewhere other progA < input1 progA program gets its input from a
than terminal file named input1
Wildcards UNIX has a set of wildcards that it accepts.
mkdir dirname Creates a directory mkdir junk Makes a directory named junk in
your current directory
You can also designate where the directory is to mkdir ~/left Makes a directory in your home
reside. directory named left
rm file1 file2 file3 Removes (deletes) file(s) rm xyz Deletes a file named xyz
rm xyz abc Deletes the files named xyz and
abc
rm * Deletes everything nonhidden
rm -i file1 file2 Prompts before deletion of files rm -i * Prompts at each nonhidden file
*******USE -i AT FIRST******* and lets you decide whether or
not to delete it
rm -f file1 file2 Forces deletion without prompt regardless of rm -f program Removes the file program
permissions without regard to permissions,
status, etc.
rm -r directory Remove a directory along with anything inside of rm -r bin Each of these will remove the bin
rm -R directory it rm -R bin directory and everything inside of
it.
rmdir directory Removes a directory like rm -r does if the rmdir bin Removes the bin directory if it is
directory is empty empty
****dangerous**** This combination will force the removal of rm -Rf c_ya Forces removal without prompts
rm -fR name any file and any directory including anything of the c_ya directory and
rm -Rf name inside of it anything inside of it
rm -Ri directory Deletes the contents of a directory and the rm -Ri rusure Deletes anything in the directory
directory if it is empty by prompting the user called rusure that you verify at
before each deletion the prompt, and if you remove
everything in the directory, you
will be prompted whether you
want to remove the directory
itself or not
NOTE: Options can be combined using rm
rmdir -p directory Removes a directory and any empty parent rmdir -p /home/bin/dir1 Deletes the dir1 directory; if bin
directories above it (-pi does the same directory is empty, it is deleted,
thing but it prompts before each removal) and if home directory is empty it
is also deleted
pwd Prints the current directory to pwd May print something like
the screen “/home/bob”
pr (option) filename Prints the specified file to the pr userlist Prints the contents of userlist to
default printer (options are not the default printer
required but can be combined
in any order)
pr +k filename Starts printing with page k pr +5 userlist Prints the contents of userlist
starting with page 5
pr -k filename Prints in k columns pr -2 userlist Prints the contents of userlist in 2
columns
pr -a filename Prints in multicolumns across pr -3a userlist1 Prints userlist in three columns
the page (use with -k) across the page
pr -d filename Prints in double space format pr -d userlist Prints userlist with double space
format
pr -h “header” filenamePrints the file with a specified pr -h “users” userlist Prints userlist with users as the
header rather than the filename header
NOTE: Options can be combined using pr
lpstat (-options) Prints printer status information to lpstat Prints status of all requests made
screen (options not required) to the default printer by the
current server
lpstat -u“user1, user2” Prints the status of requests made by lpstat -u“bob” Prints status of all requests made
the specified users by the user with the id bob
lpstat s Prints the queues and the printers they none None
print to
lpstat -t Shows all print status information none None
lpstat -d Shows the default printer for the lp none None
command
lpstat -r Lets you know if the line printer none None
scheduler is running
lp (-option) file(s) Like pr, this prints designated files on lp junkfile Prints the file junkfile to the
the connected printer(s) (options not default printer in default one-
required and options may be sided, single-sided, single-spaced
combined). format
lp -ddest file(s) Prints the file(s) to a specific lp -dbobsq zoom Sends the file zoom to the bobsq
destination print queue to print
lp -nnumber file(s) Allows user to designate the number of lp -n5 crash Prints five copies of crash in
copies to be printed default settings
lp -ttitle file(s) Places title on the banner page lp -tBobs cash Prints Bobs on the banner page of
the file printout named cash
lp -ooption file(s) Allows printer-specific options to be lp -od output Prints the output file double-
used (i.e., double-sided or two pages sided on the printout
per side, etc.)
lp -obold output Prints output in bold print
lp -ohalf output Divides the paper into two halves
for printing output
lp -oquarter output Prints four pages of output per
side of paper
lp -olandscape output Prints output in landscape
orientation
lp -oportrait output Prints output in portrait
orientation
NOTE: Options can be combined using lp
cancel request_id Stops print jobs or removes them from cancel 5438 Stops the print job with the id
the queue (request_ids are obtained 5438 whether it is printing or if it
using lpstat) is sitting in the queue
cancel -a printer Removes all print requests from the cancel -a bobsprt Removes all the requests from the
current user on the specified printer current user to the printer named
bobsprt
cancel -u login_id Removes any print requests queued cancel -u bob Cancels all queued print requests
belonging to the user for user bob
©2001 TechRepublic, Inc. www.techrepublic.com. All rights reserved. 5
UNIX Command Cheat Sheets www.techrepublic.com
kill process_id Stops the process with the said id kill 6969 Kills the process with PID 6969
kill -9 process_id Destroys the process with the said id kill -9 6969 PID # 6969 doesn’t have a chance
here.
grep string file Searches input file(s) for specified grep mike letter Searches for the string mike in the
string and prints the line with file named letter and prints any
matches line with mike in it to the screen
grep -c string file Searches and prints only the number grep -c hayes bankletter Searches the file bankletter for the
of matches to the screen string hayes and prints the number
of matches to the screen
grep -i string file Searches without regard to letter case grep -i hi file1 Searches file1 for hi, Hi, hI, and
HI and prints all matches to the
screen
grep -n string file Prints to the screen preceded by the grep -n abc alpha Searches alpha for abc and prints
line number the matches’ lines and line
numbers to the screen
grep -v string file All lines that do not match are printed grep -v lead pencils Prints all lines in pencils that do
not contain the string lead
grep -x string file Only exact matches are printed grep -x time meetings Prints only lines in meetings that
match time exactly
grep is useful when you use it in a | ps -ef | grep bob Finds all processes in full listing
“pipe” and then prints only the ones that
match the string bob to the screen
You can also redirect its output grep -i jan b_days>mymonth Searches the file b_days for case-
to a file. insensitive matches to jan and
places the matching lines into a file
called mymonth
vi filename Text editor that exists on every UNIX system none None
in the world
compress filename Compresses the file to save disk space. none None
uncompress filename Expands a compressed file none None
eval `resize` Tells the target computer that you’ve resized none None
the window during telnet
chexp # filename Keeps the file(s) from expiring (being chexp 365 nr* Keeps the target computer
erased) on the target computer for # days from deleting all files starting
with nr for 1 year (365 days)
chexp 4095 nr* Makes all files whose name
starts with nr never expire or
be deleted (infinite)
qstat Displays the status of a process that has been qstat Shows the status of the
submitted the Network Queuing System requests submitted by the
(basically a batch job) invoker of the command—
this will print request-name,
request-id, the owner, relative
request priority, and request
state (is it running yet?)
qstat -a Shows all requests
qstat -l Shows requests in long
format
qstat -m Shows requests in medium-
length format
qstat -u bob Shows only requests
belonging to the user bob
qstat -x Queue header is shown in an
extended format
xterm Opens a new window (x-terminal) for you to xterm This opens another window
xterm -option work like the one you are currently
xterm +option -option sets the option working in.
+option resets the option to default USING XTERM WILL
ELIMINATE A LOT OF
DESKTOP CLUTTER. I
STRONGLY SUGGEST
YOU LEARN TO USE IT
IN YOUR SCRIPTS.
xterm -help Displays the xterm options xterm -help Shows the options available
xterm -(areas) color Allows you to modify different xterm -bg white The first command sets the
colors in your xterm window background color to white.
xterm -bd huntergreen
The second command sets the
window border color to
xterm -fg red huntergreen.
The third command window sets
the text color to red.
xterm -fn font Sets the font in the new xterm xterm -fn courr18 Sets the font to courr18 (default
window is fixed)
xterm -iconic Starts the new xterm as an icon xterm -iconic -title xyz Opens an xterm in iconic form
(double-click to maximize) with the title xyz
NOTE: Options can be combined using xterm