Thursday, May 2, 2024
43
rated 0 times [  43] [ 0]  / answers: 1 / hits: 143910  / 1 Year ago, sun, may 7, 2023, 9:03:17

I've started learning bash scripting by using this guide: http://www.tldp.org/LDP/abs/abs-guide.pdf



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.


More From » command-line

 Answers
6

cat will list the contents of a file comming after cat to standard output and the > sends it to the file messages and wtmp where > means to first remove all contents of the file and >> would mean to ADD to the current file. In this case you are using > so the file will end up being empty.



Now for the kicker: /dev/null is a device that sends 'nothing' to the 2 files behind the >.



There is a reason to do it like this: the file is NOT removed from the system. If you would rm it and then do a touch messages the permissions might be wrong and if just after the rm something would want to write to the file it would be gone and error out. Depending on how the software is created it could crash.


[#23531] Monday, May 8, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
istictroubli

Total Points: 306
Total Questions: 96
Total Answers: 114

Location: Sao Tome and Principe
Member since Wed, Jul 13, 2022
2 Years ago
;