Ricky : In this video you’re going to
learn how to redirect text output in the
Linux command line. We’ll go over some
basic operators, the cat and tee commands,
which will let you write, append, and pipe
text around. We’ll also take a look at
Here Documents for input-output
redirection. Once you start learning other
commands, text redirection will make a lot
more sense, so mastering it can make you
really good at writing bash scripts, which
is super important for tooling and
automation in cyber security. Let’s go.
One key principle to understand about
Linux is that it’s text-oriented, meaning
many of the standard tools are focused on
sending input to a command, processing it,
and outputting it somewhere, whether to
the screen or a file. You should also know
that everything in Linux is a file, which
are all identified by a file descriptor,
which are non-negative numbers that
reference an open file. Believe it or not,
the input, output, and error messages that
get displayed in a terminal window are
actually special files. They’re located in
/dev/stdin, stdout, and stderr. These
files are represented by file descriptors
0, 1, and 2. In the command line, you can
access these file descriptors through the
greater or less than arrows, with a
respective number for each. So for
example, if I wanted to run ls on my home
folder and redirect the output to a file,
all I’d need to do is use 1 and the
greater than arrow to send it there. This
would be the exact same as having only the
arrow, which is just shorthand and can be
used interchangeably. To print the
contents of this file, I can use the cat,
or concatenate command, which will read
the file as standard input. This is the
same as using the stdin file descriptor,
which is 0 followed by the less than
arrow, which passes the file to the cat
tool. Using just the less than arrow is
shorthand and will do the same thing. Just
to show you the concept that we’re passing
streams of text around via files, I’m
going to run the previous command, passing
our text file as input into cat, but now
redirecting it to the actual stdout file.
You’d never actually run this command
since It does exactly the same thing but
even more explicitly. Now understand that
the standard out arrow is destructive, and
will overwrite anything passed to it. If I
run ls on the snap folder but write the
output to home.txt, I’ll lose everything
in that file. To append output instead of
overwriting, all you need to do is use the
double arrow. In this case, you’ll see
that we’ve preserved the original text. If
you ever need to truncate and zero out a
file, you can simply use the right arrow
followed by the file to do so. As you can
see here, boom, it's blown away. If you
want to redirect output of a command to
both a file and show its output, we need
to use the tee command. To do this we need
to use a pipe symbol, which passes the
output of one command to be the input of
another. Tee here will write any input
given and print it to standard output as
well as the file. Using it with the -a
flag will make it append and not overwrite
If, for some reason, we want to run a
files.
script or tool and suppress any output
from flooding the screen, /dev/null is a
file that throws out anything written to
it, kind of like a black hole or
bitbucket. For example, here I have a
script that, when run, will create ten
numbered folders in the tmp directory and
print out whatever folder it's creating.
If I redirect the output to /dev/null,
it’ll execute the commands but suppress
any output. You’ll notice here that
I’m getting error messages because
I’ve already created the folders. You can
Also suppress this by passing the
standard error file descriptor, which is 2
right arrow, to /dev/null. If you want to
save the error messages, you can also send
it to a normal file.
If you’re running some command and want
detailed logging of both output and error,
you can first redirect the output stream
to a file, and then the error stream into
ampersand 1, which is the file descriptor
for standard out. It’s kind of a weird
syntax, but you may eventually use it one
day for capturing error messages. Now
let’s take a look at the cat command in
more detail, since there’s a lot of useful
flags that can modify its behavior. Here
I’ve got a file containing hashes and
their plaintext passwords, with some
newlines and tabs thrown in. To show the
line number count in the file, I can use
the -n for numbering flag. To remove any
repeated empty lines, I can throw in the
-s for squeeze flag. To see any tab
characters that are in the file, I can use
the -t for tab flag, which will indicate
tabs with caret capital I. To see where
the ends of lines are occurring, I can use
the -e for end flag, which will indicate
it with a dollar sign. If you’ve got a
file or script created on a Windows
system, it’s going to contain some extra
invisible characters for newlines, often
known as CRLF, or carriage-return line
feed. Sometimes this won’t display or run
properly on Linux. This is incredibly
frustrating when you’re trying to
calculate the hash of two files that look
the same or run a script that you’ve
copy-pasted between systems. For example I
have another password file made in
Notepad, which looks normal running the
usual cat command. Adding the -v flag show
the extra Windows non-printing
characters with the caret M symbol. If you want
to display all non-printable and
whitespace characters, use the -A for all flag,
which is the same as using -vet.
Finally, we can also use cat to
concatenate both these files together into one
target file using the standard out
redirection symbol from earlier. I’m going to
throw in the line numbering and show all
whitespaces in this combined file. To
bring it full circle, I can pipe the cat
command’s output into tee to write to the file
and print the results, all in one line.
Now here’s a bonus tip for you in using
cat and redirecting output for
multiple lines of text at a time. In Linux
there’s a feature called Here Documents, that
lets you pass around a literal snippets
of text, preserving all the symbols
and whitespaces. We’re going to use
cat to write some lines from a poem I use
to know, to a file, which I’ll name poe.txt
We’ll use the double left arrows to
append standard input, followed by EOF, then
hit enter. Now we can type any text we
want to here and it'll preserve it
We’ll end it again with EOF. So what EOF
stands for is end-of-file, which acts as a
delimiter and can be anything, really, but
EOF is the most common one I’ve seen
people use since it likely won’t appear in
whatever you’re typing. Anything in
between the EOF delimiters will get
written to the file poe.txt. If I take a
look at poe.txt, to see what it contains,
we see it contains exactly what we typed
including all the new lines and spaces.
You can also use here documents with
commands other than cat to pass snippets
of text to it, which is useful in a
non-interactive script you’re writing, for
example. As you become more familiar with
Linux commands and even custom tools,
being able to pipe and redirect the input
and output between them is going to be
essential. This might be slicing and
dicing log files for review during an
incident response, or chaining together
several tools on a penetration test. So
that is it for redirecting text input and
output on the Linux command line. Thanks
so much for watching, and I’ll see you
soon!