Files, Redirection & find

Redirecting output and errors, here-documents, find, file tests and path manipulation.

Every process has three standard streams: input (0), output (1) and errors (2). Redirection connects them to files; find walks directory trees; small tools like dirname and basename handle paths.

Working with files

> overwrites a file, >> appends, 2> redirects only error messages.

cat > file <<EOF ... EOF writes a multi-line here-document.

find build -name '*.log' searches by name; -type f counts regular files only.

-s tests 'exists and is not empty'; -x 'is executable'; chmod +x makes it so.

dirname and basename split a path; basename can also strip an extension.

Useful patterns

cmd 2>&1 > out.log cmd > out.log 2>&1 Redirections apply left to right: in the wrong order, errors are pointed at the terminal (where stdout was) before stdout moves to the file.

How do you append both output and errors of a command to build.log?

cmd >> build.log 2>&1 (or cmd &>> build.log in Bash).