0

When redirecting output to a file which already exists:

$ ls
my_file
$ dump_all > my_file

When does the file system get freed from the space consumed by the original file? (For example the concern could be that there is only space for one copy of the data)

Adam Terrey
  • 437
  • 1
  • 5
  • 8

1 Answers1

4

When does the file system get freed from the space consumed by the original file?

Immediately. Redirecting output to a file causes the shell to open the file with the O_TRUNC option, which will truncate the file (i.e, remove all of its contents) if it already exists.

  • 1
    Note that the file is truncated to 0 size slightly *before* the program (`dump_all` in this case) is started. The shell opens the file with `O_TRUNC`, then executes the program with that open file as its standard output. – Gordon Davisson Jan 30 '18 at 04:33
  • 1
    Note also that if two or more such commands are run simultaneously, results can get *interesting* as the two or more commands will all be writing to different offsets in the same file. Disk space that gets freed upon the truncation might suddenly be "reclaimed" by the file if the underlying filesystem doesn't support sparse files. – Andrew Henle Jan 30 '18 at 11:21