10

What is the fastest / most elegant way to read out a file and then write the content to that same file?

On Linux, this is not always the same as 'touching' a file, e.g. if the file represents some hardware device.

One possibility that worked for me is echo $(cat $file) > $file but I wondered if this is best practice.

3
  • How about "touch $file" to update its timestamp? May 8, 2014 at 17:30
  • 1
    No, actually, you can't run echo $(cat $file) >$file. The lack of quotes means your newlines get turned to spaces and your globs get expanded. May 8, 2014 at 17:38
  • touch didn't work in this case. My solution works fine so far but I don't know much about the specifics.
    – fweth
    May 8, 2014 at 17:47

3 Answers 3

10

You cannot generically do this in one step because writing to the file can interfere with reading from it. If you try this on a regular file, it's likely the > redirection will blank the file out before anything is even read.

Your safest bet is to split it into two steps. You can hide it behind a function call if you want.

rewrite() {
    local file=$1

    local contents=$(< "$file")
    cat <<< "$contents" > "$file"
}

...

rewrite /sys/misc/whatever
1
  • Nice, thanks. I just checked, in my specific case, the brightness file can't be blanked or overwritten with anything other than a number, that might be the reason why my solution is working despite your concerns.
    – fweth
    May 8, 2014 at 17:51
7

I understand that you are not interested in simple touch $file but rather a form of transformation of the file.

Since transformation of the file can cause its temporary corruption it is NOT good to make it in place while others may read it in the same moment.

Because of that temporary file is the best choice of yours, and only when you convert the file you should replace the file in a one step:

cat "$file" | process >"$file.$$"
mv "$file.$$" "$file"

This is the safest set of operations.

Of course I omitted operations like 'check if the file really exists' and so on, leaving it for you.

5
  • Why rm $file? The mv is an atomic operation, even when overwriting; using the rm means you're creating a race condition where none need exist. May 8, 2014 at 17:39
  • Also, you need quotes around your variables to be truly safe -- otherwise, a filename with spaces can be split to multiple arguments if this is run on a POSIX-compliant shell (pretty much any common shell except zsh). May 8, 2014 at 17:39
  • 1
    ...also, dont use $$` for this purpose -- mktemp exists for a reason; predicting temporary files' names lets attackers do nasty things if you're writing into a directory where other folks can create files (such as /tmp). May 8, 2014 at 17:40
  • @CharlesDuffy: Thank you for pointing the mv out. I had also put quotes around the file name (which really dislike on Linux, but I understand that people may like spaces in file names ;))
    – Grzegorz
    May 8, 2014 at 17:41
  • 1
    ...it's not really a matter of personal preference, it's a matter of correctness. I've seen TB of production data wiped out after a bug in some (non-bash) code dumped random data into a filename, and a backup script misinterpreted it. May 8, 2014 at 17:42
6

The sponge utility in the moreutils package allows this

~$ cat file
foo
~$ cat file | sponge file
~$ cat file
foo

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.