Instead of Removing a File, Empty It
Save Space with Bit Bucket Log Files and Mailboxes
Unlinking Open Files Isn't a Good Idea
Save Space with a Link
Limiting File Sizes
Save Space with Tab Characters
Compressing Files to Save Space
Save Space: tar and compress a Directory Tree
How Much Disk Space?
zloop: Run a Command on Compressed Files
Edit Compressed Files with zvi, zex, and zed
Compressing a Directory Tree: Fine-Tuning
Save Space in Executable Files with strip
Don't Use strip Carelessly
Trimming a Directory
Trimming a Huge Directory
Huge Files Might Not Take a Lot of Disk Space
Sometimes you don't want to remove a file completely - you just want to empty it:
When you remove a file and create a new one with the same name, the new file will have your default permissions (22.4) and ownership (22.3). It's better to empty the file now, then add new text later; this won't change the permissions and ownership.
Completely empty files (ones that ls -l says have zero characters) don't take any disk space to store (except the few bytes that the directory entry (18.2) uses).
You can use the empty files as "place markers" to remind you that something was there or belongs there. Some UNIX logging programs won't write errors to their log files unless the log files already exist. Empty files work fine for that.
Empty files hold a "timestamp" (just as files with text do) that shows when the file was last modified. I use empty files in some directories to remind me when I've last done something (backups, printouts (21.9), etc.). The find -newer (17.8, 17.9) command can compare other files to a timestamp file.
Well, you get the idea by now.
How can you empty a file? Watch out: when some editors say that a file has "no lines," they may still append a newline character when writing the file. Just one character still takes a block of disk space to store. Better:
In the Bourne shell, the most efficient way is to redirect the output of a null command:
If the file already exists, that command will truncate the file without needing a subprocess.
In the C shell copy the UNIX empty file, /dev/null (13.14), on top of the file:
cp /dev/null afile
You can also "almost" empty the file, leaving just a few lines, this way:
That's especially good for log files that you never want to delete
Use cat and rm, not mv-mv will break the link to
the original file (
afile) and replace it with the temporary file.