Skip to content

Instantly share code, notes, and snippets.

@boredzo
Created January 5, 2016 05:40
Show Gist options
  • Save boredzo/65953408da506df42d46 to your computer and use it in GitHub Desktop.
Save boredzo/65953408da506df42d46 to your computer and use it in GitHub Desktop.
The Linux Cheatsheet I published 12 and a half years ago (and almost 15 years after the original Linux Cheatsheet).
About the Linux Cheatsheet
==========================
maintained by Mac-arena the Bored Zo, [email address that no longer works]
copyright 2003 Mac-arena the Bored Zo
available from [URL no longer works]
HTML/PHP version at [URL works but no longer has a mirror]
MIRRORS WELCOME! please email me if you want to mirror.
some commands submitted by Steve Leopardi [email address deleted],
frost, and rajnchaos
Linux Cheatsheet IS NOT PUBLISHED BY IDG BOOKS and anyone who says it is is
an idjit. :) IDG publishes the "Cheat Sheet" line of books - note the space.
Cheatsheet and the Cheat Sheets are unrelated.
Conventions
===========
* listings are useful to root (or limited to root).
Conventions - Modifier keys
===========================
cmd = command or windows key
opt = alt, option, or meta key
sft = shift key
ctrl or ^ = ctrl
l prefix = left (e.g. left shift)
r prefix = right (e.g. right shift)
Conventions - Key separators
============================
- = press these together (e.g. cmd-q = hold cmd & Q together, then release both
, = press in order (e.g. press ctrl-x, then ctrl-c)
Shell prompt
============
clear = clear screen and put prompt at top
command command args = run command w/ args as its parameters, ignoring aliases
date = put system date to stdout (\d in $PS1 is recommended instead)
*rdate server = sync clock with server via NTP (I recommend tock.usno.navy.mil)
*uptime = put how long the system has been up to stdout
*free = how much memory is available
Shell prompt - Shells
=====================
bash = Bourne Again SHell
pdksh = Public Domain Korn SHell (aka ksh)
tcsh = TENEX C SHell
sh = bourne SHell
zsh = Z SHell
mc = Midnight Commander - not CLI, rather like DOS Shell actually
pilot = Pine's Internal Lister of Things - simpler than mc; included w/ pine
Shell prompt - IP
=================
nslookup host = resolve host (could be either a hostname or IP)
nslookup host DNS = resolve host using given DNS
ping host = send an ICMP ping to host
whois hostname = whois this hostname (rs.internic.net)
Shell prompt - Power on/off
===========================
*shutdown -h now = shutdown immediately
*shutdown -r now = shutdown and restart immediately
*shutdown -h/-r minutes = shutdown and restart in minutes
Shell prompt - Users and Logins
===============================
*who = see who is on the system
write person = send a message to person (ctrl-D to end)
mesg boolean = set whether you can be written to (y or n)
login = login again (does not kill your previous shell)
su = same as above with "root" for username
logout = logout and disconnect your session
exit = quit the non-login and/or Korn shell (same as kill -9'ing its PID)
Shell prompt - Users and Logins - /etc/passwd format
====================================================
username:passwd*:UID:GID:finger name:home dir:shell
*usually just '*'; passwords are stored elsewhere
Shell prompt - File manipulation
================================
cd folder = switch to folder
folder = /folder = folder "folder" at the top level
folder = folder = folder "folder" in .
folder = . = current folder
folder = .. = folder that is one level higher
folder = ~ = home directory (as defined in $HOME)
folder = ~user = user's home directory
these can be combined, as in '/~boredzo/src/../bin/./emacs'
pwd = put working directory (use sh \w, csh %~, or ksh $(pwd) in $PS1 instead)
ls -AdFkl folder = show infomation on folder (which of course could also be a
file)
ls -ABCF = multicolumn list of all files, folders, and links except . and ..
ls -ABFkl = long (1-col) list of all files, folders, and links except . and ..
ls -Al|wc -l = count files/folders
ls -Al folder|wc -l = count files/folders in folder
touch -r file time = set modification date of file to time (now if omitted)
chmod user[+-=]permissions file = set permissions for user of file to
permissions (see Symbolic Permissions below)
chmod octal file = set permissions of file to octal (see below)
chown user file = give file to user - to get it back they must give it back!
chown user:group file = give it to group as well
mv file file = rename file1 to file2
mv file folder = move file to folder
mv file folder/file = move file1 to folder, then rename it to file2
cp source dest = copy source to dest (e.g. cp ~/risebox/cheatsheet.txt ~)
cp -a source dest = exact copy
ln -s source target = create a symbolic link (alias, shortcut) to source at
target
ln source target = create a hard link - needed for applications
grep pattern = search for pattern in the text piped to grep (see below)
grep -f file = search for the patterns in file in the text piped to grep
cat file1 file2 = append file2 to file1 and put the result to stdout
cat file = display file uninterrupted
less -M file = display file (see below for paging cmds)
less -M = display whatever is piped to less
head file = display the first 10 lines of file
tail -f file = display the last 10 lines of file, adding as the file grows
wc -l file = count lines in file
wc -w file = count words in file
wc -c file = count characters in file
du -b file = file size for file in bytes
du -k = ...in kibibytes
du -BM = ...in mebibytes
Shell prompt - Symbolic permissions
===================================
u = user who owns the file
g = group that owns the file
o = Public At Large (others)
a = all of the above
+ = add given permissions, ignore others
- = revoke given permissions, ignore others
= = set permissions absolutely (add specified perm's and revoke any others)
r = read
w = write
x = execute
s = user/group ID bit; process owner == file owner (instead of the user opening
the file)
t = sticky bit; contents stay in memory until pushed out by disuse
chmod a=rwx,o-w file.txt = sets permissions of file.txt to rwx for file owner
and his group, but only rx for anyone else
chmod a=rwx,o-w,u+s file.txt = sets permissions of file.txt as above, but anyone
who runs it will have the same access as the file owner (IOW, file owner = process owner) - so watch out if owner is root
octal is usually more efficient, and NEVER set your shell +s! this gives anyone
with an account root access without being root!
Shell prompt - Octal permissions
================================
down = permission
across = position in octal number (from right)
1 2 3 4
1 o+x g+x u+x o+t, sticky bit (prog is cached in RAM; faster reload)
2 o+w g+w u+w g+s, if g+x, group ID (group=process owner), else lock
4 o+r g+r u+r u+s, if u+x, user ID (file owner = process owner)
total number at left to get permission to set - a=rwx is 777; u=rwx,o=r is 704
add 4th digit for sticky/[UG]ID bits, e.g. a=rwx,u+s = 4777
Shell prompt - Environment
==========================
alias name=value = alias name to value (typing name then executes value)
alias name = display the alias name
unalias name = delete alias
typeset -x variable=value = set variable to value
variable=value;export variable = same as above
export variable=value = same as above
typeset = show environment (all variables)
set = same as typeset
env = same as typeset, but doesn't show as many
Shell prompt - Environment - PS1 (prompt) - Bourne (sh/bash)
============================================================
\d = date
\h = machine name
\H = entire hostname
\n = newline
\! or \# = how many cmds you've executed so far (including login and this one)
\s = the name of the current programme, i.e. the shell
\t = time (24-hr)
\T = time (12-hr) - bash 2.0 only
\@ = time (AM/PM) - bash 2.0 only
\u = $LOGNAME
\w = working directory
\\ = backslash (\)
\nnn = the character expressed by octal nnn (see ASCII character list below)
Shell prompt - Environment - PS1 (prompt) - C (csh/tcsh)
========================================================
%d %w %D = date (same as bash \d; add %Y for year)
%m = machine name
%M = entire hostname
%B and %b = begin/end boldface
%U and %u = begin/end underline
%S and %s = begin/end inverse (standout mode)
%! or %h = how many cmds you've executed so far (including login and this one)
%T = time (24-hr)
%P = time (24-hr w/ secs)
%t or %@ = time (12-hr)
%p = time (12-hr w/ secs)
%n = $LOGNAME
%~ = working directory (equivalent to bash)
%c0n = last n (default 1) segments of the working dir; if 0 is present, replace
missing parts with "/skipped" e.g. "/skipped/boredzo"
%% = percent sign (%)
\nnn = the character expressed by octal nnn (see ASCII character list below)
Shell prompt - Environment - PS1 (prompt) - Korn (ksh/pdksh)
============================================================
...has no PS1 variables. use command or variable substitution instead (see Korn Shell Script below)
Shell prompt - Processes
========================
ps = show all open processes
ps aux|grep pattern = list all processes matching pattern
nohup command = execute command without killing it on logout
kill PID = kills (terminates) the process marked PID (according to ps)
*kill -9 PID = force-kills the process marked PID (according to ps) using
SIGKILL - equivalent to Mac cmd-opt-esc or DOS ctrl-alt-del
the process does not get to clean up after itself; USE WITH CAUTION!!!
you can (and very probably will) lose data this way!
screen command = start screen and execute command within it (sort of a shell)
ctrl-A,d = detach a process launched with the above method
screen -r = reattach all processes bkgnd'd with the above sequence
(to detach = to suspend output to stdout; reattach = resume it again)
(this effectively "hides" an application, and in a lot of cases can be better
than command &)
command & = executes command and backgrounds it
fg = resume all suspended (&'d) cmds
crontab -e = edit your crontab schedules
Shell scripting (Bourne and Korn)
=================================
test -e file = file exists
test -x file = ...and is executable
test -w file = ...writable
test -r file = ...readable
test -s file = ...not empty
test -d file = ...a directory
test -f file = ...a regular file (not a device driver or directory)
test text1 = text2 = test whether text1 is the same as text2 (string)
test text1 != text2 = test whether it isn't (string)
[ expression ] can also be used instead of test expression
Korn shell recommends you use [[ expression ]] instead of either
break = exit from a loop
echo text = display text on the screen
echo -n text = display text but don't put a newline after it
echo -e text = display text and look for these wildcards:
\n = newline
\t = tab
\a = alert (system beep character, ctrl-g - see ASCII character list)
\0nnn = character with octal value nnn (see ASCII character list)
\\ = a backslash
print text = same as echo -e, for ksh (use print -r to emulate echo)
print -un text = print to file descriptor n (ksh only)
print -r text = print to file descriptor n (ksh only)
read variable = assign 1 line of user input to $variable
if commands
then
commands
elif
commands
then
commands
else
commands
fi = determines the result of a command(s) and executes other commands based on it (elif and else are optional)
case text in
pattern) commands;;
pattern) commands
morecommands;;
...
esac = determine whether text matches pattern, pattern, etc. and act on it
(often used with read for a prompt)
Regular expressions (patterns)
==============================
. = any character except \000 ("") or \n (newline)
* = 0 or more times
+ = 0 or 1 times
? = 1 times
[characters] = any of characters
^ (at beginning of character class) = all BUT the chars in the class
- = range (e.g. [0-9a-zA-Z] searches for all alphanumeric chars)
(pattern) = search for a subpattern (e.g. [(fools)(fool's)] gold)
\ = escape character (don't parse it) - e.g. \. searches for a period
^ (at beginning of pattern) = search only at start of line
$ (at end of pattern) = search only at end of line
{number} = number instances of preceding char, separated by commae
{,number} = no more than number instances
{number,} = no fewer
less - Paging Commands
======================
b = go backward one screen
f = go forward one screen
space = same as f
up = go backward one line
down = go forward one line
e = launch $EDITOR (usu. ed)
v = launch $VISUAL (def. vi, but emacs recommended)
p,percentage = go to percentage of the file
/,pattern = search for pattern
Compressing
===========
archive = the archive to make from
original = the file or folder to compress
tar -czf archive original = tar and GZip
zip -mR9yT archive original = zip (Windows)
Decompressing
=============
archive = the archive to decompress
tar -xzf archive
unzip -tX archive
pine - Shell prompt
===================
pine = start pine and go to the main menu
pine addr = start pine with a blank message to addr, and quit when it's sent/canceled
pine - Main menu
================
c = create new outgoing message
l = folder list
a = address book
pine - Lists
============
- = backward (up) one screen
space = forward (down) one screen
d = mark item for deletion
u = unmark item for deletion
r = reply to it
f = forward it to someone else
< = previous list or menu
return or enter or > = view it
tab = select next new message
c = compose new message
pine - Lists - Entry markings
=============================
N = new; hasn't been read
(nothing) = has been read
A = has been read and/or replied to (i.e. answered)
pine - Incoming messages
========================
- = bwd one screen
spc = fwd one screen
v = view attachment list
pine - Outgoing messages
========================
ctrl-x = send message
ctrl-c = cancel and dispose of message (w/o sending)
ctrl-j = attach file(s)
ctrl-y = bwd one screen
ctrl-v = fwd one screen
ctrl-k = cut to end of line (same as emacs ctrl-k)
ctrl-u = uncut (same as emacs ctrl-y)
vi - Shell prompt
=================
vi = start vi with an empty buffer and no open files
vi file = open file for editing
vi - Command mode
=================
i = input mode
esc = command mode
:se number = display line numbers
:se nonumber = don't display line numbers
:e = open file
:w = save (write) file to disk
:wq = save and quit
:q! = quit but don't save
number,command = apply command number times (e.g. 4,x = delete 4 characters)
. = repeat last effective cmd
esc,u = undo it
vi - Command mode - Moving the cursor
=====================================
- = go to beginning of previous line
0 (zero) or ^ = go to beginning of this line
$ = go to end of this line
ret or + = go to beginning of next line (NOT THE SAME AS INPUT RETURN)
w = go to beginning of next word
b = go to beginning of this word
e = go to end of this word
sft-h = go to first line onscreen
sft-l = go to last line onscreen
ctrl-f = forward (down) one screen
ctrl-b = backward (up) one screen
1,sft-g = top of the buffer
sft-g = bottom of the buffer (i.e. EOF)
:line = go to line (e.g. :35 = go to line 35)
column,| = go to column (e.g. 4,| = column 4)
vi - Command mode - Deletion
============================
deleted text is put to the general purpose buffer; use the p cmd to retrieve it
you can also use yanks to not delete it but still put it in the GPB
basically delete = cut and yank = copy in your OS's Edit menu
x = delete this character
d,w = delete word
d,d = delete line
sft-d = delete to end of line (same as emacs ctrl-k)
y,w = yank to beginning of next word
sft-y or y,$ = yank to end of line
y,y = yank entire line
buffer"lines"y,y = yank lines number of lines into buffer buffer (a-z)
p = paste contents of general-purpose buffer
vi - Command mode - Replacement
===============================
to use replacers type the sequence and then type the string of characters to
replace with, e.g. 2,c,w,"Bored Zo" to replace from the cursor to the end of
the next word to "Bored Zo"
r = replace a single character
shift-r = replace characters from the cursor(e.g. if | is the cursor and your text is "I AM the |Entertained Zo!" shift-r,"Bored" will give you "I AM the Boredtained Zo!")
c,w or c,e = change from cursor to end of word
c,b = change from beginning of word to before cursor
c,$ or sft-c = change from cursor to end of line
c,c = change the ENTIRE line
:x,ys/oldstring/newstring/ = substitute newstring for oldstring from line x to line y (if y = $ then to EOF)
~ = toggle a character's case
vi - Command mode - Searching
=============================
see section on regular expressions above
/ = search forward from cursor for a pattern
? = search backward from cursor for a pattern
n = next result
sft-n = previous result
vi - Command mode - Insertion
=============================
o = insert a blank line below this one
sft-o = ...above this one
:r file = insert file at cursor
emacs
=====
note that you can usually use opt- instead of esc, and each binding will still
work.
e.g. opt-esc,esc = cancel (like ctrl-g), opt-x = summon command prompt
emacs - Shell prompt
emacs = start emacs with one empty buffer and no open files
emacs file = open file for editing
emacs file +number = open file and scroll to line number
ctrl-x,ctrl-c = quit
emacs - Buffers
===============
ctrl-x,ctrl-f = open file and create a buffer for it
ctrl-x,b = switch buffers (default is the next)
ctrl-x,k = kill (close) buffer and file
ctrl-x,0 (zero) = kill window
ctrl-x,1 = kill other window(s)
ctrl-x,2 = spawn new window under this one
ctrl-x,3 = spawn new window to the right of this one
ctrl-x,o (oh) = switch to other window
emacs - Text mangling - Moving the cursor
=========================================
arrows = move cursor 1 character in any of the 4 cardinal directions - horizontal position in the line is maintained when you move u/d
esc,f = fwd (right) one word
esc,b = bwd (left) one word
ctrl-a = move to start of line
ctrl-e = end of line
esc,r = move to window line (?!)
ctrl-v = fwd (down) one screen (typically 22-24 lines)
esc,v = bwd (up) one screen
ctrl-x,] = fwd one page (defined by lpd?)
ctrl-x,[ = bwd one page
esc,< = top of buffer
esc,> = bottom of buffer
emacs - Text mangling - Moving strings
======================================
ctrl-t = transpose this character and the previous
esc,t = transpose this word and the next or previous depending on position in word
ctrl-x,ctrl-t = transpose this line and the previous
esc,c = properly capitalises the word (Like this)
ctrl-x,ctrl-l = decapitalises the selection
esc,l = decapitalises the entire word (like this)
ctrl-x,ctrl-u = capitalises the selection
esc,u = capitalises the entire word (LIKE THIS)
ctrl-spc = set mark
ctrl-w = cut selection (text from mark to cursor) to kill buffer (clipboard)
esc,w = copy selection
ctrl-y = paste ("yank") contents of kill buffer to working buffer
emacs - Text mangling - Search and replace
==========================================
ctrl-s = search fwd (down) from cursor
ctrl-r = search bwd (up) from cursor
esc,ctrl-s = search fwd from cursor using a regular expression (see above)
esc,ctrl-r = search bwd from cursor using a regexp
esc,sft-ctrl-5 = replace fwd from cursor using a regexp
ctrl-g = cancel
! = replace all from cursor and including this hit
? = help
. = go back to where search started and exit replace
y/spc = replace and continue
n = don't replace but continue
emacs - Text mangling - Deletion
================================
anything you delete is put to the kill buffer - up to kill-buffer-max (def. 30)
can be in the kill buffer at any one time
ctrl-y = yank last (most recent) item in kill buffer
esc,y = substitute just-yanked text for next-to-last item in kill buffer
ctrl-d = delete this character (same as fwd-del)
del = delete previous character
esc,d = delete next word
ctrl-w = wipe (kill) selection and put it to kill buffer (see above)
ctrl-k = kill everything from cursor to end of line (eol)
emacs - Text mangling - Rectangular editing
===========================================
rectangles are defined with the mark as one corner and the insertion point as
the other
ctrl-x,r,k = kill (cut)
ctrl-x,r,d = delete (clear)
ctrl-x,r,y = yank (paste)
ctrl-x,r,c = blank: replace chars within w/spaces
emacs - Email - rmail
=====================
m = make a new message in sendmail mode in second window
g = check email
esc,x,rmail = start rmail mode and check email
emacs -f rmail = ...from the shell
h = list your email
ctrl-opt-t = ...that have a certain subject
opt-`,sft-s,s = ...that were sent by a certain entity
ctrl-opt-s = ...that match a regexp
opt-p = read previous message
opt-n = read next message
sft-, = read first message
sft-. = read last message
number,j = jump to numberth message
emacs - Email - rmail - Reading
===============================
. = top of message
spc = fwd one screen
bksp = bwd one screen
emacs - Email - rmail - Deleting
================================
d = flag this message for deletion
u = unflag it
x = delete all flagged messages
ctrl-o,filename = save this message to filename (ASCII text)
emacs - Email - sendmail
========================
ctrl-x,m = new message
ctrl-x,4,m = ...in another window
ctrl-c,ctrl-w = paste the .signature file here
ctrl-c,ctrl-c = send this message
ctrl-x,ctrl-k = cancel it
ctrl-x,ctrl-c = ...and quit
emacs - C mode
==============
ctrl-opt-q = indent expression intelligently (just use tab, it's easier)
ctrl-c,ctrl-e = expand cpp macros (might be handy for debugging)
opt-`,c,u = seek comments which eat the region and remove the region from them
Programming - gdb
=================
gdb executable = start gdb and load executable
l = list the 10 lines around main(){
l = list the next 10 lines
b linenum = (break) stop execution at linenum
r = (run) start execution
s = (step) move forward one line, even into functions*
n = (next) step, but not in functions
l = list 4.5 lines on either side of current line
nostop = don't pass signals onto the program (don't stop it)
pass = pass the signal on anyway, but nonetheless, don't kill the program
*only your functions, not library ones
Programming - Compiling
=======================
gcc files.c -o executable -g = compile a test C version for gdb
gcc files.c -o executable -O2 = compile a final C version fully optimised*
gcc -c file.c = compile file.c but don't link it into an executable; useful for
make**
g++ files.c -o executable -g = compile a test C++ version for gdb
g++ files.c -o executable -O2 = compile a final C++ version fully optimised*
g++ -c file.C = compile file.C but don't link it into an executable; useful for
make**
strip -s objfile = remove symbols to free up disk (bad for debuggers)
*note that optimisation tends to alter the flow of the program and makes it
very hard to debug, and code compiled with debugging and optimisation can
even run a THIRD way.
**see Programming - make
Programming - make
==================
make = compile a program by the Makefile in .
make target = look for Makefiles containing the target and make that only
make -f makefile = use specified makefile
make -n target = fake make; echoes commands to stdout instead of executing them
make -j target = multiple jobs simultaneously
Programming - make - Makefiles
==============================
#text = comment
target: dependencies = rule: target requires the files dependencies
command = executes the command after the tab character if the above rule is met
Programming - make - Makefiles - Macros
=======================================
VPATH = dir1:dir2... = other dirs to seek files
CC = compiler = redefine default compiler
name = value = custom macro definition
$(name) = macro instance, e.g. $(foo) -> bar
$@ = name of target
$* = $@ sans suffix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment