Sponsored Content
Top Forums UNIX for Advanced & Expert Users Why is editing a file by renaming the new one safer? Post 303042321 by Cacializ on Saturday 21st of December 2019 10:55:59 AM
Old 12-21-2019
Why is editing a file by renaming the new one safer?

Hello:
I've been reading about ways to edit files from the command line, and I've found two websites which state that the following is the safest way to edit a file:
Code:
command original > new
mv new original

That is, renaming the newer file to the previous one. This is what the websites I mentioned state:
Quote:
Before you start, be warned that editing files is a really bad idea. The preferred way to modify a file is to create a new file within the same file system, write the modified content into it, and then mv it to the original name. This is the only way to prevent data loss in the event of a crash while writing. However, using a temp file and mv means that you break hardlinks to the file (unavoidably), that you would convert a symlink to hard file, and that you may need to take extra steps to transfer the ownership and permissions (and possible other metadata) of the original file to the new file. Some people prefer to roll the dice and accept the tiny possibility of data loss versus the greater possibility of hardlink loss and the inconvenience of chown/chmod (and potentially setfattr, setfacl, chattr...).
(Source: BashFAQ/021 - Greg's Wiki)

Quote:
So, generally speaking, to accomplish almost any editing task on a file, temporary files should be used. Sure, if the file is big, creating a temporary file becomes more and more inefficient, and requires that an amount of available free space roughly the same size of the original file is available. Nonetheless, it's by far the only right and sane way to do the job. Modern machines should have no disk space problems.

[...]

In any case, for our purposes the important thing to remember of these methods is that the old file stays around (whether under its original name or a different one) until the new one has been completely written, so errors can be detected and the old file rolled back. This makes them the preferred method for changing a file safely.
(Source: “In-place”¯ editing of files << \1)

What I don't understand is: what makes it safer? Why is it safer in the case the machine stops unexpectedly once the file was renamed? What's the logic behind this?

Thanks in advance.
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies

2. What is on Your Mind?

Nuclear Elephant iPhone Wipe making your iPhone Safer for Resale

For those with the iPhones here might read up how to wipe your personal data off the phone before reselling or trade-in. ;) source: Nuclear Elephant: iPhone Wipe June 1, 2008: Making your iPhone Safe for Resale Since my posts regarding the iPhone restore mode being insufficient for wiping... (0 Replies)
Discussion started by: sparcguy
0 Replies

3. What is on Your Mind?

Is M$ safer than UN*X(-LIKE)??

I know that you already know the answer to the question. It just springed in my mind after what happened yesterday. I was getting some books off of Amazon.com, since they are cheeper than bookstore, and my mother said something that made me laugh 'till I couldn't breathe. I have a Debian Lenny... (12 Replies)
Discussion started by: Texasone
12 Replies

4. Solaris

metaset - renaming /editing the set name - possible ?

Hi all I need to change my current metaset setname from lh-rms02ds to lh-lgwrms01ds. Is it possible ?? I know within veritas I do a vxedit, is there a way in SVM ? I notice theres a metarename, but this is for the metadevice level. Any tips ? help ?? Or do I need to delete /... (2 Replies)
Discussion started by: sbk1972
2 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

7. Shell Programming and Scripting

Need help in editing a file

I have a file which has 10 million records in it. When am trying to edit the file with vi, the following error occurs: ~ ~ ~ ~ ~ ~ ~ ~ "file1" Value too large for defined data type Is there any way that I can edit this file without using vi? Any help would be really appreciated.... (8 Replies)
Discussion started by: bobby1015
8 Replies

8. UNIX for Dummies Questions & Answers

# /UNIX.org file deleting is safer or not

Hi All, Please tell me the use of # /Unix.org file it has reserved a huge disk space, I want to know is it safer to truncate or delete. Thanks - Rukshan.:) (1 Reply)
Discussion started by: rukshan4u2c
1 Replies

9. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies
POEDIT(1)						      General Commands Manual							 POEDIT(1)

NAME
poedit - program to aid editing gettext po files SYNOPSIS
poedit [-a] [-n] <file.po> potooledit [-a] [-n] <file.po> DESCRIPTION
poedit is a shell script, which uses potool(1), iconv(1) and your favourite editor to help you edit gettext po files. It first retrieves only the untranslated entries from the file (or, if -a is specified, reorders the entries so that the translated ones are grouped separately from the untranslated ones) and puts them into a temporary file. Then recodes the file to the charmap of your cur- rent locale, and runs your favourite editor on the temporary file so you can add new translations. After you have finished, it recodes the file back to the original charset and merges the new translations back into the original file. poedit is also known as potooledit on Debian systems, to avoid a name clash with an unrelated program of the same name. OPTIONS
-a causes poedit to include already translated entries as well in the temporary file for editing (they are grouped separately, though). Without this option, only the untranslated entries (and the file header) are presented for editing. -n causes poedit not to do any charset recoding before nor after editing the file. You should only use this option if you know what you are doing. Be particularly careful when using this option (without -a) on files that are in an encoding incompatible with your locale's charmap. In that case, the intermediate file to be edited will probably only contain ASCII characters, which means your editor will be free to interpret this as your default locale encoding. This in turn, will make the file invalid when it is merged together with the already translated messages. See Debian bug #297074 for an example of this happening. SEE ALSO
potool(1), postats(1), locale charmap(1), msgmerge(1), msgfmt(1). AUTHOR
Poedit was written by Zbigniew Chyla and is now being maintained by Marcin Owsiany <porridge@debian.org>. September 21, 2007 POEDIT(1)
All times are GMT -4. The time now is 05:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy