Re: sed --in-place bug (updates mtime of file even if no changes are made)
Bob Proulx <[email protected]>
| Newsgroups | gmane.comp.gnu.utils.bugs |
|---|---|
| Message-ID | <[email protected]> |
John Vanderpool wrote: > here is how this came to light, a very senior SA had the task of changing a > NASA mandated mail list from one name to another and the name was all > over the systems so he ran: > > find / -type f -exec sed --in-place 's/[email protected]/[email protected]/' {} \; > > and boom, every file on the systems all had the current date - ouch! Ouch! That probably would have taken a while to run too since it would have needed to read every file on the system. And if any files were NFS mounted then it would at least read them too. (It should fail over nfs when it tries to write files since root is squashed over nfs and has no permission.) I wouldn't recommend that as a way to change the email address everywhere because searching every file from the root because it is too large of a brush. I would really only want to change files that needed to be changed. That command may corrupted binary db files too. Let's assume that a program keeps addresses in an aliases.db file. The string may exist in the db file and be changed from one to the other. If the number of characters is exactly the same then it should be okay. But if the number of characters changes then the db file may not be stitched together correctly. The resulting db file may be corrupted. You would need to rebuild it from the source to fix it. > possible man page change: > > -i[SUFFIX], --in-place[=SUFFIX] > > edit files in place (makes backup if extension supplied) > WARNING: input file's mtime is updated regardless of > if sed made any changes or not It isn't just the mtime. The file is edited. And it does so by creating a new temporary file and moving it into place. The other times associated with the file atime, ctime, also change. The inode number associated with the file changes to the new file. I would rather see the behavior documented more fully so that the ramifications may be seen as opposed to calling out one of the infinite number of possibilities that may result downstream of using this option. Note that 'perl -i' has the same behavior. It says: -i[extension] specifies that files processed by the "<>" construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. ... I think just saying more words along those lines should be enough. BTW... Whenever I need to do something like this I always grep first and edit only if the grep tells me that I need to do something. That way the file isn't modified if there isn't any change to it. if grep -q [email protected] somefile; then sed --in-place 's/[email protected]/[email protected]/' fi Bob