7.1.2.1. Emptying files
Sometimes the content of a file doesn't interest you, but you
need the file name as a marker (for instance, you just need the
timestamp of a file, a reminder that the file was there or should
be there some time in the future). Redirecting the output of a null
command is how this is done in the Bourne and Bash shells:
andy:~> cat wishlist > placeholder
andy:~> ls -la placeholder
-rw-rw-r-- 1 andy andy 200 Jun 12 13:34 placeholder
andy:~> > placeholder
andy:~> ls -la placeholder
-rw-rw-r-- 1 andy andy 0 Jun 12 13:35 placeholder
|
The process of reducing an existing file to a file with the same
name that is 0 bytes large is called truncating.
For creating a new empty file, the same effect is obtained with
the touch command. On an existing file,
touch will only update the timestamp. See
the Info pages on touch for more details.
To "almost" empty a file, use the
tail command. Suppose user andy's
wishlist becomes rather long because he always adds stuff at the
end but never deletes the things he actually gets. Now he only
wants to keep the last five items:
andy:~> tail -5 wishlist > newlist
andy:~> cat newlist > wishlist
andy:~> rm newlist
|