From: Andrew Savige Date: 2007-08-12T08:16:23+09:00 Subject: Re: Sorting a logfile, how would you write it? --- William James wrote: > It's my understanding that when you use -i, a temporary file > is created, the original file is deleted, and the temporary > file is renamed. Doesn't this cause unnecessary disk > fragmentation? To do this safely you'll need a temporary file. Slurping a file into memory, sorting it, then writing it back to the same file is an unsound practice, i.e. not "rerunnable-safe". Suppose, for example, you suffer a power failure half-way through writing back the file, or the write fails due to "disk full" or "user disk quota exceeded" or for any other reason. Oops, you've just corrupted your input file. Worse, if this program is run automatically or by a naive user you risk permanent data loss without knowing about it, for when the program is re-run after the write failure, it will use as its input file the now corrupted file and appear to "work". The simple remedy is to first write to a temporary file; once you're sure the temporary file has been written without error (and after the permissions on the temporary are updated to match the original) you then (atomically) rename the temporary file to the original. In that way, if writing the new file is interrupted for any reason, you can simply re-run the program without losing any data. Though you could roll all this code for yourself, the -i switch is more convenient, and safer than rewriting a file in place. Cheers, /-\ ____________________________________________________________________________________ Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage. http://au.docs.yahoo.com/mail/unlimitedstorage.html