Truncate File (>)

Writes stdin to disk - overwriting contents if file already exists

Description

Redirects output to file.

If a file already exists, the contents will be truncated (overwritten). Otherwise a new file is created.

Usage

<stdin> |> [ -i | --ignore-pipeline-check ] filename

<stdin> |> [ -w | --wait-for-eof ] filename

Examples

g * |> files.txt

Flags

Detail

Race Condition Detection

If no flags are specified, then |> will check if the filename supplied is used in any parameters for other commands in the pipeline. If it has been, then |> will wait for an EOF (End Of File) from stdin before opening filename.

This is to allow pipelines like the following:

open example.log | regexp m/error/ |> example.log

Under traditional shells and Murex’s normal scheduler, all commands in a pipeline would run concurrently. This leads to a race condition where |> opens (and thus truncates) a file before other commands can read from it.

However by default, |> will check the pipeline to look for any other references of filename and if it exists, it will wait for an EOF before |> truncates filename.

This wait for EOF behaviour can be forced with the --wait-for-eof / -w flag.

Alternatively, if you want to force |> to run concurrently then you can disable the pipeline check with the --ignore-pipeline-check / -i flag.

High Memory Usage

WARNING! Waiting for EOF will cause |> to cache the pipeline into RAM. If your pipeline is parsing multi-gigabyte or larger files then you may experience performance issues.

For large datasets, it might be preferable to write to a temporary file first.

open example.log | regexp m/error/ |> example.log.tmp
mv example.log.tmp example.log

The move operation should be instantaneous on most filesystems because your operating system will just alter filesystem metadata rather than move the file contents.

Flag Without A Filename

If you specify a flag without a filename, eg |> --wait-for-eof, then it is assumed that the flag is the filename.

Syntactic Sugar

While |> is referred to as an operator, it’s actually a pipe followed by a builtin:

out "foobar" | > example.txt

Thus you can actually use > by itself.

Creating An Empty File

If > is at the start of a pipeline then it is treated as null input. This a convenient shortcut to create an empty file or blank an existing file.

Create a new empty file:

> newfile

Clear a large log file without deleting the file itself:

> large.log

Appending A File

To append a file (ie write at the end of the file without overwriting its contents) use >> instead.

Synonyms

See Also


This document was generated from builtins/core/io/write_doc.yaml.

This site's content is rebuilt automatically from murex's source code after each merge to the master branch. Downloadable murex binaries are also built with the website.

Last built on Tue Dec 10 22:56:57 UTC 2024 against commit 60f05a260f05a227caf73dd5b3478e3cb3f4bb24e46745b.

Current version is 6.4.1005 (develop) which has been verified against tests cases.