-
Notifications
You must be signed in to change notification settings - Fork 50
/
bash-redirections-cheat-sheet.tex
executable file
·152 lines (132 loc) · 6.67 KB
/
bash-redirections-cheat-sheet.tex
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
% Bash Redirections Cheat Sheet
%
% by Peteris Krumins ([email protected])
% http://www.catonmat.net - good coders code, great coders reuse
%
% 2012.09.09
%
\documentclass[9pt]{memoir}
\usepackage[left=1cm,top=0.5cm,right=1cm,bottom=0.5cm,nohead,nofoot]{geometry}
\usepackage[pdftex]{hyperref}
\usepackage{verbatim}
\usepackage{array}
\usepackage{upquote}
\hypersetup{pdftitle={Bash Redirections Cheat Sheet}}
\hypersetup{pdfauthor={Peteris Krumins ([email protected])}}
\hypersetup{pdfkeywords={cheat sheet, bash, shell, bourne again shell, sh, redirection, redirections, command line, unix, linux, cheat sheat, cheet sheet, cheet sheat}}
\hypersetup{pdfsubject={http://www.catonmat.net - good coders code, great coders reuse}}
\hypersetup{colorlinks}
\pagestyle{empty}
% -----------------------------------------------------------------------
\begin{document}
\begin{center}
\huge \textbf{Bash Redirections Cheat Sheet}
\end{center}
\vspace{0.1in}
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{|m{5.5cm}|m{12.2cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|cmd > file| & Redirect the standard output (stdout) of \verb|cmd| to a file. \\
\hline
\verb|cmd 1> file| & Same as \verb|cmd > file|. \verb|1| is the default file descriptor (fd) for stdout. \\
\hline
\verb|cmd 2> file| & Redirect the standard error (stderr) of \verb|cmd| to a file. \verb|2| is the default fd for stderr. \\
\hline
\verb|cmd >> file| & Append stdout of \verb|cmd| to a file. \\
\hline
\verb|cmd 2>> file| & Append stderr of \verb|cmd| to a file. \\
\hline
\verb|cmd &> file| & Redirect stdout and stderr of \verb|cmd| to a file. \\
\hline
\verb|cmd > file 2>&1| & Another way to redirect both stdout and stderr of \verb|cmd| to a file. This \underline{is not} the same as \verb|cmd 2>&1 > file|. \underline{Redirection order matters!} \\
\hline
\verb|cmd > /dev/null| & Discard stdout of \verb|cmd|. \\
\hline
\verb|cmd 2> /dev/null| & Discard stderr of \verb|cmd|. \\
\hline
\verb|cmd &> /dev/null| & Discard stdout and stderr of \verb|cmd|. \\
\hline
\verb|cmd < file| & Redirect the contents of the file to the standard input (stdin) of \verb|cmd|. \\
\hline
\small{
\verb|cmd << EOL| \par
\verb|line1| \par
\verb|line2| \par
\verb|EOL|
} & Redirect a bunch of lines to the stdin. If \verb|'EOL'| is quoted, text is treated literally. This is called a here-document. \\
\hline
\small{
\verb|cmd <<- EOL| \par
\verb|<tab>foo| \par
\verb|<tab><tab>bar| \par
\verb|EOL|
} & Redirect a bunch of lines to the stdin and strip the leading tabs. \\
\hline
\verb|cmd <<< "string"| & Redirect a single line of text to the stdin of \verb|cmd|. This is called a here-string. \\
\hline
\verb|exec 2> file| & Redirect stderr of all commands to a file forever. \\
\hline
\verb|exec 3< file| & Open a file for reading using a custom file descriptor. \\
\hline
\verb|exec 3> file| & Open a file for writing using a custom file descriptor. \\
\hline
\verb|exec 3<> file| & Open a file for reading and writing using a custom file descriptor. \\
\hline
\verb|exec 3>&-| & Close a file descriptor. \\
\hline
\verb|exec 4>&3| & Make file descriptor \verb|4| to be a copy of file descriptor \verb|3|. (Copy fd \verb|3| to \verb|4|.) \\
\hline
\verb|exec 4>&3-| & Copy file descriptor \verb|3| to \verb|4| and close file descriptor \verb|3|. \\
\hline
\verb|echo "foo" >&3| & Write to a custom file descriptor. \\
\hline
\verb|cat <&3| & Read from a custom file descriptor. \\
\hline
\verb|(cmd1; cmd2) > file| & Redirect stdout from multiple commands to a file (using a sub-shell). \\
\hline
\verb|{ cmd1; cmd2; } > file| & Redirect stdout from multiple commands to a file (faster; not using a sub-shell). \\
\hline
\verb|exec 3<> /dev/tcp/host/port| & Open a TCP connection to \verb|host:port|. (This is a bash feature, not Linux feature). \\
\hline
\verb|exec 3<> /dev/udp/host/port| & Open a UDP connection to \verb|host:port|. (This is a bash feature, not Linux feature). \\
\hline
\verb|cmd <(cmd1)| & Redirect stdout of \verb|cmd1| to an anonymous fifo, then pass the fifo to \verb|cmd| as an argument. Useful when \verb|cmd| doesn't read from stdin directly. \\
\hline
\verb|cmd < <(cmd1)| & Redirect stdout of \verb|cmd1| to an anonymous fifo, then redirect the fifo to stdin of \verb|cmd|. Best example: \verb\diff <(find /path1 | sort) <(find /path2 | sort)\. \\
\hline
\verb|cmd <(cmd1) <(cmd2) | & Redirect stdout of \verb|cmd1| and \verb|cmd2| to two anonymous fifos, then pass both fifos as arguments to \verb|cmd|. \\
\hline
\verb/cmd1 >(cmd2)/ & Run \verb|cmd2| with its stdin connected to an anonymous fifo, and pass the filename of the pipe as an argument to \verb|cmd1|. \\
\hline
\verb/cmd1 > >(cmd2)/ & Run \verb|cmd2| with its stdin connected to an anonymous fifo, then redirect stdout of \verb|cmd| to this anonymous pipe. \\
\hline
\verb/cmd1 | cmd2/ & Redirect stdout of \verb|cmd1| to stdin of \verb|cmd2|. Pro-tip: This is the same as \verb|cmd1 > >(cmd2)|, same as \verb|cmd2 < <(cmd1)|, same as \verb|> >(cmd2) cmd1|, same as \verb|< <(cmd1) cmd2|. \\
\hline
\verb/cmd1 |& cmd2/ & Redirect stdout and stderr of \verb|cmd1| to stdin of \verb|cmd2| (bash 4.0+ only). Use \verb/cmd1 2>&1 | cmd2/ for older bashes. \\
\hline
\verb/cmd | tee file/ & Redirect stdout of \verb|cmd| to a file and print it to screen. \\
\hline
\verb|exec {filew}> file| & Open a file for writing using a named file descriptor called \verb|{filew}| (bash 4.1+). \\
\hline
\verb|cmd 3>&1 1>&2 2>&3| & Swap stdout and stderr of \verb|cmd|. \\
\hline
\verb|cmd > >(cmd1) 2> >(cmd2)| & Send stdout of \verb|cmd| to \verb|cmd1| and stderr of \verb|cmd| to \verb|cmd2|. \\
\hline
\verb/cmd1 | cmd2 | cmd3 | cmd4/ \par
\verb/echo ${PIPESTATUS[@]}/ & Find out the exit codes of all piped commands. \\
\hline
\end{tabular}
\vfill
I explained each one of these redirections in my article \href{http://www.catonmat.net/blog/bash-one-liners-explained-part-three/}{All About Bash Redirections}: \par
\href{http://www.catonmat.net/blog/bash-one-liners-explained-part-three/}{www.catonmat.net/blog/bash-one-liners-explained-part-three/}
\vfill
Did I miss any redirections? Let me know! Email me [email protected], or fork this cheat sheet on github: \par \href{http://github.com/pkrumins/bash-redirections-cheat-sheet}{www.github.com/pkrumins/bash-redirections-cheat-sheet}
\vfill
\framebox{\parbox{5in}{
A cheat sheet by \textbf{Peteris Krumins} ([email protected]), September 2012.
\href{http://www.catonmat.net}{http://www.catonmat.net} - good coders code, great coders reuse
\vspace{2mm}
\footnotesize{Released under GNU Free Document License.}}}
\end{document}