-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunix_cheat_sheet.tex
More file actions
353 lines (276 loc) · 13.3 KB
/
unix_cheat_sheet.tex
File metadata and controls
353 lines (276 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage{fancyhdr}
\usepackage{url}
\usepackage{color}
\usepackage{textcomp}
\usepackage{geometry}
\usepackage{courier}
\geometry{
top=1.0in, % <-- you want to adjust this
inner=0.5in,
outer=0.5in,
bottom=1.0in,
headheight=4ex, % <-- and this
headsep=3ex, % <-- and this
}
\renewcommand{\familydefault}{\sfdefault}
\addtolength{\parskip}{\baselineskip}
\pdfpagewidth 8.5in
\pdfpageheight 11in
\pagestyle{fancy}
\newcommand{\urlwofont}[1]{\urlstyle{same}\url{#1}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\lhead{\leftmark}
\chead{}
\rhead{\rightmark}
\lfoot{}
\cfoot{Page \thepage}
\rfoot{}
\hypersetup{
colorlinks=false,
linkcolor=blue,
citecolor=black,
filecolor=black,
urlcolor=blue
}
\begin{document}
\section{Useful Unix Utilities}
\begin{enumerate}
\item{\texttt{man}}
\subitem{Displays manuals (aka \texttt{man} pages). Press \texttt{Q} to exit a \texttt{man} page.}
\subitem{Type \texttt{/<text>} to search for text. Press \texttt{N} to go to next match, \texttt{shift+N} to go to previous match.}
\subitem{Usage: \texttt{man ls};\texttt{ man cp};\texttt{ man man}}
\item{\texttt{pwd}}
\subitem{Print current directory}
\subitem{Usage: \texttt{pwd}}
\item{\texttt{clear}}
\subitem{Clears the screen (sort of).}
\subitem{In a Mac, you can also press \texttt{Command+K} at any time to clear the screen, but this differs from \texttt{clear} in that it actually clears the screen.}
\subitem{Usage: \texttt{clear}}
\item{\texttt{echo}}
\subitem{Print a line to the console.}
\subitem{Usage: \texttt{echo `hello, world'}}
\item{\texttt{ls}}
\subitem{List files in a directory (current directory by default)}
\subitem{Useful Options: \texttt{-r} (recursive), \texttt{-a} (hidden files), \texttt{-l} (verbose)}
\subitem{Usage: \texttt{ls};\texttt{ ls -al};\texttt{ ls -al \urlwofont{~/}};\texttt{ ls myfolder}}
\item{\texttt{cd}}
\subitem{Change directory}
\subitem{Usage: \texttt{cd /};\texttt{ cd ..};\texttt{ cd \urlwofont{~/}};\texttt{ cd myfolder}}
\item{\texttt{cp}}
\subitem{Copy file (or directory), overwrites destination if it exists}
\subitem{Useful Options: \texttt{-R}}
\subitem{Usage: \texttt{cp a.txt b.txt};\texttt{ cp -R folder otherfolder}}
\item{\texttt{mv}}
\subitem{Move files}
\subitem{Warning: overwrites destination if it exists}
\subitem{Usage: \texttt{mv a.txt b.txt};\texttt{ mv a.txt folder/b.txt};\texttt{ mv a.txt folder}}
\item{\texttt{rm}}
\subitem{Delete files}
\subitem{Useful options: \texttt{-r} (recursive)}
\subitem{Usage: \texttt{rm a.txt};\texttt{ rm -r folder}}
\subitem{Trivia: Never run \texttt{rm -rf /}. It will delete everything.}
\item{\texttt{touch}}
\subitem{Make an empty file. If file exists, updates last modified date.}
\subitem{Useful to trigger watchers like guard.}
\subitem{Useful options: \texttt{-r} (recursive)}
\subitem{Usage: \texttt{touch newfile.txt}; \texttt{touch -r *}}
\item{\texttt{mkdir}}
\subitem{Make an empty directory}
\subitem{Usage: \texttt{mkdir folder}}
\item{\texttt{rmdir}}
\subitem{Delete a directory. It has to be empty}
\subitem{Usage: \texttt{rmdir folder}}
\item{\texttt{grep}}
\subitem{Find all lines in file matching some regular expression.}
\subitem{Useful options: \texttt{-I} (ignore binary files like images), \texttt{-i} (ignore case), \texttt{-r} (recursive), \texttt{-v} (invert match)}
\subitem{Usage: \texttt{grep pattern *};\texttt{ grep -iIr nginx puppet/*}}
\item{\texttt{find}}
\subitem{Find all files with some property.}
\subitem{Useful options: \texttt{-name}}
\subitem{Usage: \texttt{find . -name "*s.coffee"}}
\item{\texttt{curl}}
\subitem{Make HTTP requests from console.}
\subitem{Useful options: \texttt{-d <data>} for form data, \texttt{-X <method>} to specify HTTP method}
\subitem{Usage:}
\subsubitem{\texttt{curl www.google.com}}
\subsubitem{\texttt{curl localhost:8000}}
\subsubitem{\texttt{curl localhost:8000/api/messages -d "message=hello"}}
\subsubitem{\texttt{curl localhost:8000/api/messages/1 -d "message=world" -X "PUT"}}
\item{\texttt{ping}}
\subitem{Sends data to a remote server. The server will send data back to you if it is up.}
\subitem{Usage: \texttt{ping www.google.com}; \texttt{ping localhost}}
\item{\texttt{ps}}
\subitem{A way to list all processes and their respective process IDs}
\subitem{Useful options: Use it with \texttt{-ef}}
\subitem{Note: Use \texttt{grep} to filter/search for a particular process name.}
\subitem{Usage: \texttt{ps -ef | grep InternetExploder}}
\item{\texttt{kill}}
\subitem{Terminates a process. Expects a process ID.}
\subitem{Useful options: \texttt{-9} to absolutely terminate a process. The process cannot stop it.}
\subitem{Note: Use \texttt{ps} to see the process IDs.}
\subitem{Usage: \texttt{kill 1234}; \texttt{kill -9 12345}}
\item{\texttt{chmod}}
\subitem{Changes file permissions.}
\subitem{Use the command as follows: \texttt{chmod <A><B><C>}, where \texttt{<A>}, \texttt{<B>}, and \texttt{<C>} are explained below.}
\subitem{There are other ways to use the command (see \texttt{man chmod}), but these ones are useful for most purposes.}
\subitem{Values of \texttt{<A>}: \texttt{u} (User), \texttt{g} (Group), \texttt{o} (Others)}
\subitem{Values of \texttt{<B>}: \texttt{+} (Add) \texttt{-} (Remove)}
\subitem{Values of \texttt{<C>}: \texttt{r} (Read permissions) \texttt{w} (Write permissions) \texttt{x} (Executable permissions)}
\subitem{Usage:}
\subsubitem{\texttt{chmod u+x myscript.sh}}
\subsubitem{\texttt{chmod o-w myfile.txt}}
\item{\texttt{chown}}
\subitem{Change file ownership.}
\subitem{Use the command as follows: \texttt{chown <owner>:<group> <file>}, \texttt{chown <owner> <file>}, or \texttt{chown :<group> <file>}.}
\subitem{Useful options: \texttt{-R} (recursive)}
\subitem{Usage: \texttt{chown alvin:up-mit-gsl myfile.txt}}
\item{\texttt{sudo}}
\subitem{Do action as another user (\texttt{root} by default)}
\subitem{Useful options: \texttt{-u <username>}}
\subitem{Usage: \texttt{sudo touch a.txt};\texttt{ sudo rm a.txt}}
\item{\texttt{nano}}
\subitem{Simple console-based text editor}
\subitem{Usage: \texttt{nano a.txt}}
\item{\texttt{tail}}
\subitem{Display last few lines of a file. Useful for watching log files}
\subitem{Useful options: \texttt{-f} (wait for more data)}
\subitem{Usage: \texttt{tail a.txt};\texttt{ tail -f /var/log/upstart/guard.log}}
\item{\texttt{head}}
\subitem{Display top few lines of a file. Not as useful as \texttt{tail}.}
\item{\texttt{cat}}
\subitem{Print out contents of a file to the console}
\subitem{Useful options: \texttt{-n} (enumerate lines), \texttt{-s} (compress blank lines)}
\subitem{Usage: \texttt{cat a.txt};\texttt{ cat -sn a.txt}}
\item{\texttt{less}}
\subitem{View the contents of a file. Press \texttt{Q} to exit.}
\subitem{Commands similar to \texttt{man}.}
\subitem{Usage: \texttt{less a.txt};\texttt{ cat -n a.txt | less}}
\item{\texttt{diff}}
\subitem{Compare two files}
\subitem{Useful options: \texttt{-i} (ignore case), \texttt{-b} (ignore blank lines)}
\subitem{Usage: \texttt{diff a.txt b.txt};\texttt{ diff \{a,b\}.txt}}
\item{\texttt{wc}}
\subitem{Word count tool; useful for counting lines of code and things.}
\subitem{Useful options: \texttt{-c} (characters), \texttt{-w} (words), \texttt{-l} (lines)}
\subitem{Usage: \texttt{wc -l a.txt}}
\item{\texttt{ssh}}
\subitem{Log into a remote computer or virtual machine}
\subitem{Usage: \texttt{ssh username@hostname};\texttt{ ssh payton@kalibrr.com}}
\item{\texttt{scp}}
\subitem{It's like \texttt{cp}, but between multiple computers.}
\subitem{Usage: \texttt{scp a.txt payton@kalibrr.com:/home/payton/a.txt}}
\item{\texttt{apt-get} (Debian-based distros only e.g. Debian, Ubuntu, Mint)}
\subitem{Package manager for installing and removing things.}
\subitem{Useful commands:}
\subsubitem{\texttt{install} - Installs a package}
\subsubitem{\texttt{remove} - Removes a package}
\subsubitem{\texttt{purge} - Removes a package and clears configuration}
\subsubitem{\texttt{update} - Updates the cached package listing}
\subitem{Usage:}
\subsubitem{\texttt{sudo apt-get install vim}}
\subsubitem{\texttt{sudo apt-get purge vim}}
\subsubitem{\texttt{sudo apt-get update}}
\item{\texttt{apt-cache} (Debian-based distros only e.g. Debian, Ubuntu, Mint)}
\subitem{Tool for searching for packages}
\subitem{Useful commands:}
\subsubitem{\texttt{search} - search for package with substring}
\subsubitem{\texttt{show} - show detailed information about a package}
\subitem{Usage: \texttt{apt-cache search vi | grep vim};\texttt{ apt-cache show vim}}
\item{\texttt{initctl} (Ubuntu only)}
\subitem{Tool for managing services}
\subitem{Useful commands:}
\subsubitem{\texttt{start} - starts a service}
\subsubitem{\texttt{stop} - stops a service}
\subitem{Usage: \texttt{sudo initctl start guard};\texttt{ sudo initctl stop nginx}}
\item{\texttt{service} (Ubuntu only)}
\subitem{Another tool for managing services}
\subitem{Usage: \texttt{sudo service guard start};\texttt{ sudo service nginx stop}}
\item{\texttt{open} (Mac only)}
\subitem{Opens a file with default program}
\subitem{Usage: \texttt{open a.png} (opens with Preview)}
\end{enumerate}
\newpage
\section{Unix Command Line 101}
\subsection{Directory Naming}
If it starts with \texttt{/}, it's an absolute path that starts from the root directory ( \texttt{/} ).
If it starts with \texttt{\urlwofont{~/}}, it's an absolute path that starts from the user's home directory (\texttt{/Users/username/} on Mac; \texttt{/home/username/} on Linux).
Everything else is a path relative to the current directory (see \texttt{pwd}).
`\texttt{..}' means one folder up.
`\texttt{.}' means current folder.
\subsubsection{Examples}
\begin{enumerate}
\item \texttt{cd ..} brings you one folder up.
\item \texttt{cd example/..} is the same as \texttt{cd .}
\end{enumerate}
\subsection{Pipes and Redirection}
\begin{enumerate}
\item{\texttt{|}}
\subitem{Pass the output from a command to another command}
\subitem{Usage:}
\subsubitem{\texttt{ls -R | grep ".*\textbackslash.txt"} (Find all files that end with \texttt{.txt})}
\subsubitem{\texttt{cat a.txt | grep -v "\textasciicircum\$" | less} (Read file without the empty lines)}
\subitem{This is one of the more powerful features of bash. Be sure to use it.}
\item{\texttt{<}}
\subitem{Use a file as input.}
\subitem{Example: \texttt{java Hello < input.txt} (Run a Java program entitled Hello with \texttt{input.txt} as input.)}
\item{\texttt{>}}
\subitem{Save output to a file. Overwrites current file.}
\subitem{Example: \texttt{java Hello < input.txt > output.txt}}
\item{\texttt{>>}}
\subitem{Append output to a file.}
\subitem{Example: \texttt{java Hello < input.txt >> output.txt }}
\item{\texttt{2>}}
\subitem{Save error output to a file. Overwrites current file.}
\item{\texttt{2>>}}
\subitem{Append error output to a file.}
\end{enumerate}
\subsection{Globbing}
You can pass a ``glob'' to many commands. A glob is a pattern that's much simpler than regular expressions but is useful nonetheless.
\texttt{*} matches any number of characters including the empty string
\texttt{?} matches exactly one character
\subsubsection{Examples}
\begin{enumerate}
\item \texttt{rm -r *.coffee} (remove all files that end in \texttt{.coffee})
\item \texttt{rm -r ???} (remove files and directories whose names are three characters long)
\end{enumerate}
\subsection{Curly Braces}
Using curly braces (\texttt{\{\}}) in a command causes it to expand the command.
\subsubsection{Examples}
\begin{enumerate}
\item \texttt{diff \{a,b\}.txt} is equivalent to \texttt{diff a.txt b.txt}
\item \texttt{touch \{a,b,c,d\}.\{c,py,java\}} - \texttt{touch}es 12 files
\item \texttt{diff file\{1,2\}.txt} is equivalent to \texttt{diff file1.txt file2.txt}
\end{enumerate}
\newpage
\section{Advanced Stuff}
\subsection{More Unix Commands}
\begin{enumerate}
\item{\texttt{vim}}
\subitem{Advanced console-based text editor}
\subitem{Learn on your own. It's very complicated.}
\item{\texttt{emacs}}
\subitem{Even more complicated and powerful text editor.}
\subitem{We heard it can even play music.}
\item{\texttt{tar}}
\subitem{Super duper unzipper. Bahala na kayo sa options.}
\item{\texttt{make}}
\subitem{Recompiles parts of programs that changed.}
\subitem{Also complicated. Learn on your own.}
\end{enumerate}
\subsection{More on Bash}
Variables:
To set variables:
\texttt{VARIABLE="hello world"} (No spaces)
To use variables:
\texttt{echo \$VARIABLE}
Note: Variables are case-sensitive.
If you want to initialize some stuff before starting the bash prompt, you can place the codes in \\ \texttt{\urlwofont{~/}.bash\char`_profile}
For more advanced bash scripting, check the Internet.
\end{document}