I've started learning bash scripting by using this guide:
However I got stuck at the first script:
cd /var/log
cat /dev/null > messages
cat /dev/null > wtmp
echo "Log files cleaned up."
What do lines 2 and 3 do in Ubuntu (I understand cat)? Is it only for other Linux distributions? After running this script as root, the output I get is Log files cleaned up. But /var/log still contains all the files.
5 Answers
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.
filemay also be a device node.- If
filedoesn't exist, it is created as a regular file. - If
filealready 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 ofcat /dev/nulltomessagesandwtmp. - If
filealready exists as a symbolic link, the link's target is used. - If
filealready exists as a directory, you'll get an error likebash: 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.)