nushell

how to redirect stdout to a file in NuShell?


how to redirect some command output to a file in NuShell?

In zsh/bash we can redirect output of ls command to a file as below

ls > fileList.txt

how can we do the same in NuShell?


Solution

  • To redirect/save into a file, use the save command (see the manual).

    However, save takes strings as input, while ls provides records. You can convert those records into strings by defining how.

    One way, if your primary interest lies in saving the looks, is to explicitly render the table as shown in your shell using the table command (see the manual which even lists ls | table as one of the examples):

    ls | table | save fileList.csv
    

    But if you are more interested in the data conveyed, re-format it into a datatype of your choice using to (see a list of formats). For example, to re-format it as CSV use to csv, for HTML use to html, for JSON use to json etc. There is also to text which simply lists all records line by line with their key names prepended. Its manual page even lists ls | to text as one of the examples.

    ls | to csv | save fileList.csv
    # or
    ls | to html | save fileList.html
    # or
    ls | to json | save fileList.json
    # or
    ls | to text | save fileList.txt
    # etc.