Assuming the commands succeeded, /var/log/messages and /var/log/wtmp still exist but are now blank.
Shell Redirection
> is a redirection operator, implemented by the shell. Its syntax is:
command > file
This redirects command's standard output to file.
file may also be a device node.
- If
file doesn't exist, it is created as a regular file.
- If
file already exists as a regular file and is non-empty, it is overwritten. This is typically the case in the commands you ran, where you redirected the output of cat /dev/null to messages and wtmp.
- If
file already exists as a symbolic link, the link's target is used.
- If
file already exists as a directory, you'll get an error like bash: file: Is a directory.
(Of course, these operations may fail for other reasons, such as lack of permissions or a filesystem error.)
The >> redirection operator is similar, but it appends to the end of non-empty regular files instead of overwriting their contents. (Another redirection operator is <. command < file uses file as command's standard input.)
The null Device
/dev/null is a simple device (implemented in software and not corresponding to any hardware device on the system).
/dev/null looks empty when you read from it.
- Writing to
/dev/null does nothing: data written to this device simply "disappear."
Often a command's standard output is silenced by redirecting it to /dev/null, and this is perhaps the null device's commonest use in shell scripting:
command > /dev/null
You're using /dev/null differently. cat /dev/null outputs the "contents" of /dev/null, which is to say its output is blank. > messages (or > wtmp) causes this blank output to be redirected to the file on the right side of the > operator.
Since messages and wtmp are regular files (rather than, for example, device nodes), they are turned into blank files (i.e., emptied).
You could use any command that does nothing and produces no output, to the left of >.
An alternative way to clear these files would be to run:
echo -n > messages
echo -n > wtmp
The -n flag is required, or echo writes a newline character.
(This always works in bash. And I believe the default sh in every GNU/Linux distribution and other Unix-like system popularly used today supports the -n flag in its echo builtin. But jlliagre is right that echo -n should be avoided for a truly portable shell script, as it's not required to work. Maybe that's why the guide you're using teaches the cat /dev/null way instead.)
The echo -n way is equivalent in its effects but arguably is a better solution, in that it's simpler.
cat /dev/null > file opens three "files":
- The
cat executable (usually /bin/cat), a regular file.
- The
/dev/null device.
file
In contrast, echo -n > file opens only file (echo is a shell builtin).
Although this should be expected to improve performance, that's not the benefit--not when just running a couple of these commands by hand, anyway. Instead, the benefit is that it's easier to understand what's going on.
Redirection and the trivial (blank/empty) command.
As jlliagre has pointed out (see also jlliagre's answer), this can be shortened further by simply omitting the command on the left of > altogether. While you cannot omit the right side of a > or >> expression, the blank command is valid (it's the command you're running when you just press Enter on an empty prompt), and in omitting the left side you're just redirecting the output of that command.
- Note that this output does not contain a newline. When you press Enter on a command prompt--whether or not you've typed anything--the shell (running interactively) prints a newline before running the command issued. This newline is not part of the command's output.
Redirecting from the blank command (instead of from cat /dev/null or echo -n) looks like:
> messages
> wtmp