\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{k.mate555} \pdfinfo{ /Title (mac-linux-command-line.pdf) /Creator (Cheatography) /Author (k.mate555) /Subject (Mac / linux command line Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Mac / linux command line Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{k.mate555} via \textcolor{DarkBackground}{\uline{cheatography.com/200161/cs/42310/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}k.mate555 \\ \uline{cheatography.com/k-mate555} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 6th February, 2024.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basics}} \tn % Row 0 \SetRowColor{LightBackground} man \{cmd\} & man page for cmd \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} type \{cmd\} & executable/shell/alias \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} help \{cmd\} & if there is no man page \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} wc & word count (`-l`ines `-w`ords) \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} sort \{file\} & alphabetically (`-r`everse `-n`umeric `-u`nique `-k` column) \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{white} ls | sort -nk4 & sort by file size \tn % Row Count 9 (+ 1) % Row 6 \SetRowColor{LightBackground} mkdir {[}-p{]} & also make `-p`arents if not exists \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} rm {[}-r -d -i{]} & (`-r`ecursive `-d` empty directories `-i` prompt before every removal) \tn % Row Count 15 (+ 4) % Row 8 \SetRowColor{LightBackground} mv \{source ...\} \{destination\} & move file(s) or directories or rename (if single file or folder is specified as source \& destination) \tn % Row Count 20 (+ 5) % Row 9 \SetRowColor{white} cp \{source ...\} destination & copy file(s) \tn % Row Count 22 (+ 2) % Row 10 \SetRowColor{LightBackground} history | less & open history in pager \tn % Row Count 23 (+ 1) % Row 11 \SetRowColor{white} history | grep \{term\} & search for term in history \tn % Row Count 25 (+ 2) % Row 12 \SetRowColor{LightBackground} cat \{file1\} \{file2\} & con{\emph{cat}}enate \& print contents of file(s) \tn % Row Count 27 (+ 2) % Row 13 \SetRowColor{white} less \{file\} & page file contents \tn % Row Count 28 (+ 1) % Row 14 \SetRowColor{LightBackground} head -\{n\} \& tail & print firts / last n lines of file \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basics (cont)}} \tn % Row 15 \SetRowColor{LightBackground} tail -f & follow (eg. for logs) \tn % Row Count 1 (+ 1) % Row 16 \SetRowColor{white} tr \{search\} \{replace\} & replace search with replace in stdout (eg. `cat msg | tr s S` to uppercase all `s`) (`-d`elete \{search\} instead of replacing) \tn % Row Count 7 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Grep}} \tn % Row 0 \SetRowColor{LightBackground} man grep | grep "count" -i -C5 & grep in `man` for "count" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} -i & insensitive case \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} -w & word search \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} -r & recursive \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} -c & count number of matches \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} -A \{n\} & end n lines after match \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} -B \{n\} & start n lines before match \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} -C \{n\} & start \& end n lines before \& after match \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} -n & add line number for each match \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} -m \{n\} & limit to n matches \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} -l & show filenames \tn % Row Count 17 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Search for patterns in each file's contents (supports regex!) \newline `grep "\{pattern\}" \{file\}` print each line from `\{file\}` that contains `\{pattern\}` \newline `egrep` (or `grep -E`) - use regex meta-characters (eg. `?` `\{\}` etc.) for their special meaning and not as a normal character} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Shortcuts}} \tn % Row 0 \SetRowColor{LightBackground} ctrl+l & clear screen \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} ctrl+a / ctrl+e & move to beginning/end of line \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} option+← / option+→ & move left/right one word \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} ctrl+t & swap char with one before \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} option+t & swap word with one before \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} ctrl+u & kill from cursor to beginning of line \tn % Row Count 11 (+ 2) % Row 6 \SetRowColor{LightBackground} option+d & kill word forward \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} ctrl+w & kill word backward \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} !! & last command \tn % Row Count 14 (+ 1) % Row 9 \SetRowColor{white} !-\{n\} & current command minus n \tn % Row Count 15 (+ 1) % Row 10 \SetRowColor{LightBackground} ctrl+r & incremental history search \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Some options on some OSes / terminals need to be set with bindkey, eg. `bindkey "\textasciicircum{}{[}b" backward-word`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Streaming / redirection / piping}} \tn % Row 0 \SetRowColor{LightBackground} \textgreater{} & redirect output to specific file (overwrite!) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \textgreater{}\textgreater{} & append new data to specific file \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \textless{} & pass contents of file to stdin \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} 2\textgreater{} & redirect stderr to specific file \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} 2\textgreater{}\textgreater{} & append stderr to file \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} 2\textgreater{}\&1 & redirect both sderr and stdout to same file \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} command1 | command2 & pipe (redirect) stdout of 1 to stdin of 2 \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} tee & read stdin and copy both to stdout and a file. \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} cat a.txt b.txt | tee both.txt | wc -l & see above \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{If redirecting both stdout and stderr, out has to be first! Eg. cat a.txt b.txt \textgreater{} both.txt 2\textgreater{} error.txt} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{less}} \tn % Row 0 \SetRowColor{LightBackground} space or f & next page \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} b & previous page \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} enter or down arrow & scroll one line \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} / \{pattern\} & search for pattern \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} q & quit \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Pager to read manuals, docs, etc.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Permissions}} \tn % Row 0 \SetRowColor{LightBackground} r & file can be read, dir contents listed \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} w & file can be modified, dir contents modified \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} x & file can be treated as a program to be executed, dir can be `cd`d into \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} - & file/dir can't be read/modified/executed (depending on where the dash is) \tn % Row Count 11 (+ 4) % Row 4 \SetRowColor{LightBackground} chmod \{permission\} \{file\} & change permissions for given file \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} chmod g+w & add write permissions for group \tn % Row Count 15 (+ 2) % Row 6 \SetRowColor{LightBackground} chmod a-w & remove write permissions for all \tn % Row Count 17 (+ 2) % Row 7 \SetRowColor{white} chmod a=r & set permissions to read {\bf{ONLY}} for all \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} chmod 020 & -{}-{}- -w- -{}-{}- \tn % Row Count 20 (+ 1) % Row 9 \SetRowColor{white} chmod 700 & rwx -{}-{}- -{}-{}- \tn % Row Count 21 (+ 1) % Row 10 \SetRowColor{LightBackground} chmod 644 & rw - r-{}- r-{}- \tn % Row Count 22 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Modes: {\bf{u}}ser, {\bf{g}}roup, {\bf{o}}thers, {\bf{a}}ll \newline What: {\bf{-}}: remove, {\bf{+}}: add, {\bf{=}}: only \newline Which: {\bf{r}}ead, {\bf{w}}rite, {\bf{x}}ecute} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Expansion}} \tn % Row 0 \SetRowColor{LightBackground} \textasciitilde{} & home dir \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \textasciitilde{}\{someuser\} & home dir of someuser \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} * & zero or more characters \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} ? & any single character \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} {[}{]} & range of characters eg. {[}123{]} {[}0-9{]} {[}A-F{]} \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} {[}\textasciicircum{}{]} & negate range eg. {[}\textasciicircum{}0-9{]}* -\textgreater{} not starts with a digit \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \{\} & generate arbitrary strings, eg. touch page\{1,2,3\}.txt will generate 3 files \tn % Row Count 12 (+ 3) % Row 7 \SetRowColor{white} \{...\} & generate between range eg. \{1..31\} \{a..e\} \{2..10..2\} (start,stop,step) \tn % Row Count 15 (+ 3) % Row 8 \SetRowColor{LightBackground} \{x,y\{1..5\},z\} & will expand to `x y1 y2 y3 y4 y5 z` \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{\$((expression))} & arithmetic expansion (`+`, `-`, `{\emph{`, `/`, `}}*`, `\%`) \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{\$(command)} & command substitution (eg. echo today is \$(date)) \tn % Row Count 21 (+ 2) % Row 11 \SetRowColor{white} "double quotes" & respect spacing \& ignore special characters except \$, \textbackslash{} and ` (backtick) \tn % Row Count 24 (+ 3) % Row 12 \SetRowColor{LightBackground} 'single quotes' & suppress all forms of substitution \tn % Row Count 26 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Before any command runs, some special commands expand to match given files eg.: \newline `ls -l *.txt` will be expanded to contain all files ending with `.txt` \newline `mv app?.css Styles/` -\textgreater{} move {\bf{all}} files matching pattern to Styles directory} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Finding files}} \tn % Row 0 \SetRowColor{LightBackground} locate \{searchterm\} & find all files containing searchterm. (-ignore case `-l` limit to n options `-e` only files that exist now) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} find \{dir/\} & list every file and directory inside dir/ \tn % Row Count 9 (+ 3) % Row 2 \SetRowColor{LightBackground} -type d & only find directories (or `f`iles) \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} -name "\{pattern\}" & find files with given pattern ({\bf{case sensitive!}}) (`iname` for case insensitive) \tn % Row Count 16 (+ 5) % Row 4 \SetRowColor{LightBackground} -size +1G & larger than 1gb (or -50M, 20k -\textgreater{} exactly 20k) \tn % Row Count 19 (+ 3) % Row 5 \SetRowColor{white} -user \{uname\} & files belonging to uname \tn % Row Count 21 (+ 2) % Row 6 \SetRowColor{LightBackground} -empty & empty files/folders \tn % Row Count 22 (+ 1) % Row 7 \SetRowColor{white} {[}acm{]}min \{n\} & {\bf{a}}ccessed / {\bf{c}}hanged / {\bf{m}}odified n minutes ago (+- for greater/less than) \tn % Row Count 27 (+ 5) % Row 8 \SetRowColor{LightBackground} {[}acm{]}time \{n\} & a/c/m n * 24 hours ago \tn % Row Count 29 (+ 2) % Row 9 \SetRowColor{white} `-and` `-or` `-not` & logical operators for more complex needs eg. find -type f -not -name "*.html" \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Finding files (cont)}} \tn % Row 10 \SetRowColor{LightBackground} `-exec command '\{\}' ';'` & execute cmd for each match, eg.: find \textasciitilde{}/Downloads -type f -empty -exec ls -l '\{\}' ';' \tn % Row Count 5 (+ 5) % Row 11 \SetRowColor{white} find -type f -name "*.html -exec cp '\{\} '\{\}\_COPY' ';' & find each matching file \& create a copy with suffix \tn % Row Count 8 (+ 3) % Row 12 \SetRowColor{LightBackground} -ok command '\{\}' ';' & same as above but ask for confirmation each time (`y`/`n`) \tn % Row Count 11 (+ 3) % Row 13 \SetRowColor{white} find -name "*.txt" | xargs ls & equivalent to the -exec ls '\{\}' ';' \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}